1

I have a a php string:

e.g. df299cc4cda279e4d7344b42a8006d94488c753f

This is representing a 20 bit HEX output. How would I select the n'th byte? For example if I select the 3rd one it should return 9c.

Thanks.

3 Answers 3

3

You can take a look at chunk_split() or str_split() and get a result like:

$string = "df299cc4cda279e4d7344b42a8006d94488c753f";
$bytes = str_split($string, 2); 
$yourByte = $bytes[2]; 
Sign up to request clarification or add additional context in comments.

Comments

1
$bytes = str_split($string, 2);
echo $bytes[2];

Or you may want to convert the string to actual binary using hex2bin, then echo the index [2] of the string, possibly converting back to hex for output.

Comments

1

Given $n = byte number (3 in your example) and $data = string with hex output,

return substr($data, ($n - 1) * 2, 2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.