使用空值搜索字符串缓冲区(chr(0)) [英] Search a string buffer with nulls (chr(0))

查看:85
本文介绍了使用空值搜索字符串缓冲区(chr(0))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在缓冲区中搜索一串字符。

缓冲区本质上是一个二进制文件(它是一个图像)并包含许多chr(0)字符。 />
我需要找到的字符串也包含chr(0)字符。实际上它是以下字符串:

chr(100)& chr(0)& chr(0)& chr(163)


这是我需要处理的项目的图像文件中的标记。

所以我会做以下事情:

I need to search a buffer for a string of characters.
The buffer is essentially a binary file (it''s an image) and contains lots of chr(0) characters.
The string I need to locate also contains chr(0) characters. In fact it is the following string:
chr(100) & chr(0) & chr(0) & chr(163)

This is a marker in the image file for items I need to process.
So I though I would do the following:

展开 | 选择 | Wrap | 行号

推荐答案

我不能和VB6说这样做,但是一个字符串定义是用chr(0)终止的unicode
http://msdn.microsoft.com/en-us/libr...em.string.aspx
http://msdn.microsoft.com/en-us/libr...67(VS.85).aspx


所以你有两个问题:

1)当你创建字符串时,你的单个字节被转换成双字节的unicode字符,所以你的字符串不再正确表示你的字节。

2)第一个字符(0)是字符串的终止
I can''t speak to VB6 doing this, but a string by definition is unicode terminated with a chr(0)
http://msdn.microsoft.com/en-us/libr...em.string.aspx
http://msdn.microsoft.com/en-us/libr...67(VS.85).aspx

So you have two issues:
1) your single bytes are being turned into two-byte unicode characters when the string is made, so your string no longer properly represents your bytes.
2) The first char(0) is the termination of the string


好的 - 我现在将回答我自己的问题 - 也许有人会说 - 不 - 不是这样的假!

看来VB.NET存储字符串的方式与VB6的方式(DUH!)截然不同。所以使用file.readalltext将文件读入字符串变量,如下所示:

Dim MyFileData as String = System.IO.File.FeadAllText(" mybinaryfile.bin")


准确地对数据进行编码,但不同于将字符分配给字符串,如:

Dim MySearchString as String = Chr(100)& Chr(0)& Chr(0)& Chr(163)


所以你真的永远不会找到它:

Dim iLocation as Integer = MyFileData.IndexOf(MySearchString )


这是因为MyFileData和MySearchString中的数据没有以相同的方式编码到它们各自的字符串变量中。


相反,你必须做的是确保两个字符串被相同地编码到字符串变量中。这样做:

Ok - I will now answer my own question - and maybe someone will say - No - not that way dummy!
It appears that the way VB.NET stores strings is very different from the way VB6 did (DUH!). So reading a file into a string variable using file.readalltext like this:

Dim MyFileData as String = System.IO.File.FeadAllText("mybinaryfile.bin")

encodes the data accurately, but differently than assigning characters to a string like:

Dim MySearchString as String = Chr(100) & Chr(0) & Chr(0) & Chr(163)

So you really will never find it using:

Dim iLocation as Integer = MyFileData.IndexOf(MySearchString)

This is because the data in MyFileData and MySearchString is not encoded the same way into their respective string variables.

Instead what you must do is make sure that both strings are encoded into the string variable identically. Do it this way:

展开 | 选择 | Wrap | 行号


@tlhintoq


好​​笑 - 我在发布解决方案之后看到了你的回复 QA TLHO''

我们显然在同一页面上 - 但是看起来VB.NET字符串可以包含chr(0)字符并且仍在使用(包括那些烦人的小空值)只要你知道它是如何编码的。我不完全确定VB.NET对ReadAllText方法做了什么,因为我必须显式调用ReadAllText(mybinaryfile.bin, Encoding.Default )。显然,Encoding.Default 不是 ReadAllText使用的默认编码,因为如果我无法使用Encoding.Default参数,我的比较将失败 - 即使我String.Normalize结果。

最后我认为这里有很多变数,我可能只是想出一个相对肤浅的解决方案。我的目标是保持代码简单和干净 - 避免在整个地方制作字符串副本。

我想,如果我有一个更长的字符串来搜索我将不得不更加关注如何String.Normalize方法适用于它 - 虽然我不在这里。


Des
@tlhintoq
Funny - I saw your response after I posted my solution. QA TLHO''
We are clearly on the same page - however it does appear that VB.NET strings can contain chr(0) characters and still be used (including those annoying little nulls) provided you are aware of how it is encoded. I''m not entirely sure exactly what VB.NET does with the ReadAllText method because I have to explicitly call ReadAllText("mybinaryfile.bin", Encoding.Default). Apparently Encoding.Default is not the default encoding used by ReadAllText, because if I fail to use the Encoding.Default parameter my comparisons will fail - even though I String.Normalize the result.
In the end I think there are a lot of variables at play here and I''ve likely just come up with a relatively superficial solution. My goal was to keep the code simple and clean - avoiding making string copies all over the place.
I think, if I had a longer string to search for I would have to pay closer attention to how the String.Normalize method worked on it - I''m outa my league here though.

Des


这篇关于使用空值搜索字符串缓冲区(chr(0))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆