Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many characters does bin2hex(random_bytes()) depending on input?

I'm using the following code to genrate a simple UID for my app:

private function _createUid() {
    $bytes = random_bytes(128);
    $uid = bin2hex($bytes);

    return $uid;
}

By doing this, the result of _createUid() will be a 256 character string. My question is, will this string always be 256 characters if run on the same server, I'm aware of some differences on different servers. Also, is one byte equal to two characters?

Any help would be great, thanks!

like image 539
Joe Scotto Avatar asked Mar 31 '17 03:03

Joe Scotto


1 Answers

In hex a byte is always expressed as 2 characters.

Hexadecimal representation of a byte is a sequence of two character pairs. The first character represents the most significant 4 bits and the second character represents the least significant 4 bits of a byte.

like image 192
zaph Avatar answered Nov 15 '22 22:11

zaph