正确使用unicode字符

问题描述 投票:-1回答:1

我试图用任何类型的字符保存二进制文件,例如:

$  LC_CTYPE=en_US.UTF-8 erl
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V9.2  (abort with ^G)
1> TextBinaryWithSpecialChar =  <<" Hey, If you’re down, I’m here to help">>.                    
<<32,72,101,121,44,32,73,102,32,121,111,117,25,114,101,32,
  100,111,119,110,44,32,73,25,109,32,104,101,114,...>>
2> 

我怎样才能保存我想要的东西:<<“嘿,如果你失败了,我来帮忙”>>

erlang otp
1个回答
2
投票
1> <<"you’re"/utf8>>. 
<<121,111,117,226,128,153,114,101>>

在UTF-8中,“右单引号”由三个字节表示:

             Hex 
          Notation
           -------
1st byte:    E2 => E=14    2 
                   1110  0010 = 226

2nd byte:    80 =>  8     0
                   1000  0000 = 128

3rd byte:    99 =>  9     9
                   1001  1001 = 153

我不知道当你没有为二进制文件指定utf8时,erlang如何获得25的“Right Single Quotation Mark”。 (14 + 2 + 9 = 25?)

在erlang中,"abc"只是列表[97,98,99]的快捷方式,类似于二进制文件:

5> <<97,98,99>>.
<<"abc">>

shell是否显示字符或数字是无关紧要的:

6> <<97,98,99>> == <<"abc">>.
true
© www.soinside.com 2019 - 2024. All rights reserved.