Search code examples
phpunicodecharacter-encodingbitnon-ascii-characters

PHP reversing a byte and printing it


I'm working with strings in PHP and the character 'a' in binary looks like this: 01100001.

What I want to do is reverse this, which then becomes 10000110 and convert back to its text representation, but everytime I try it either gives me a ? or a blank square indicating a non-printable or invalid character.

I tried online ASCII/UNICODE binary to text converters but they all give me nothing, why is that and how could I see what character does my byte decode to?


Solution

  • 10000110 is decimal 134 which means it's in the extended ASCII table (value over 127)

    To be displayed properly it should be converted to cp1252

    <?php
    for ($i = 33; $i <= 255; $i++) {
        echo "$i: " . htmlentities(chr($i), ENT_QUOTES, 'cp1252') . "<br />";
    }
    

    See also here