Given an input of a string with bytes that may be in either binary, octal, or hex, output the string's ASCII equivalent.
Input will be provided in the following format, for example:
501200100001147
which represents
0x50 0o120 0b01000011 0x47
which is equivalent (in ASCII) to
PPCG
Binary, octal, and hex will always be provided with 8, 3, and 2 digits respectively.
For the purposes of this challenge, only printable ASCII must be supported.
This is the range 32..126
inclusive. Therefore, it is impossible for there to
be ambiguity. Note that
A string represents binary if and only if it starts with a
0
and its second character is either a0
or a1
. All printable ASCII characters have their high bit off in binary (i.e. start with a0
), and none of them start with00
or01
in either hex or octal.With binary out of the way, note that all printable ASCII characters start with
2
-7
in hex and0
-1
in octal. Therefore, it is possible to unambiguously distinguish between hex and octal as well.
You may assume that hex input is provided as either lowercase or uppercase, whichever is more convenient.
Regex makes the parsing portion of the challenge semi-trivial. I don't want to ban the use of regex outright, but if you have a non-regex solution longer than its regex-using counterpart, feel free to post it along with the "real" answer anyway, since I'd be interested to see it as well. :)
Since this is code-golf, the shortest code in bytes will win.
Test cases:
In Out
-----------------------------------
501200100001147 | PPCG
5C01101111100 | \o@
313206306400110101 | 12345
2A200530402C | * + ,
0011111100111111 | ??
<empty string> | <empty string>