-2

Hey guys I am working with a software that needs to check for a char byte lets say like this:

char id = 0xa2;

As we all know that is a char byte in hex, what I need is a way to read from a txt file that same hex number, and compare it with id. As you may notice when you read from a file what you have is a char pointer containing the string let's say 0xa2 I need a way that I could compare the two values to check if it is the same ID obviously I know that one of the two values have to be converted to match the other, I just dont know how to do it and I have tried several methods

user2948982
  • 61
  • 1
  • 2
  • 7

3 Answers3

3

You could use a modification of the following answers:

Community
  • 1
  • 1
Igor Ševo
  • 5,339
  • 3
  • 31
  • 73
0

Simplest is to convert the char to a string. E,g,

char idStr[5];
sprintf(idStr, "0x%02x", (unsigned char)id);

The compare idStr with the string read from the file.

Case might be an issue, obviously 0xA2 and 0xa2 should compare equal.

john
  • 72,145
  • 4
  • 50
  • 71
  • thanks @jhon you gave a nice idea I did it that way then I compare the two strings with a little function I created and pum working fine good stuff – user2948982 Nov 29 '13 at 15:11
0

I can suggest the following solution. Instead of a file stream I am using a string stream.

    std::istringstream is( "0xa2" );

    char id = '\xa2';

    int x;

    is >> std::hex >> x;

    if ( id == static_cast<char>( x ) ) std::cout << "They are equal each other" << std::endl;
Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303