Jump to content

PHP Binary Data


rlelek

Recommended Posts

I am trying to convert a C application to a PHP script, but I have come across one problem.

Is there a specific function to get a string or integer to binary data?

 

I have searched the PHP manual for a few hours now, as well as google, but i cannot find an answer.

All I found was how to convert from binary to hexadecimal.

 

Please let me know if this is even possible to do, and point me in the right direction!

 

Thank You!

 

 

 

Also, does anyone know of a good binary resource guide?

Link to comment
https://forums.phpfreaks.com/topic/117967-php-binary-data/
Share on other sites

This is for an Open Source Project for creating

the USPS Intelligent Mail barcode.

 

as stated in the specifications.....(routing code is zip code)

 

"3.2.1.1 Conversion of Routing Code – The routing code shall be converted from a 0-,

5-, 9-, or 11-digit string to an integer value in the range of 0 to 101,000,100,000 by

applying the following algorithm:

If the routing code is 0 digits long, Value = 0

If the routing code is 5 digits long, Value = (5-digit string converted to integer) + 1

If the routing code is 9 digits long, Value = (9-digit string converted to integer) + 100000 + 1

If the routing code is 11 digits long, Value = (11-digit string converted to integer) + 

1000000000 + 100000 + 1

The routing code shall then be converted into binary data. The binary data shall hold

104 bits maximum (or 13 bytes). The routing code binary data, which shall fit within 37

bits, shall be put into the rightmost 37 bits of the binary data. "

 

I see the PHP bitwise operators, but I think I need more than that :D

here's a snippet of the (unfinished) PHP code....

 


$source = "0123456709498765432101234";
if($source !== "^([0-9]{20})|([0-9]{25})|([0-9]{29})|([0-9]{31})$"){
    $l = 0;
    $zip = substr($source, 20);
    echo $zip . '<br />';
    switch(strlen($zip)){// add case 0
      case 5:
      	$l = $zip + 1;
      	break;
      case 9:
      	$l = $zip + (100000 + 1);
      	break;
      case 11:
      	$l = $zip + (1000000000 + 100000 +1);
      	break;
      default:
      	$l = 0;
    }
    $v = $l;
    $v = $v * 10 + substr($source, 0, 1);
    $v = $v * 5 + substr($source, 1, 1);
    $ds = $v && substr($source, 2, 18); /// used to be &
    $byteArray = array();
    $byteArray[12] = $l & 255;
    $byteArray[11] = $l >> 8 & 255;
    $byteArray[10] = $l >> 16 & 255;
    $byteArray[9] = $l >> 24 & 255;
    $byteArray[8] = $l >> 32 & 255;

 

Let me know if you need more code...

Please remember this will be open source.

 

Thanks for the replies!

Edit:

Thanks PapaFace! I almost didnt see it was a hyperlink! This is exactly what I needed!

Thank You PFMaBiSmAd! I Will Be looking at the function base_convert

 

(Still no one with a binary resource/guide?)

Link to comment
https://forums.phpfreaks.com/topic/117967-php-binary-data/#findComment-606889
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.