New format/fwrite options

Happi <>
Wed Mar 27 18:08:33 CET 2002


Richard Carlsson wrote:
> Seriously, how about ~B for signed binary and ~b for precision-width
> 2-complement binary (default precision 32?)
>
> fwrite("~B", [-5])  =>  -101
> fwrite("~.8b", [-5])  =>  11111011
> fwrite("~.*b", [-5, 8])  =>  11111011

Yes, that is what I want. (I included an implementation given io_lib_format
from my last mail. (For local Hipers: the patch is checked in.))

If we now just could allow 0x in inputs... ;)

--------------------------------------
I'm Happi, you should be happy.
Praeterea censeo "0xCA" scribere Erlang posse.



diff -r1.3 io_lib_format.erl
104c104,105
< collect_cc([$B|Fmt], [A,Base|Args]) -> {$B,[A,Base],Fmt,Args};
---
> collect_cc([$B|Fmt], [A|Args]) -> {$B,[A],Fmt,Args};
> collect_cc([$b|Fmt], [A|Args]) -> {$b,[A],Fmt,Args};
164a167,176
> control($B, [A], F, Adj, P, Pad, I) ->
>   string(
>     if A < 0 -> [$- | int_to_base(-A, [], 2)];
>      true ->  int_to_base(A, [], 2)
>     end,
>     F, Adj, P, Pad);
> control($b, [A], F, Adj, none, Pad, I) ->
>     string(int_to_binary(A, 32), F, Adj, none, Pad);
> control($b, [A], F, Adj, P, Pad, I) ->
>     string(int_to_binary(A, P), F, Adj, P, Pad);
421a434,450
>
> int_to_binary(N, Wordsize) ->
>     if N < 0 ->
>       Bits = int_to_base(-N, [],2),
>       pad(length(Bits),Bits,Wordsize,$1);
>        true ->
>       Bits = int_to_base(N, [], 2),
>       pad(length(Bits),Bits,Wordsize,$0)
>     end.
>
> pad(N,Bits,Wordsize, Pad) ->
>   if N < Wordsize ->
>       pad(N+1,[Pad|Bits],Wordsize,Pad);
>      true ->
>       Bits
>   end.
>






More information about the erlang-questions mailing list