lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Aug 1, 2014, at 12:02 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> 
> Four functions with names like string.undumpfloat merely for access
> to the machine representation of one number at a time is not elegant.
> The intention is clearly to facilitate eventual reading and writing of
> binary files without painful conversions, so why not handle several
> numbers at once?
> 
> One could do the writing part by adding some specifiers to
> string.format, or (since string.format implies just the sort of
> conversion that we wish to avoid) bring in string.bformat (binary
> format) with a similar syntax. For the inverse operation,
> string.bscan seems a natural name.
> 
> For example, instead of
> 
> str = table.concat{string.dumpfloat(n1,"f","b"),string.dumpint(n2,8,"l")}
> 
> one would write
> 
> str = string.bformat("%fb%8l",n1,n2)
> 
> and
> 
> n1, n2 = string.scanb(str,"%fb%8l")
> 
> instead of some code that I am too terrified even to attempt.
> 

We have developed a pretty sophisticated library to do just that, including handling padding and alignment, variable widths, sub-tables, repeats (including repeats based on previously read values), endian handling etc. If there is sufficient interest I can look into making a public domain version available.

—Tim