user3165474 user3165474 - 2 months ago 22
PHP Question

PHP - HEX to EBCDIC Conversion

How do i convert an

Hex
value to
EBCDIC
in PHP?

What is the most practical way to do it?

What i did so far is
hex
to
Ascii
:

Hex : 313233343536373832313332333435
Ascii: 123456782132345


By using:

hex2bin($foo);

Answer

strtr should do the job, if you provide ebcdic to ascii mapping.

An example mapping for 3-letters alphabet:

$ebcdicSet = "\x81\x84\xa2";
$asciiSet = "ads";

$ebcdic = "\x81\xa2\x84\x81\xa2\x84";  // 6 bytes
$ascii = strtr($ebcdic, $ebcdicSet, $asciiSet);

echo $ascii; // outputs "asdasd"

If your input is not binary hex, but a sting with hex representation, you need to hex2bin it first:

$ebcdic = "81a28481a284"; // 12 bytes
$ascii = strtr(hex2bin($ebcdic), $ebcdicSet, $asciiSet);
echo $ascii; // outputs "asdasd"