rlelek Posted August 4, 2008 Share Posted August 4, 2008 How would someone go about creating a byte array in PHP? I see google results for C# and maybe a few VB results, but no PHP. Basically, what I want to do is take a binary string, and then convert it to 13 bytes. I would then like to convert those bytes to display in hexadecimal form. something like this? 00 00 00 0D 13 8A 87 BA B5 CF 38 04 B1 (There are 13 listed) Please help! Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/118029-php-byte-array/ Share on other sites More sharing options...
ShaunO Posted August 4, 2008 Share Posted August 4, 2008 Maybe bin2hex() does what you need? Quote Link to comment https://forums.phpfreaks.com/topic/118029-php-byte-array/#findComment-607200 Share on other sites More sharing options...
rlelek Posted August 4, 2008 Author Share Posted August 4, 2008 sorry, bin2hex does not really do what I need to do. i first need to essentially "split" the code up into 13 bytes? I really don't know how to phrase it better than that. I've only started working with bits/bytes. Anyway, I do want each byte to be changed to a hex when it is split. So we have step 2, but we still need to figure out step 1.... Quote Link to comment https://forums.phpfreaks.com/topic/118029-php-byte-array/#findComment-607204 Share on other sites More sharing options...
kenrbnsn Posted August 4, 2008 Share Posted August 4, 2008 Does this: <?php $in_str = 'this is a test'; $hex_ary = array(); foreach (str_split($in_str) as $chr) $hex_ary[] = sprintf("%02X", ord($chr)); echo implode(' ',$hex_ary); ?> give you what you're looking for? Ken Quote Link to comment https://forums.phpfreaks.com/topic/118029-php-byte-array/#findComment-607213 Share on other sites More sharing options...
rlelek Posted August 4, 2008 Author Share Posted August 4, 2008 Thanks Ken, right before I read your post, ShaunO's idea worked...somewhat However, it is really odd, let me show everyone an output. first, I have the hex that I want (d138a87bab5cf3804b1); so I convert it to a decimal and it matches the variable! but when I try to convert back to a hexidecimal... things go WRONG. Have a look... /* $tb = 6.17512345671E+22 (integer) */ echo '<br />Test: ' . base_convert($tb, 10, 16); echo '<br />Test 2: ' . base_convert((hexdec('d138a87bab5cf3804b1')), 10, 16); /* both ouput 382994669bd2 when converted to hex */ I just don't get it, I think decimals and hexidecimals should be able to convert. Maybe it is too big of a number? Quote Link to comment https://forums.phpfreaks.com/topic/118029-php-byte-array/#findComment-607219 Share on other sites More sharing options...
sasa Posted August 4, 2008 Share Posted August 4, 2008 yes Quote Link to comment https://forums.phpfreaks.com/topic/118029-php-byte-array/#findComment-607228 Share on other sites More sharing options...
rlelek Posted August 4, 2008 Author Share Posted August 4, 2008 yes So if that yes means it is too big.... how should I get around it? I'll keep looking ..... Quote Link to comment https://forums.phpfreaks.com/topic/118029-php-byte-array/#findComment-607320 Share on other sites More sharing options...
kenrbnsn Posted August 4, 2008 Share Posted August 4, 2008 Can you explain exactly what you want to do? If you tell us more clearly, we may be able to help you. Ken Quote Link to comment https://forums.phpfreaks.com/topic/118029-php-byte-array/#findComment-607359 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.