Saturday, September 6, 2014

How to convert an Un-Printable_latin1_list to binary in ERLANG

I needed to convert [1,2,3,4,5,6,7,8] to <<"1,2,3,4,5,6,7,8">>. When I used list_to_binary() it returns <<1,2,3,4,5,6,7,8>> which gives me an error in encoding the above output with the rfc4627:encode().

So, I was instructed to use

case io_lib:printable_latin1_list(Value) of
            true ->
                list_to_binary(Value);
            false ->
                list_to_binary(lists:subtract(lists:concat([lists:concat([",",X])||X<-Value]),","))
        end;


When the
Value = [1,2,3,4,5,6,7,8].
return value from the case will be <<"1,2,3,4,5,6,7,8">>

If the
Value = "abcdefgh". (or a printable list)
return value will be <<"abcdefgh">>

____________________________________________________________________________
Reference & Special Thanks goes to Upul Dissanayake@Sri Lanka

Labels: , , ,