我在将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>>
2条答案
按热度按时间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 and117,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 the0,17
here means that the length of the string is 17 bytes.117,48,48,48,48
stands foru0000
.\u
is an unknown escape sequence, so it just becomesu
, and the backslash is ignored.So if you want to get exactly
<<0,112,97,114,116,110,101,114,0>>
, you probably wantlist_to_binary
, or perhapsunicode:characters_to_binary
if you might have Unicode characters in your string:Alternatively, skip the string and create the binary straight away:
50few1ms2#
Erlang不支持
\u
转义。请改用\x00
。