lua-users home
lua-l archive

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

On Sat, Sep 4, 2010 at 07:15, Patrick Mc(avery
<> wrote:
> I am having no trouble at all using Lua to manipulate bytes according to PIL
> but on P198 it says
> 'you can even do pattern matching over binary data'
> I would like to be able read and manipulate bits not bytes.
> The code below manipulates a few "1"'s but should change thousands.
> bin ='29fb.bin', 'r') --29fb is 512 KB I am on Linux no "rb"
> bin_all = bin:read("*all")
> bin_zero, ntimes  = bin_all.gsub(bin_all, '1', '0')
> print(ntimes) --> 20 --should be way more times then this
> Is there also a way to convert binary to hex in pure Lua, I know Mike Pall
> has a great library but it is there a way to do this in pure Lua?
> Is there also a way to read one bit? read(1) is one byte, is there a way to
> read one bit?
> Thanks for reading-Patrick

There's no built-in function to read a single bit; you should be able
to write one fairly trivially that reads a byte and returns only a
specified bit of it, or converts a string to a series of zeros and
ones and back.

BTW, "*all" is unnecessary in read; you only need "*a". The manual
doesn't specify what unrecognized formats will do, so this is
undefined behaviour and might not work in other implementations.

Sent from my toaster.