Jump to content

Archived

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

rlelek

PHP byte array

Recommended Posts

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!

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

yes

 

So if that yes means it is too big....

how should I get around it?

 

I'll keep looking .....

Share this post


Link to post
Share on other sites

Can you explain exactly what you want to do? If you tell us more clearly, we may be able to help you.

 

Ken

Share this post


Link to post
Share on other sites

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