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?

4
  • Did you try location = string.find(variable,"\0",startlocation) with a single zero? Commented Jan 27, 2012 at 10:56
  • One, two or three zeros is the same thing. Commented Jan 27, 2012 at 11:01
  • 3
    shouldn't you use %z for zero characters in patterns (at least in 5.1) ? Commented Jan 27, 2012 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. Commented Jan 27, 2012 at 11:12

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, must be an error in my code then. I need to check the other variables.
@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").
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 ;)
Note that \0 isn't valid in a pattern in lua 5.1; use %z.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.