CyberLawrence Posted August 9, 2021 Share Posted August 9, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/ Share on other sites More sharing options...
requinix Posted August 9, 2021 Share Posted August 9, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588943 Share on other sites More sharing options...
Solution Barand Posted August 9, 2021 Solution Share Posted August 9, 2021 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588944 Share on other sites More sharing options...
CyberLawrence Posted August 9, 2021 Author Share Posted August 9, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588952 Share on other sites More sharing options...
requinix Posted August 9, 2021 Share Posted August 9, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588953 Share on other sites More sharing options...
CyberLawrence Posted August 9, 2021 Author Share Posted August 9, 2021 15 minutes ago, requinix said: Not quite. It will look like var_dump(base_convert("A1", 16, 2)); whatever that says it looks like. string(8) "10100001" Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588954 Share on other sites More sharing options...
requinix Posted August 10, 2021 Share Posted August 10, 2021 There ya go: it's not a number but a string containing some number of digits - perhaps less than 8 of them if the number was <=127. So what's your next thought? Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588955 Share on other sites More sharing options...
CyberLawrence Posted August 10, 2021 Author Share Posted August 10, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588958 Share on other sites More sharing options...
ginerjm Posted August 10, 2021 Share Posted August 10, 2021 form vars from html are always received as strings in php. Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588961 Share on other sites More sharing options...
requinix Posted August 10, 2021 Share Posted August 10, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588962 Share on other sites More sharing options...
CyberLawrence Posted August 10, 2021 Author Share Posted August 10, 2021 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 ^ Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588964 Share on other sites More sharing options...
Barand Posted August 10, 2021 Share Posted August 10, 2021 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; Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588968 Share on other sites More sharing options...
CyberLawrence Posted August 10, 2021 Author Share Posted August 10, 2021 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! Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588991 Share on other sites More sharing options...
Barand Posted August 10, 2021 Share Posted August 10, 2021 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; } Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588993 Share on other sites More sharing options...
requinix Posted August 10, 2021 Share Posted August 10, 2021 I was thinking base_convert 16->2 + str_split... Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1588999 Share on other sites More sharing options...
Barand Posted August 10, 2021 Share Posted August 10, 2021 Good thinking, Batman! $vars = array_reverse(str_split(base_convert($input, 16, 2))); Quote Link to comment https://forums.phpfreaks.com/topic/313519-unpacking-a-binary-number-string/#findComment-1589004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.