Jump to content

To Binary


The Little Guy

Recommended Posts

Because numbers do not begin with 0. If an items costs fifty dollars you never see it represented as $050 do you?

 

Just because many binary numbers are representative of values that can range from 0 to 256 (i.e. 8 bits) does not mean that all binary numbers are limited to that space. They can be less than or greater than 8 bits.

 

If you need the value presented to the user as representiing 8 bits, then simply use string_pad() or sprintf().

Link to comment
https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129143
Share on other sites

Maybe I am going at this the wrong way, but what I want to do, is write binary data to a file, and save it as binary data.

 

I then want to open the file and read the data.

 

Okay. Is there a question in there somewhere? You apparently already have the code to conver to binary. So, now you just need to save it to a file. You need to decide "how" it will be saved. You can save it delimited in some manner (e.g. tab or comma delimited) in which case you don't need to worry about the leading zeros. Or, if you want to store it as one really long string of zero's and one's then you will want to pad the binary valules so they are a consistent length.

Link to comment
https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129152
Share on other sites

I am just messing around, but I can't figure this out...

 

$hx = bin2hex('a');
echo base_convert($hx, 16, 2);

prints out 1100001

but I really want it to print: 01100001

 

why is the first 0 missing when my code runs?

 

$hx = bin2hex('a'); is wrong bin2hex() expects a binary string not a hex one however hex2bin() doesn't exist quite possibly because it's easy to create:

 

A = 1010

B = 1011

C = 1100

D = 1101

E = 1110

F = 1111

 

echo base_convert($hx, 16, 2); makes no sense converting from base16 to base16

Link to comment
https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129156
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.