0

I want to store a byte from fread as type char, and then be able to perform comparison operations on that byte as an int. I have

int num;
char buffer;
num = fread(buffer, 1, 1, myFP);

and I'm sure it's not right.

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by John A
0

>and I'm sure it's not right.
Nope. fread() returns the number of successfully-read elements. You need to convert the char from buffer into an integer. If you'll take a look at an ASCII table, you'll see that characters are just decimals. You'll also see that numbers are stored as decimals, too.

So... you'll notice that '0' starts at 48. Just subtract 48:

num = buffer - 48;
0

If you do just simple equals and smth like that just do

char c;
int i;
i = int(c);

Nope, that won't work. It just casts the ASCII value contained in 'c' to 'i'. You're still going to have to subtract to get the actual integer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.