How to show base for 8 bit hex number

I need to compare 8 bit hexadecimal numbers in a text file with those i've created. Problem is that the ones in the text file are 8 bit with the leading 0's included. E.g the number 255 is represented as: 0x000000FF while my program is outputting the same number as: 0xFF. How do I add those leading zeros to my hexadecimal numbers? Is there a way of doing this?

I am storing them both as strings and need to compare them and recognize when they are equal. i.e I need the program to recognize that 0x000000FF=0xFF but when they're held as strings it wont see them as equal, correct (I haven't tried it)?

The reason they're being held as strings is that I'm using the getline member of a fstream object and I don't think there's an equivalent "getnumber" function, right?

Any help much appreciated

closed account (o3hC5Di1)
I'm assuming that this thread:

http://cplusplus.com/forum/beginner/72076/

Is the same question?

All the best,
NwN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <sstream>
#include <string>

unsigned int hex_string_to_uint( const std::string& hex_string )
{
    std::istringstream stm(hex_string) ;
    unsigned int value ;
    if( stm >> std::hex >> value && stm.eof() ) return value ;
    else throw "error" ;
}

bool equal_hex_strings( const std::string& a, const std::string& b )
{
    try { return hex_string_to_uint(a) == hex_string_to_uint(b) ; }
    catch( const char* ) { return false ; }
}
Topic archived. No new replies allowed.