3

I have a raw string of bytes $b coming from

$b=sha1($k,true);

I need to know the value of $b[$ix]. The only way I've found is

$arr=unpack('Cw',$b[$ix]);
$value=$arr["w"];

But for such elemental operation it seems too much overload.

Is there a more straight way to access the bytes in a raw string?

1
  • just treat it as an array of bytes: $foo = 'abc123'; echo $foo[2] outputs c. Commented Jul 22, 2016 at 20:01

1 Answer 1

2

Ascii value:

$b=sha1($k,true);
echo ord($b[$ix]);
Sign up to request clarification or add additional context in comments.

2 Comments

This also works, $b=sha1($k); echo $b[2 * $ix] * 16 + $b[2 * $ix + 1];
$arr=unpack('C',$b[$ix]); $value=$arr[1];

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.