> Erlang中文手册 > utf8_binary_to_list/1 把一个 utf8 编码的二进制数据转为一个 unicode 列表

asn1rt:utf8_binary_to_list/1

把一个 utf8 编码的二进制数据转为一个 unicode 列表

用法:

utf8_binary_to_list(UTF8Binary) -> {ok,UnicodeList} | {error,Reason}

内部实现:

%% utf8_binary_to_list/1 transforms a utf8 encoded binary to a list of
%% unicode elements, where each element is the unicode integer value
%% of a utf8 character.
%% Bin is a utf8 encoded value. The return value is either {ok,Val} or
%% {error,Reason}. Val is a list of integers, where each integer is a
%% unicode character value.
utf8_binary_to_list(Bin) when is_binary(Bin) ->
    utf8_binary_to_list(Bin,[]).

utf8_binary_to_list(>,Acc) ->
    {ok,lists:reverse(Acc)};
utf8_binary_to_list(Bin,Acc) ->
    Len = utf8_binary_len(Bin),
    case catch split_binary(Bin,Len) of
	{CharBin,RestBin} -> 
	    case utf8_binary_char(CharBin) of
		C when is_integer(C) -> 
		    utf8_binary_to_list(RestBin,[C|Acc]);
		Err -> Err
	    end;
	Err -> {error,{asn1,{bad_encoded_utf8string,Err}}}
    end.

把一个 UTF8 编码的二进制数据转为一个整数形式的列表,列表里的每一个整数值(大于 255 的整数值是一个中文字符)代表一个字符它对应的 unicode 值。如果二进制数据不是一个合法的 UTF8 编码的字符串,那么转换失败。

asn1rt:utf8_binary_to_list(list_to_binary("abc测试")).