ASCII code not displayable
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>
精彩评论