Jump to content

String representation of Hex to Binary?


wheelerc

Recommended Posts

Hi, I'm new here so go easy :) I'll try and explain the problem as best I can. I'm sure there is something simple i'm missing...

 

I'm trying to verify some data which is posted to a php script from an external server. One of the posted fields is a signed hex number, but its obviously a string as part of the $_POST vars. I need to convert this value to a binary number so that it can be encrypted and then verified.

 

An example hex string 'signature':

 

-52700d0e05079f7e0496c21e86d11ef5da2aa225260ec84ee9ece4bac263a6530cd2daf9ab2b98e26f3d0c9836e804144a96ea05fb8e95b825e8c6b58516be22416f5bc002558105a5886782c225e0895ab17c357b29ee2ceed2a32b922fb513644ce6465a02531f0df83442b04b1e0e995bf47fa2f0dc98dee01b61b6ac3386

 

The documentation for the system i'm trying to use says....

 

  • Convert the value of the signature parameter into a big endian 2’s compliment binary value.
  • If the first bit in the value is a 1, left pad with 1 until you get 1024 bits.  If the first bit in value us 0, left pad with 0 until we get 1024 bits.  This is consistent with 2’s compliment

Link to comment
Share on other sites

That thing is massive! I doubt there are any functions out there that can handle a number that big. I would convert each character individually and make a big string of one's and zero's.

 

Start at the end of the string and convert that letter to binary and place it into an array. When all the characters have been converted, pop them off into another array to fix the order.

 

I bet there is a better way to do this, so maybe see if someone else answeres, too.

Link to comment
Share on other sites

Well, i've got it to a 1024bit binary number (bit its only working for positive numbers at the moment)

 

--- POST Singature ---
1d89060ce43d3a797783817e5ea947d1a68ae0b375356b20338b506360adf87ba2eeb32b4909ad5d7d573583c77a26bf48835c5939c9df684399cece22af93e492b9a2708249c0fd4ca4088c7a3a74b8d09ac3a6ae6eb71fab1b08196bf70a32da71776e6191ea7aec4f20889c75059761efde40eb1247fe9b94f4ff57838703
length: 256
--- Binary Signature ---
0001110110001001000001100000110011100100001111010011101001111001011101111000001110000001011111100101111010101001010001111101000110100110100010101110000010110011011101010011010101101011001000000011001110001011010100000110001101100000101011011111100001111011101000101110111010110011001010110100100100001001101011010101110101111101010101110011010110000011110001110111101000100110101111110100100010000011010111000101100100111001110010011101111101101000010000111001100111001110110011100010001010101111100100111110010010010010101110011010001001110000100000100100100111000000111111010100110010100100000010001000110001111010001110100111010010111000110100001001101011000011101001101010111001101110101101110001111110101011000110110000100000011001011010111111011100001010001100101101101001110001011101110110111001100001100100011110101001111010111011000100111100100000100010001001110001110101000001011001011101100001111011111101111001000000111010110001001001000111111111101001101110010100111101001111111101010111100000111000011100000011
length: 1024

 

How would I go about converting that binary number into twos compliment if its negative?

Link to comment
Share on other sites

$string = ereg_replace("0", "q", $string);
$string = ereg_replace("1", "0", $string);
$string = ereg_replace("q", "1", $string);

("q" is just a placeholder)

 

That should work as two's compliment.

256 would be exactly 1024 as binary, as I'm sure you have figured out.

 

Looks like you've got it working pretty good.

Link to comment
Share on other sites

Oh yeah, forgot about the addition. You will probably want to add one to the hex value before converting it. Then you can use the three ereg_replaces that I suggested. One way to add one to a string would be to take the last character, $string[strlen($string)-1], and compare its ascii value to decimal 15. Since there is always the chance that it will keep going up the characters, like FFFFFFFFFFFFFFFFFFFFFFFFFF etc., I would suggest a recursive function to do this check. If it is over 15, go to the next and so on. Of course, if it is 15, you have to make it 0 and add to the previous character, which would require the recursive function again. This seems a lot more complicated than I originally thought. Maybe it will let you do this:

 

Change the entire string to decimal with hexdec($string) and cast it as an integer with settype($string, "integer") (php doesn't have hex type) simply increment the integer and put it back to hex with dechex($string). Then you can convert it to binary and flit the bits.

Link to comment
Share on other sites

I believe adding 1, then flipping the bits will yeild a different result to flipping the bits, then adding one. I've looked into the bcmath functions which may be of some use - settype could be handy too...

 

This is only hald of the whole problem - once i've got it into twos complement, i then have to rsa encrypt it and mess about with it some more... I don't think php is really cut out for this kind of work :(

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.