Question on C

Yohhan

Senior member
May 17, 2002
263
0
0
I'm trying to grab hex characters from a file, and store them in arrays, so I can do some bit manipulation. However, the function I'm using to get the character from file, fgetc() takes in a character coded in ASCII. Is there something like a fgethex() function so I can store something like: "3FFA" in hex as opposed to ASCII? Thanks ahead.
 

onelin

Senior member
Dec 11, 2001
874
0
0
I don't think there is a built in function, but it should be easy enough to make one yourself. I know I've done ASCII-> hex before and it only took a few minutes.

I'm not entirely sure what you mean though, you want to read hex characters "3FFA" and somehow make it "3FFA" instead of "3FFA" (ascii equiv????)

but making 'a' turn into "61" (hex), that I would understand.

Please clarify.
 

kleinesarschloch

Senior member
Jan 18, 2003
529
0
0
i think the original poster has a poor idea of what hexadecimal really is. it's just a notation for representing numbers. fgetc() just reads from the file and those values can be represented in hex or binary or decimal and it would still be the same value. :confused:
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
cant you cast it as an unsigned byte or something? (I'm not good at C, but it seems like that should work)
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
Originally posted by: kleinesarschloch
i think the original poster has a poor idea of what hexadecimal really is. it's just a notation for representing numbers. fgetc() just reads from the file and those values can be represented in hex or binary or decimal and it would still be the same value. :confused:
I think he has an ASCII file, but each of the characters represent a hex number. So, the file will have the string "3FFA" but he wants to store that in an array as {3,15,15,10}. That or he wants to read in the string "3FFA" and store the value of 16378.
 

Yohhan

Senior member
May 17, 2002
263
0
0
Sorry, I'll try to clarify. I'm new to C.

I have a file with hexadecimal values, such as "3FFA" in it. I need to read these values one byte at a time, using my C program. I'm using fgetc() to do so.

So, I'm assuming fgetc() grabs the first byte. Now here's where I start to run into trouble. Is 3F considered the first byte, as it would be hexadecimal, or is 3 considered the first byte as it would be in ASCII?

If 3F is considered the first byte, is it just stored as 45 in binary? If this is the case, what if I have a random characters such as: "WXYZ"? How are they stored?

If 3 is considered the first byte, is it stored as 33 (ASCII code for 3 I believe) in binary? I'm assuming when fgetc() reads in a character from file, it reads it in as its ASCII code. But if I want A to represent a binary 10, then it's a problem if A is actually representing 95, or whatever its ASCII value is.

Hope that made more sense than my original post. Thanks for the help, I need it:)
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
Originally posted by: Yohhan
Sorry, I'll try to clarify. I'm new to C.

I have a file with hexadecimal values, such as "3FFA" in it. I need to read these values one byte at a time, using my C program. I'm using fgetc() to do so.

So, I'm assuming fgetc() grabs the first byte. Now here's where I start to run into trouble. Is 3F considered the first byte, as it would be hexadecimal, or is 3 considered the first byte as it would be in ASCII?

It depends on what your input file is. If you can open the file in notepad and see 3FFA then you're looking at ASCII, in which case fgetc() would return the ASCII value of '3.' If you have to open the input file in a hex editor to see 3FFA, then fgetc() will return 0x3F.

If 3F is considered the first byte, is it just stored as 45 in binary? If this is the case, what if I have a random characters such as: "WXYZ"? How are they stored?

"WXYZ" are ASCII characters. They have the hex values 0x57, 0x58, 0x59, 0x60. fgetc() does not care what the characters corresponding to hex values 0x57, 0x58, 0x59, 0x60 are. In fact, they do not have to be characters at all. The ASCII system is there only for human convenience.

If 3 is considered the first byte, is it stored as 33 (ASCII code for 3 I believe) in binary? I'm assuming when fgetc() reads in a character from file, it reads it in as its ASCII code. But if I want A to represent a binary 10, then it's a problem if A is actually representing 95, or whatever its ASCII value is.

33 is not a binary number, but yes, fgetc() would return 0x33 if it reads in the character '3.'

Judging from your questions, you have a bunch of hex values in text (ASCII) form. You can look into fscanf() as I mentioned before to automatically convert text like "AF" into an integer. Or you can have a few if-statements to handle the 0-9 and A-F characters one at a time.

Edit: To use fscanf() though, you need to have spaces between the hex numbers.