2012-01-27 22 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

相關問題