質問

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