-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
  • 8

3 Answers3

3

You could use a modification of the following answers:

Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
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
  • 85,011
  • 4
  • 57
  • 81
  • 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
  • 301,070
  • 26
  • 186
  • 335