Hexadecimal conversions

Robert Virding <>
Tue Nov 26 11:56:26 CET 2002


Robert Virding wrote:
> ----- Original Message ----- 
> From: "Chris Pressey" <>
> To: <>
> Cc: <>
> Sent: Thursday, November 21, 2002 9:05 PM
> Subject: Re: Hexadecimal conversions
> 
>>Also on the subject, there was once on this list some code to add hex
>>formatting to io_lib:format/2, but I don't know if it ever made it into
>>OTP...
>>
>>-Chri
> 
> A few different attempts at adding hex output to io_lib:fwrite :-) have been done.
 > I did a reasonably complete one which gave some control over output 
format.
 > I will try and find it and post it to this list so you can see if 
this is what you want/need.
> 
> Robert

Looking  back through old erlang-questions entries I see that earlier 
this year I posted a modification to io_lib_format.erl which had both 
signed based integers and binary/hex output.

I am reposting it, and invite comments. If no one really complains 
loadly then perhaps it can go into the next distribution? There is one 
small bug which needs fixing.

Some examples:

Eshell V2001.09.13  (abort with ^G)
1> io:fwrite("~#~n", [4711]).                     %Default is 8
8#11147
ok
2> io:fwrite("~#~n", [-4711]).
-8#11147
ok
3> io:fwrite("~.16#~n", [-4711]).
-16#1267
ok
4> io:fwrite("~.16#~n", [4711]).
16#1267
ok
5> io:fwrite("~28.16#~n", [4711]).
                      16#1267
ok
6> io:fwrite("~b~n", [4711]).                     %Default is 32 bits
00000000000000000001001001100111
ok
7> io:fwrite("~b~n", [-4711]).
11111111111111111110110110011001
ok
8> io:fwrite("~.16b~n", [-4711]).                 %Default is 32 bits
1110110110011001
ok
9> io:fwrite("~.16b~n", [4711]).
0001001001100111
ok
10> io:fwrite("~.16x~n", [4711]).
1267
ok
11> io:fwrite("~.16x~n", [-4711]).
ed99
ok
12> io:fwrite("~x~n", [-4711]).
ffffed99
ok
13> io:fwrite("~x~n", [4711]).
00001267
ok

The precision field is used for the base with ~# and the number of bits 
in ~b/~x. The field width is still the field width. ~b/~x are really 
only meant to be used to display bits as it is not guaranteed that if 
you read them back again you will get the same value. Why is left as an 
exercise to the reader. :-)

Comments?

Robert
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: io_lib_format.erl
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20021126/38375338/attachment.ksh>


More information about the erlang-questions mailing list