1

Considering the output of the following statement in php

hex2bin(dechex(4112))

And the output for this

"\020\020"

I have come to understand that this is the octal representation of the hex representation of the above decimal value (4112 -> 0x1010) with each octal part being the thing in between the backslashes. So three octal digits are used to represent two hex digits. Here the 0x10 translates to 020 in octal so we get \020\020 for 0x1010

But what is the name for this sort of encoding? Where does it come from? Searches on the internet about this did not help much


Also why does the following statement fail?

hex2bin(dechex(123123))

1 Answer 1

1

You're encoding numbers as binary data in a way that makes the most significant byte come first. This is called big-endian representation.

Encoding 123123 with your method fails because dechex produces an odd number of digits. You need two hex digits to make one byte, so hex2bin requires that the input string has an even length.

Sign up to request clarification or add additional context in comments.

3 Comments

oh maybe i wasnt clear. i meant to ask what the name of the specific format is called that represents bytes in octal with each bye separated by a backslash
although thanks for letting me know that the function failed because the string didn't have even length!
That's called string with octal escapes. Nothing in your question produces such a string, I guess it's just how whatever you're using to debug chooses to display non-printable characters.

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.