Erlang: How to transform a decimale into a Hex string filled with zeros

你离开我真会死。 提交于 2019-12-12 13:04:34

问题


I would like to transform 42 (Base 10) into 000002A (Base 16) in Erlang...

I have found some pointers on the web :

io:format("~8..0B~n", [42]) -> 00000042

And

io:format("~.16B~n", [42]) -> 2A

But I cannot seems to find how to do both at the same time, I have tried :

io:format("~8..0.16B~n", [42])

Which seemed to be the logical thing, but it is not, it gives me an error.

Thanks.


回答1:


io:format("~8.16.0B~n", [42]).
0000002A

basically, it's ~F.P.Pad where:

  • F = field width
  • P = precsion
  • Pad = pad character

see the full io:format docs



来源:https://stackoverflow.com/questions/9466287/erlang-how-to-transform-a-decimale-into-a-hex-string-filled-with-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!