Jump to content

Archived

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

The Little Guy

To Binary

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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().

Share this post


Link to post
Share on other sites

Ok, that is good to know!

 

Another thing...

 

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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

if I pad the letters, to make each letter in a string 8 bits, then save the file will the file display as a string of 1's and 0's or as binary data?

Share this post


Link to post
Share on other sites

Are you sure that you really want a file full of ASCII 1s and 0s; that doesn't fit the normal use of the phrase "save it as binary data".

Share this post


Link to post
Share on other sites

×

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.