Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading hexadecimal values

Status
Not open for further replies.

KVL

Programmer
Joined
May 6, 2002
Messages
3
Location
BE
Hello,

does anyone knows how to assign hex values to the variables Op en Ki
when reading them out of a file that looks e.g. like this:
Op 63 BF A5 0E E6 52 33
65 FF 14 C1 F4 5F 88
73 7D
Ki DB C2 46 41 2B 8C FA
30 7F 70 F0 A7 54 86
32 95
I found a function sscanf so maybe I can scan each line and convert the
values afterwards but what should I do with the text "OP" and "Ki".


Thanx for your help.
 
You are pretty close. Do something like this:

int hex;
char *hex_string = "BF"
sscanf(hex_string,"%x",&hex);

Of course, you probably want to do something a little more fancy and put stuff in arrays but that is just details. If you're reading from a file look at the function fscanf().

-bitwise
 
i think this will also work fine

ifstream infile("input.txt");

int data;
infile>>hex>>data;


second option is

char* endPtr;
char buffer[2];
infile.getline(buffer,2);
data = strtol(buffer,endPtr,16);

Matt
 
Sorry, as for KI and OP the buffer method above would work with a strcmp. I forgot to make the buffer size 3 however and set the last character to Null

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top