The Little Guy Posted November 1, 2010 Share Posted November 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/217485-to-binary/ Share on other sites More sharing options...
Psycho Posted November 1, 2010 Share Posted November 1, 2010 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(). Quote Link to comment https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129143 Share on other sites More sharing options...
The Little Guy Posted November 1, 2010 Author Share Posted November 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129147 Share on other sites More sharing options...
Psycho Posted November 1, 2010 Share Posted November 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129152 Share on other sites More sharing options...
ignace Posted November 1, 2010 Share Posted November 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129156 Share on other sites More sharing options...
The Little Guy Posted November 1, 2010 Author Share Posted November 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129157 Share on other sites More sharing options...
salathe Posted November 2, 2010 Share Posted November 2, 2010 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". Quote Link to comment https://forums.phpfreaks.com/topic/217485-to-binary/#findComment-1129336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.