• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question on C

Yohhan

Senior member
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.
 
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.
 
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. 😕
 
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. 😕
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.
 
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🙂
 
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.
 
Back
Top