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

a 夏天 提交于 2019-12-12 10:45:07

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/43131909/how-many-characters-does-bin2hexrandom-bytes-depending-on-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!