Writing binary data

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing binary data

Stockdale, Daire, SOE
Hello

Is there any way to write to file the binary representation of a
lua_Number without using a custom module? I'd like the following to
work:

local myNumbers = { 0.0, 1.0, 2.0, 3.0, 4.0 }
local myFile = assert( io.open( "myFile.dat", "wb" ) )
for i=1, #myNumbers do
        -- Write myNumbers[i] to file here as binary,
        -- as if in C we had called
        -- fwrite( &myNumbers[i], sizeof(myNumbers[i]), 1, myFile )
end


Thanks,
Daire


 
========================================
DISCLAIMER

The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged.  Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else.  

If you have received this email in error, please notify us immediately by e-mail on [hidden email] and then delete the email and any copies.  The SEGA Group have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.

========================================

Reply | Threaded
Open this post in threaded view
|

RE: Writing binary data

Dolan, Ryanne Thomas (UMR-Student)

Check out the string.char and string.byte functions.

http://www.lua.org/manual/5.1/manual.html#pdf-string.byte

-----Original Message-----
From: [hidden email] on behalf of Stockdale, Daire, SOE
Sent: Thu 5/11/2006 5:21 AM
To: [hidden email]
Subject: Writing binary data
 
Hello

Is there any way to write to file the binary representation of a
lua_Number without using a custom module? I'd like the following to
work:

local myNumbers = { 0.0, 1.0, 2.0, 3.0, 4.0 }
local myFile = assert( io.open( "myFile.dat", "wb" ) )
for i=1, #myNumbers do
        -- Write myNumbers[i] to file here as binary,
        -- as if in C we had called
        -- fwrite( &myNumbers[i], sizeof(myNumbers[i]), 1, myFile )
end


Thanks,
Daire


 
========================================
DISCLAIMER

The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged.  Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else.  

If you have received this email in error, please notify us immediately by e-mail on [hidden email] and then delete the email and any copies.  The SEGA Group have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.

========================================




winmail.dat (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Writing binary data

Adam D. Moss
These don't do what the OP needs.  But I don't know of anything
else that does, either.

Dolan, Ryanne Thomas (UMR-Student) wrote:

> Check out the string.char and string.byte functions.
>
> http://www.lua.org/manual/5.1/manual.html#pdf-string.byte
>
> -----Original Message-----
> From: [hidden email] on behalf of Stockdale, Daire, SOE
> Sent: Thu 5/11/2006 5:21 AM
> To: [hidden email]
> Subject: Writing binary data
>  
> Hello
>
> Is there any way to write to file the binary representation of a
> lua_Number without using a custom module? I'd like the following to
> work:
Reply | Threaded
Open this post in threaded view
|

Re: Writing binary data

Petite Abeille
In reply to this post by Stockdale, Daire, SOE

On May 11, 2006, at 12:21, Stockdale, Daire, SOE wrote:

> Is there any way to write to file the binary representation of a
> lua_Number without using a custom module?

Well, what's wrong with an external module? :)

For example, Luiz Henrique de Figueiredo has a nice, tidy library just
for that purpose:

"lpack — A library for packing and unpacking binary data."

"pack is called as follows: pack(F,x1,x2,...), where F is a string
describing
how the values x1, x2, ... are to be interpreted and formatted. Each
letter
in the format string F consumes one of the given values. Only values of
type
number or string are accepted. pack returns a (binary) string
containing the
values packed as described in F. The letter codes understood by pack
are listed
in lpack.c (they are inspired by Perl's codes but are not the same).
Numbers
following letter codes in F indicate repetitions."

http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lpack

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/