tags:

views:

53

answers:

4

I'm trying to read hexa data(color value ex. 0xffffffff) from txt file...

but i don't know how to read it....

i declared the color value like 'uint color' and i want to change the value though txt file.

if i use int data i can use 'atoi' function, but what can i use function for uint?

+2  A: 

You can use strtoul

strtoul actually returns a long, so you can do one of two things:

  1. Just truncate the data
  2. Check if it fits in a unit

example usage:

char *endptr;
unsigned long ul = strtoul(str, &endptr, 16);
if (str == endptr)
    // error, no data was converted

// just truncate
unsigned int utrunc = (unsigned int)ul;   

// or you can first check if it fits
if (ul < UINT_MAX)
    unsigned int ufit = (unsigned int)ul;  
R Samuel Klatchko
+1  A: 

You can directly read the hex number from the file as:

unsigned int n;
fscanf(fd,"%x",&n);
codaddict
A: 

If you don't like the nice automated methods my predecessors posted, you can always do this manually...

 const char _hex[] =    
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\x00\x00\x00\x00\x00"
                "\x00\x0A\x0B\x0C\x0D\x0E\x0F\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x0A\x0B\x0C\x0D\x0E\x0F\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";

 int color;
 char color_txt[6]="DD1173";

 color = _hex[color_txt[5]]
       + _hex[color_txt[4]]<<4
       + _hex[color_txt[3]]<<8
       + _hex[color_txt[2]]<<12
       + _hex[color_txt[1]]<<16
       + _hex[color_txt[0]]<<20;

There are of course more elegant methods - use conditionals in a function to convert hex char to int, loop over the values and so on.

SF.
Your solution implies a given Endianess. Putting the values as text in a data file removes Endianess from the problem set.
Thomas Matthews
@Thomas: replace shifts with multiplication (*0x10, *0x100, *0x1000) and this becomes endian-agnostic. Except the ascii text hex endianness, but written numbers-as-text are -always- low-endian.
SF.
+1  A: 

In C++, you can use the hex manipulator with a std::istream:

unsigned int Read_Value(std::istream& input)
{
    unsigned int value;
    input >> hex >> value;
    return value;
}
Thomas Matthews
+1 for the C++ way ...
neuro