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