0

I need to take a large binary string (whose length will always be divisible by 8) ...

// 96-digit binary string
$str = '000000000011110000000000000000001111111111111111111111111111111111111111000000000000000000001111';

... then convert it to a binary value (to store in a mysql db as type varbinary), and later convert it back again to recreate that string.

This is most likely NOT a duplicate question. Every posted stackoverflow answer I could find is either broken (PHP7 apparently changed how some of these functions work) or doesn't offer a solution to this specific problem. I've tried a few things, such as ...

// get binary value from binary string
$bin = pack('H*', base_convert($str, 2, 16));

// get binary string from binary value
$str2 = str_pad(base_convert(unpack('H*', $bin)[1], 16, 2), 96, 0, STR_PAD_LEFT);

... but this doesn't actually work.

My goal is to go back and forth between the given binary string and the smallest binary value. How is this best done?

5
  • 1
    Unless you plan to store very large numbers of these strings (i.e. into the billions) then don't muck about - just store a string as a string. Commented May 24, 2021 at 23:03
  • If it's an integer you should be able to just convert in 8-character chunks using a loop, from the ASCII into a "real" binary string. bin1 = convert, binstr += bin1 Commented May 24, 2021 at 23:06
  • 1
    A follow-up thought: your example is 96 bits long, which will exceed the native integer capacity of PHP on any platform. Rather than converting to a binary integer of arbitrary length and unknown support, compress the string with gzdeflate() or similar. For your sample above gzdeflate() returns a 17 character string, a saving of around 80%. Commented May 24, 2021 at 23:29
  • 1
    @TangentiallyPerpendicular Your username checks out ... you've offered two very interesting and entirely perpendicular solutions :) Commented May 24, 2021 at 23:59
  • @DaveS Your approach makes sense ... if you feel like posting example code, the point is yours Commented May 25, 2021 at 0:12

1 Answer 1

2

These functions convert bit strings to binary character strings and back.

function binStr2charStr(string $binStr) : string
{
  $rest8 = strlen($binStr)%8;
  if($rest8) {
    $binStr = str_repeat('0', 8 - $rest8).$binStr;
  }
  $strChar = "";
  foreach(str_split($binStr,8) as $strBit8){
    $strChar .= chr(bindec($strBit8));
  } 
  return $strChar;
}


function charStr2binStr(string $charStr) : string
{
  $strBin = "";
  foreach(str_split($charStr,1) as $char){
    $strBin .= str_pad(decbin(ord($char)),8,'0', STR_PAD_LEFT);
  } 
  return $strBin;
}

usage:

// 96-digit binary string
$str = '000000000011110000000000000000001111111111111111111111111111111111111111000000000000000000001111';

$strChars = binStr2charStr($str);
// "\x00<\x00\x00\xff\xff\xff\xff\xff\x00\x00\x0f"

//back
$strBin = charStr2binStr($strChars);
Sign up to request clarification or add additional context in comments.

1 Comment

Where gzdeflate() reduces the 96-char string to 17, this yields 12. For smallest possible result, this is the way to go. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.