在Erlang中,将整数转换为字符串可以使用integer_to_list/1
函数。这个函数接受一个整数作为参数,并返回一个表示该整数的字符串。例如:
Int = 123,
String = integer_to_list(Int),
io:format("The integer ~p as a string is: ~s~n", [Int, String]).
输出:
The integer 123 as a string is: 123
如果需要将整数转换为二进制字符串,可以使用integer_to_binary/1
函数。例如:
Int = 123,
Binary = integer_to_binary(Int),
io:format("The integer ~p as a binary is: ~p~n", [Int, Binary]).
输出:
The integer 123 as a binary is: <<"123">>
如果需要将整数转换为十六进制字符串,可以使用integer_to_list/2
函数,并将第二个参数设置为16。例如:
Int = 123,
HexString = integer_to_list(Int, 16),
io:format("The integer ~p as a hex string is: ~s~n", [Int, HexString]).
输出:
The integer 123 as a hex string is: 7b
领取专属 10元无门槛券
手把手带您无忧上云