4

How can I get a similar function with pack/unpack (or other short function)?

function getHEX($number) {
    switch($number) {
        case 0: $ret = "\x00\x00\x00\x00"; break;
        case 1: $ret = "\x00\x00\x00\x01"; break;
        case 2: $ret = "\x00\x00\x00\x02"; break;
        case 3: $ret = "\x00\x00\x00\x03"; break;
        // (...)

        default: $ret = "\x00\x00\x00\x00";
    }

    return $ret;
}
1

3 Answers 3

5

You could do it with dechex in PHP:

<?php
echo dechex(10) . "\n";
echo dechex(47);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but the return value must be 4 bytes. When $ number is 1 calls me only 1 Byte
5

Here is a hint:

str_pad(dechex($number), 4, "0", STR_PAD_LEFT)

Comments

4

This function has solved my problem

pack("H*", sprintf("%08X", $number));

1 Comment

echo chr($number);

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.