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.

Recommended Answers

All 3 Replies

>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;

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.