Jump to content

unpacking a binary number (string)


Go to solution Solved by Barand,

Recommended Posts

This might be an easy fix.

I need to break an 8 bit binary number into 8 separate variables. When using unpack(), it pushes the first zeros it finds to the back as NULLs

0111 1111 gets translated at 1111 111 null

$binary3 = 01111111;

list($b7, $b6, $b5, $b4, $b3, $b2, $b1, $b0) = array_values(unpack('c*', $binary3));

var_dump($b7, $b6, $b5, $b4, $b3, $b2, $b1, $b0);

 

My OUTPUT


-----HIBYTE------
7f HEX
01111111
-----LOBYTE------
7f HEX
01111111
-----DATABYTE------
7f HEX
01111111
---------------

int(49) int(49) int(49) int(49) int(49) int(49) int(49) NULL
int(49) int(49) int(49) int(49) int(49) int(49) int(49) NULL
int(49) int(49) int(49) int(49) int(49) int(49) int(49) NULL

Bit 0 is
Bit 1 is 49
Bit 2 is 49
Bit 3 is 49
Bit 4 is 49
Bit 5 is 49
Bit 6 is 49
Bit 7 is 49

 

Link to comment
https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/
Share on other sites

1 hour ago, CyberLawrence said:

I need to break an 8 bit binary number into 8 separate variables.

Let's forget unpack() for a minute.

Is the variable a number or a string? Because what you have in this code

$binary3 = 01111111;

is not a binary number.

  • Solution

Still forgetting unpack(), for 8 bits to 8 vars

$binary3 = 0b10110001;

for ($k=0; $k<8; $k++) {
    $vars[$k]  = ($binary3 & 2**$k) ? 1:0;
}

giving

$vars = Array
            (
                [0] => 1
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 1
                [5] => 1
                [6] => 0
                [7] => 1
            )

 

13 hours ago, Barand said:

Still forgetting unpack(), for 8 bits to 8 vars

$binary3 = 0b10110001;

for ($k=0; $k<8; $k++) {
    $vars[$k]  = ($binary3 & 2**$k) ? 1:0;
}

giving

$vars = Array
            (
                [0] => 1
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 1
                [5] => 1
                [6] => 0
                [7] => 1
            )

 

I'll give Barand's solution a try.

I am taking in HEX from a html form (FF, EF, A1....) then using baseconvert to convert that to binary. that's where the "binary" is coming from.

Obviously its not true binary...

After the HEX gets converted... shouldn't it look like this as Barand has it? 0b10110001

43 minutes ago, CyberLawrence said:

After the HEX gets converted... shouldn't it look like this as Barand has it? 0b10110001

Not quite. It will look like

var_dump(base_convert("A1", 16, 2));

whatever that says it looks like.

Apparently I'm not getting what I need from the form, ie. actual numbers. I must be getting strings of numbers without base types? 0b, 0x, ..

I gotta look over some HTML code... or find out how to convert the string into a number.

PHP will get exactly what you typed into the form. Typed 1010101? That's what you'll get. Typed 85? That's what you'll get.

Decide whether you want the input to be the completely obscure binary representation of a number or to be the very familiar human concept of a decimal number (cough) then validate the input accordingly: binary would be up to 8 digits of 0 and 1, a number would be something 0-255.

What you do next depends on whether it was the binary or decimal digits, but either way you've got lots of options available: bitwise math, base_convert, str_split, string offsets, substr... The list goes on, so decide what kind of process would make sense to you and find the PHP code to express it.

Entering as HEX via the HTML form... converting to binary in order to get individual bits as variables. My breakdown is between the form and assigning the binary representation to a variable name.

Barand's code is fine...  I just need to get to this point ---> $binary3 = 0b10110001;

from this point

$WORD =   $_POST["BYTE"];

$binary3 = (base_convert($WORD,16,2));      <------ shouldn't this give me that ^

These all have the same value

+------------+------------+------------+
|  Binary    |  Decimal   |   Hex      |
+------------+------------+------------+
|  10110001  |    177     |    B1      |
+------------+------------+------------+

In my code, setting $binary3 to any of these will give the same result
 

$binary3 = 0b10110001;

$binary3 = 177;

$binary3 = 0xB1;

 

10 hours ago, Barand said:

These all have the same value

+------------+------------+------------+
|  Binary    |  Decimal   |   Hex      |
+------------+------------+------------+
|  10110001  |    177     |    B1      |
+------------+------------+------------+
$binary3 = 0b10110001;

$binary3 = 177;

$binary3 = 0xB1;

In my code, setting $binary3 to any of these will give the same result
 

 

Correct, I am aware that all those numbers are the same just in different number bases. My issue is the data coming from the HTML Form is not numeric. I convert it to binary from what I receive as HEX in text format.

The variable $binary3 will never be the same number.... so it has to be a variable. Your code works great for manually entering the binary code.

AH HA! Got it. I don't need to convert it to binary. I just convert it to decimal and use your code to "strip" the bits out of the decimal number!

Thumbs up to you sir!

I was converting to binary to see if my code was working correctly and got stuck on that....

Thanks to all!

31 minutes ago, CyberLawrence said:

AH HA! Got it.

Well done.

For the record...

$input = 'B1';                                     // hex iput

$decimal = base_convert($input, 16, 10);           // convert to 177
for ($k=0; $k<8; $k++) {
    $vars[$k]  = ($decimal & 2**$k) ? 1:0;
} 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.