I have a UUID in binary format in my Lua program (just 16 bytes in string). I need to convert it to string format, for example 550e8400-e29b-41d4-a716-446655440000.

How can I convert it?

有帮助吗?

解决方案 2

string.format("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", string.byte(str,1,16))

其他提示

Here's a program to convert a 2 bytes string to its hex format:

local str = "\x55\x0e"
local result = string.format("%02x%02x", string.byte(str, 1, 2))
print(result)

Output: 550e

string.byte gets the internal numeric representation of the characters, and then use string.format to get the hex format.

You can extend it to convert a UUID yourself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top