简体   繁体   English

将字节中的数据与十六进制数进行比较

[英]Compare data in bytes to a hex number

I need to read data from stdin where I need to check if 2 bytes are equal to 0x001 or 0x002 and then depending on the value execute one of two types of code.我需要从标准输入读取数据,我需要检查 2 个字节是否等于 0x001 或 0x002,然后根据值执行两种类型的代码之一。 Here is what i tried:这是我尝试过的:

uint16_t type;
uint16_t type1 = 0x0001;
while( ( read(0, &type, sizeof(type)) ) > 0 ) {
  if (type == type1) {
   //do smth
  }
}

I'm not sure what numeric system the read uses since printing the value of type both as decimal and as hex returns something completely different even when i wrtie 0001 on stdin我不确定读取使用什么数字系统,因为将类型的值打印为十进制和十六进制都会返回完全不同的东西,即使我在标准输入上写入 0001

The characters 0x0001 and 0x0002 are control characters in UTF-8, you wont be able to type them manually.字符 0x0001 和 0x0002 是 UTF-8 中的控制字符,您将无法手动键入它们。

If you are trying to compare the character input - use gets and atoi to convert the character-based input of stdin into an integer, or if you want to continue using read - compare against the characters '0' '0' '0' '1'/'2' instead of the values to get proper results如果您尝试比较字符输入 - 使用getsatoi将基于字符的stdin转换为 integer,或者如果您想继续使用 read - 比较字符'0' '0' '0' '1'/'2'而不是得到正确结果的值

You didn't say what kind of OS you're using.您没有说您使用的是哪种操作系统。 If you're using Unix or Linux or MacOS, at the command line, it can be pretty easy to rig up simple ways of testing this sort of program.如果您在命令行使用 Unix 或 Linux 或 MacOS,则可以很容易地使用简单的方法来测试此类程序。 If you're using Windows, though, I'm sorry, my answer here is not going to be very useful.但是,如果您使用的是 Windows,很抱歉,我在这里的回答不会很有用。

(I'm also going to be making heavy use of the Unix/Linux "pipe" concept, that is, the vertical bar | which sends the output of one program to the input of the next. If you're not familiar with this concept, my answer may not make much sense at first.) (我还将大量使用 Unix/Linux 的“管道”概念,即竖线|它将一个程序的 output 发送到下一个程序的输入。如果你不熟悉这个概念,我的回答一开始可能没有多大意义。)

If it were me, after compiling your program to a.out , I would just run如果是我,在将你的程序编译为a.out之后,我会运行

echo 00 00  00 01  00 02 | unhex | a.out

In fact, I did compile your program to a.out , and tried this, and... it didn't work.实际上,我确实将您的程序编译为a.out ,并尝试了这个,然后......它没有用。 It didn't work because my machine (like yours) uses little-endian byte order, so what I should have done was它不起作用,因为我的机器(就像你的机器一样)使用 little-endian 字节顺序,所以我应该做的是

echo 00 00  01 00  02 00 | unhex | a.out

Now it works perfectly.现在它完美地工作了。

The only problem, as you've discovered if you tried it, is that unhex is not a standard program.唯一的问题是,如果您尝试过,就会发现unhex不是标准程序。 It's one of my own little utilities in my personal bin directory.它是我个人 bin 目录中的一个小实用程序。 It's massively handy, and it's be super useful if something like it were standard, but it's not.它非常方便,如果类似的东西是标准的,它会非常有用,但事实并非如此。 (There's probably a semistandard Linux utility for doing this sort of thing, but I don't know what it is.) (可能有一个半标准的 Linux 实用程序来做这种事情,但我不知道它是什么。)

What is pretty standard, and you probably can use it, is a base-64 decoder.一个非常标准的并且您可能可以使用它的是 base-64 解码器。 The problem there is that there's no good way to hand-generate the input you need.那里的问题是没有很好的方法来手动生成您需要的输入。 But I can generate it for you (using my unhexer).但我可以为你生成它(使用我的 unhexer)。 If you can run如果你能跑

echo AAABAAIA | base64 -d | a.out

that will perform an equivalent test of your program.这将对您的程序执行等效测试。 AAABAAIA is the base-64 encoding of the three 16-bit little-endian words 0000 0001 and 0002, as you can see by running AAABAAIA是三个 16 位 little-endian 字 0000 0001 和 0002 的 base-64 编码,您可以通过运行看到

echo AAABAAIA | base64 -d | od -h

On some systems, the base64 command uses a -D option, rather than -d , to request decoding.在某些系统上, base64命令使用-D选项而不是-d来请求解码。 That is, if you get an error message like "invalid option -- d", you can try也就是说,如果你收到类似“invalid option --d”这样的错误信息,你可以试试

echo AAABAAIA | base64 -D | a.out

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM