简体   繁体   中英

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. 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

The characters 0x0001 and 0x0002 are control characters in UTF-8, you wont be able to type them manually.

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

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. If you're using Windows, though, I'm sorry, my answer here is not going to be very useful.

(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.)

If it were me, after compiling your program to a.out , I would just run

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. It didn't work because my machine (like yours) uses little-endian byte order, so what I should have done was

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. It's one of my own little utilities in my personal bin directory. 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.)

What is pretty standard, and you probably can use it, is a base-64 decoder. 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). 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

echo AAABAAIA | base64 -d | od -h

On some systems, the base64 command uses a -D option, rather than -d , to request decoding. That is, if you get an error message like "invalid option -- d", you can try

echo AAABAAIA | base64 -D | a.out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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