[erlang-questions] double to float

Tony Rogvall <>
Thu Oct 18 20:08:48 CEST 2007


Erlang defaults to big endian (historical reasons) numbers that is  
high order bytes first,
for intel like machines they use low order bytes first.

use  X/little-float

/Tony

On 18 okt 2007, at 19.18, Sten Kvamme wrote:

> Vlad pointed in the right direction, so it's working now (thanks
> Vlad!). But it still puzzles me that I have to mirror the 8 bytes to
> have erl accepting them as a float. In C I just do like this:
> fread(Xrealp, sizeof(Xreal), 1, fpdxf);
>
> Here's the erlang code snippet:
>
> ac_double(Thefile) ->
> 	<<X:8/binary, B/binary>> = Thefile,
>     <<X1,X2,X3,X4,X5,X6,X7,X8>> = X,
>     <<Y/float>> = <<X8,X7,X6,X5,X4,X3,X2,X1>>,
> 	{B,Y}.
>
> Here is the whole thing (remember I have just bought the book ;-)
> http://www.kvamme.se/pub/dxf.erl.txt
>
> And here's a small binary dxf file to run on it.
> http://www.kvamme.se/pub/b.dxf
>
> I'm learning fast and it's real fun. I can truly recommend the book.
>
> /Sten Kvamme
>
> On Oct 16, 2007, at 20:43 , Vlad Dumitrescu wrote:
>
>> Hi and welcome,
>>
>> On 10/16/07, Sten Kvamme <> wrote:
>>> I'm reading a binary file with lots of double data types. Have
>>> someone already written some code to convert the double 8 bytes,
>>> <<184,162,71,68,75,65,104,64>> to a float?
>>
>> Do you mean
>>    <<X/float>> = <<184,162,71,68,75,65,104,64>>
>> ?
>>
>> regards,
>> Vlad
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list