Jump to content

unpacking a binary number (string)


CyberLawrence
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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ^

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.