如何转换字符串' 010101010'在PHP中使用真正的二进制字符串

时间:2016-03-29 15:11:18

标签: php mysql ipv6 ipv4

我想将IP地址存储到MySQL VARBINARY(16)中。我已将二进制地址作为字符串'01001010010100101001010010100101'

当我将其插入MySQL时。我意识到这不会自动转换。

在搜索PHP手册之后,我很惊讶没有这方面的功能。

需要有关如何将二进制字符串转换为VARBINARY的帮助。

3 个答案:

答案 0 :(得分:0)

在PHP中,您可以使用bindeclong2ip将二进制字符串转换为IPv4地址:

bindec - 返回binary_string参数表示的二进制数的十进制等值。

long2ip - 将长整数地址转换为(IPv4)Internet标准点阵格式的字符串

$ip_as_int = bindec('01001010010100101001010010100101');
$ipv4 = long2ip ( $ip_as_int );
echo $ipv4;

74.82.148.165

答案 1 :(得分:0)

谢谢大家!但我认为我找到了一个更好的解决方案:6年前发布在PHP手册页上的人:

http://php.net/manual/en/function.pack.php#93085

<?php
function bin2bstr($input)
// Convert a binary expression (e.g., "100111") into a binary-string
{
  if (!is_string($input)) return null; // Sanity check

  // Pack into a string
  return pack('H*', base_convert($input, 2, 16));
}

function bstr2bin($input)
// Binary representation of a binary-string
{
  if (!is_string($input)) return null; // Sanity check

  // Unpack as a hexadecimal string
  $value = unpack('H*', $input);

  // Output binary representation
  return base_convert($value[1], 16, 2);
}

// Returns string(3) "ABC"
var_dump(bin2bstr('01000001 01000010 01000011'));

// Returns string(24) "010000010100001001000011"
var_dump(bstr2bin('ABC'));
?>

答案 2 :(得分:-1)

您可以通过组合功能function isRegistered() { var db = window.sqlitePlugin.openDatabase({name: "my.db"}); console.log("CT: registering tables") ; db.transaction(checkTable, errorCB,function(){ console.log("Success") ; }); } function isTableExists(tx, tableName, callback) { tx.executeSql('SELECT * FROM '+tableName, [], function(tx, resultSet) { if (resultSet.rows.length <= 0) { callback(false,0); } else { callback(true,2); } }, function(err) { callback(false,1); }) }; function checkTable(tx) { // create table isTableExists(tx, "myInfo", function(status,avar) { console.log("Avar: "+avar) ; if (!status) { console.log("CT: CHECKING myInfo") ; tx.executeSql("CREATE TABLE IF NOT EXISTS myInfo ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,'name' VARCHAR,'phone' VARCHAR,'email' VARCHAR,'fbID' VARCHAR,'fbToken' VARCHAR,'regCode' VARCHAR)",[],function(tx,res) { console.log("A1 Success: "+JSON.stringify(res)) ; tx.executeSql('INSERT INTO myInfo VALUES (?,?,?,?,?,?,?)',[0,"","","","","",""],function(res) { console.log("A2 Success: " + JSON.stringify(res) + " -- probably 1"); }, function(err) { console.log("A2 fail: "+JSON.stringify(err)) ; }) ; },function(err) { console.log("A1 Fail : "+ JSON.stringify(err)) ; }) ; } }, function(err){ console.log("A Fail: "+JSON.stringify(err)) ; }) ; isTableExists(tx, "mySettings", function(status,bvar) { console.log("Bvar: "+bvar) ; if (!status) { tx.executeSql('CREATE TABLE IF NOT EXISTS "mySettings" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,"vendor" VARCHAR,"display" VARCHAR,"aggregate" VARCHAR)',[],function(tx,res) { console.log("B1 Success: , "+ JSON.stringify(res)) ; var vendors = ["General","Twist","Unter","Sobi"] ; for (var n=0;n<vendors.length;n++) { tx.executeSql('INSERT INTO mySettings (id,vendor,display,aggregate) VALUES (?,?,?,?);',["","","",""],function(res) { console.log("B2 Success: " + n + " : " + JSON.stringify(res)) ; },function(err) { console.log("B2 Fail: "+JSON.stringify(err)) ; }) ; } },function(err) { console.log("B1 Fail: "+JSON.stringify(err)) ; }) ; } }, function(err){ console.log("B Fail: "+JSON.stringify(err)) ; }) ; } /* three issues: 1. everytime I execute the app, it "creates" the tables...it should only do it once. Avar & Bvar are returning "1" every time 2. The next tx.executeSql("INSERT...is not triggering (that I can tell). 3. Getting general error from isRegistered(...errorCB...); - which I believe is coming from function isTableExists() Error processing SQL: 0, a statement error callback did not return false: no such table: myInfo (code 1): , while compiling: SELECT * FROM myInfo ################################################################# Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (no such table: myInfo (code 1): , while compiling: SELECT * FROM myInfo) ################################################################# */ 完成此操作。

inet_ntoa

此外,字符串结果可以转换为conv

mysql> select inet_ntoa(conv('01001010010100101001010010100101',2,10));
+----------------------------------------------------------+
| inet_ntoa(conv('01001010010100101001010010100101',2,10)) |
+----------------------------------------------------------+
| 74.82.148.165                                            |
+----------------------------------------------------------+
1 row in set (0.00 sec)

BINARY字符串也可以使用mysql> select CAST(inet_ntoa(conv('01001010010100101001010010100101',2,10)) AS BINARY); +--------------------------------------------------------------------------+ | CAST(inet_ntoa(conv('01001010010100101001010010100101',2,10)) AS BINARY) | +--------------------------------------------------------------------------+ | 74.82.148.165 | +--------------------------------------------------------------------------+ 1 row in set (0.00 sec) BINARY轻松放入VARBINARY(16)列。