开发者

ASCII code not displayable

开发者 https://www.devze.com 2023-02-03 17:25 出处:网络
I would like to write a function which convert the \"no visible ASCII code\" in visible string example:

I would like to write a function which convert the "no visible ASCII code" in visible string

example:

abc\r\n will become abc<0d><0a>

what is the best approach ? does this开发者_JAVA百科 function already exist ?


pp(C) when C <32 ; C>126 ->
 io_lib:format("<~2.16.0B>",[C]);
pp(C) ->
 C.

1>lists:flatten(lists:map(fun pp/1,"abc\r\n")).
"abc<0D><0A>"


Just rewriting the solution given by @Nibon

pp(Str) -> pp(Str, []).

pp([],Acc) -> lists:flatten(lists:reverse(Acc));
pp([C|S], Acc) when C < 32; C>126 -> pp(S,[io_lib:format("<~2.16.0B>",[C]) | Acc]);
pp([C|S], Acc) -> pp(S,[C | Acc]).

1> pp:pp("abc\r\n").
"abc<0D><0A>"


I guess you could use io:format

1>io:format("~w~n",["abc\r\n"]).
[97,98,99,13,10]
ok
2>
0

精彩评论

暂无评论...
验证码 换一张
取 消