2012-01-27 115 views
2

我试图在Lua中找到字符串中的十六进制非可打印字符00h。我用转义字符试了一下,结果我得到了相同的位置(这是一个可打印的字符)。我摆弄了角色类,但这不算什么。我的方法是这样的:Lua:在字符串中查找十六进制值

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

我也尝试过这种方式,但没有运气:

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

我如何才能找到在Lua这个非打印模式?

+0

你尝试'位置= string.find(变量, “\ 0”,startlocation)'用一个零? – dasblinkenlight 2012-01-27 10:56:42

+0

一个,两个或三个零是相同的东西。 – lhf 2012-01-27 11:01:12

+3

不应该为模式中的零个字符使用'%z'(至少在5.1中)? – jpjacobs 2012-01-27 11:10:16

回答

2

它正常工作对我来说:

> 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 
+0

嗯,那么在我的代码中必须是一个错误。我需要检查其他变量。 – Zerobinary99 2012-01-27 10:59:40

+0

@ Zerobinary99,'string.char(00)'返回一个长度为1的字符串,其唯一的字节是0,这正是'“\ 0”'的意思。尝试'print(string.char(00)==“\ 0”)'。 – lhf 2012-01-27 11:03:17

+0

我刚刚测试过我自己;)因此编辑我的初始消息。以为我可以在你看到它之前纠正我的错误假设,但是你更快;) – Zerobinary99 2012-01-27 11:05:06