3

I'm trying to find the hexadecimal non-printable character 00h within a string with Lua. I tried it with an escape character and as a result I get the same location I start in (that's a printable character). I fiddled around with the character classes, but that didn't amount to anything. My approach looks like this:

location = string.find(variable,"\00",startlocation)

I also tried it this way, but no luck:

location = string.find(variable, string.char(00),startlocation)

How can I find this non-printable pattern in Lua?

Zerobinary99
  • 542
  • 2
  • 8
  • 17
  • Did you try `location = string.find(variable,"\0",startlocation)` with a single zero? – Sergey Kalinichenko Jan 27 '12 at 10:56
  • One, two or three zeros is the same thing. – lhf Jan 27 '12 at 11:01
  • 3
    shouldn't you use `%z` for zero characters in patterns (at least in 5.1) ? – jpjacobs Jan 27 '12 at 11:10
  • Yes, I did that, too. I found my mistake. "variable" is filled 00h. I thought I started at a printable character, but in fact I didn't. That's why "location" ended up being the same value as "startlocation", leading me to believe I somehow didn't use the correct representation of 00h in the string.find-command. Thanks to lhf for actually leading me to the right conclusion. – Zerobinary99 Jan 27 '12 at 11:12

1 Answers1

2

It works fine for me:

> return string.find("one\0two\0,three","\0,")
8   9
> return string.find("one\0two\0,three","\0")
4   4
> return string.find("one\0two\0,three","\00")
4   4
> return string.find("one\0two\0,three","\00",6)
8   8
lhf
  • 70,581
  • 9
  • 108
  • 149
  • Hmm, must be an error in my code then. I need to check the other variables. – Zerobinary99 Jan 27 '12 at 10:59
  • @Zerobinary99, `string.char(00)` returns a string of length 1 whose only byte is 0 and this is exactly what `"\0"` is. Try `print(string.char(00)=="\0")`. – lhf Jan 27 '12 at 11:03
  • I just tested that out myself ;) Hence the edit of my initial message. Thought I could correct my false assumption before you saw it, but you were faster ;) – Zerobinary99 Jan 27 '12 at 11:05
  • 2
    Note that \0 isn't valid in a pattern in lua 5.1; use %z. – daurnimator Jan 31 '12 at 04:59