Erlang -如何将\u0000字符转换为二进制?

yx2lnoni  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(319)

我在将Unicode字符转换为二进制时遇到问题。
编码:

Text = "\u0000partner\u0000"
Bin = term_to_binary(Text, [compressed, {minor_version,1}]),

结果:
<<131,107,0,17,117,48,48,48,48,112,97,114,116,110,101,114,117,48,48,48,48>>
但是当我从外部服务接收数据时,我看到有效负载具有:
<<0,112,97,114,116,110,101,114,0>>
这意味着一次\u0000被转换为<<0>>,一次被转换为<<131,107,0,17,117,48,48,48,48>>作为句子中的第一个字符,以及117,48,48,48,48和句子的结尾。
问题是:如何将<<0,112,97,114,116,110,101,114,0>>转换为"\u0000partner\u0000"或将此字符串转换为<<0,112,97,114,116,110,101,114,0>>

nuypyhwy

nuypyhwy1#

As described in the Escape Sequences section of the Erlang reference manual, Erlang doesn't support the \uXXXX escape format, only \xXX (exactly two digits) and \x{XXXX} (variable number of digits).
As for your question:
It means that one time \u0000 is converted to <<0>> one time to <<131,107,0,17,117,48,48,48,48>> as a first character in sentence and 117,48,48,48,48 and the end of the sentence.
What's happening here is that term_to_binary creates a binary in the External Term Format . The external term format always starts with a 131 byte, followed by a type byte. 107 is the type byte for a string, whose representation starts with a two-byte big-endian length - so the 0,17 here means that the length of the string is 17 bytes. 117,48,48,48,48 stands for u0000 . \u is an unknown escape sequence, so it just becomes u , and the backslash is ignored.
So if you want to get exactly <<0,112,97,114,116,110,101,114,0>> , you probably want list_to_binary , or perhaps unicode:characters_to_binary if you might have Unicode characters in your string:

> Text = "\x{0000}partner\x{0000}".
[0,112,97,114,116,110,101,114,0]
> list_to_binary(Text).
<<0,112,97,114,116,110,101,114,0>>
> unicode:characters_to_binary(Text).
<<0,112,97,114,116,110,101,114,0>>

Alternatively, skip the string and create the binary straight away:

> Bin = <<"\x{0000}partner\x{0000}">>.     
<<0,112,97,114,116,110,101,114,0>>
50few1ms

50few1ms2#

Erlang不支持\u转义。请改用\x00

Text = "\x00partner\x00".
[0,112,97,114,116,110,101,114,0]
Bin = term_to_binary(Text, [compressed, {minor_version,1}]).
<<131,107,0,9,0,112,97,114,116,110,101,114,0>>

相关问题