lua-users home
lua-l archive

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


Oops, I wrote this in email without reviewing/testing... the "local r = {}" isn't necessary.

On Wed, Mar 11, 2009 at 5:33 PM, Joseph Stewart <joseph.stewart@gmail.com> wrote:
How about this instead:

function binary(t)
  local r = {}
  for i=1,#t do
    if type(t[i]) == "number" then t[i] = string.char(t[i]) end
  end
  return table.concat(t)
end

x = binary{"this", 0x81, "this", 0x33}

I know this is less efficient than having the lexer do it, but it's an option that works without any mods to the core.

On Wed, Mar 11, 2009 at 5:17 PM, Sam Roberts <vieuxtech@gmail.com> wrote:
On Wed, Mar 11, 2009 at 8:14 AM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>> This is also my opinion. Hex escape sequences feel more natural than
>> decimal ones (at least to programmers) :) It would be interesting to
>> know why decimal ones were chosen in the first place ?
>
> This may sound weird, but I know many people that grew up comfortable
> with decimal numbers...
All the arguments that apply to the usefulness of hex literal
representation for numbers:

i = 0x54 -- this is lua code bloat? It could have been i = 82

apply to strings

i = "\x54"

Including the argument that it can be done perfectly well with an
add-on function:

i = X(54)

I've never used a programming language with 1-based array indexing. I
didn't "expect" that, but I can work with it no problem, as with
algol-like do/end, 0 being true (actually, I expected that, but
whatever :-), and all the other areas where lua is or is not similar
to language X.

The lack of hex encodings isn't about expectations when coming from
other languages, and I regret people keep bringing that up since its
obviously not convincing.

Its about the difficulty of working with binary data in lua strings,
which otherwise are will suited for this task. Its about the printout
I keep beside my monitor that I continuously refer to when coding in
lua to map decimal back and forth to binary-friendly hex format, and
my tediously verbose code:

 "this"..string.char(0x81).."that"..string.char(0x33)

instead of

 "this\x81that\x33"

> (Also, we wanted to make easy the encoding of IPs ;)

This is the only use I can think of where decimal is routinely used to
deal with binary, and the  "familiar with decimal numbers" argument
would suggest using 2130706433 instead of 127.0.0.1 for localhost
would be more comfortable for people. Browsers support this, but I
doubt people use this "feature" very often.

I work with IPs all the time in lua, and they are more usefully
represented as hex. If you want to find the broadcast address for ip
address 10.1.96.8/22 you rewrite it in hex, "\x0a\x01\x60\x08", now
its obvious.

Anyhow, binary escape sequences in lua are for representing binary
data, and that's what feels cumbersome, even for a number of us who
have worked hard to absorb the lua way.

Cheers,
Sam