-
September 4th, 2012, 09:17 PM
#1
Streaming Bytes into an Integar
Hello,
I have a FILE stream, and I want to create a function that streams a specified number of bytes (up to four bytes) and returns the value of this, as an integar.
For example, supposing that my FILE has the following data, in hex: 74 C8 02 33 F2 7B....... I then want to stream three bytes and store this as an integar. So, I want to stream "04 08 02". The data stored in the integar should then be "00 74 C8 02", because I have not streamed anything into the first byte. By converting the hex to dec, the integar should then be of the value 7653378 (if it is unsigned).
To try to achieve this, I have written the following function. I create an integar and initialise it to zero, then take each byte from the stream, and OR it with my integar. Then, I shift the integar left by 8, and read the next byte, and so on.
The problem is, when I convert "c" to "c_int", it adds on a load of 1's to the left of the "c" data. This then means that the OR comparison changes all those bits in my integar to 1.
Any ideas on how to solve this? I am also wondering whether there is a much more simple way of doing this, rather than having to write my own function....
Thanks!
Code:
int StreamFileToInt(FILE *fp, int num_bytes)
{
char c;
int c_int;
int x = 0x0000;
for (int i = 0; i < num_bytes; i++)
{
c = getc(fp);
c_int = (int)c;
x = x | c_int;
x << 8;
}
return x;
}
-
September 4th, 2012, 09:46 PM
#2
Re: Streaming Bytes into an Integar
Originally Posted by ejohns85
The problem is, when I convert "c" to "c_int", it adds on a load of 1's to the left of the "c" data. This then means that the OR comparison changes all those bits in my integar to 1.
getc already returns an int, and this int might be EOF, which is a negative value. I suspect that you have a read error, or the file is shorter than you expected. By the way, is there any reason why you use getc instead of fgetc?
-
September 5th, 2012, 06:51 AM
#3
Re: Streaming Bytes into an Integar
Yes, getc() is converted to an int automatically, but I want to be able to stream three bytes, and concatenate them into a single integar. In this way, the first byte in the integar is zeros, and the second, third and fourth are determined by the values of the three bytes I have just streamed.
So for example, if I stream three chars that have values 0xFF, 0xAA and 0x33, I want to create an integar whose value is 0x00FFAA33.
And I don't know why I am using getc and not fgetc - don't really know the difference!
Thanks!
-
September 5th, 2012, 10:14 AM
#4
Re: Streaming Bytes into an Integar
Originally Posted by ejohns85
And I don't know why I am using getc and not fgetc - don't really know the difference!
From what I see:
Originally Posted by C99 Clause 7.19.7.5 Paragraph 2
The getc function is equivalent to fgetc, except that if it is implemented as a macro, it may evaluate stream more than once, so the argument should never be an expression with side effects.
As such, it seems to me that you might as well use fgetc unless you have special reasons (efficiency?) to use getc.
Originally Posted by ejohns85
Yes, getc() is converted to an int automatically, but I want to be able to stream three bytes, and concatenate them into a single integar.
Ah, but here's the thing:
Originally Posted by C99 Clause 7.19.7.1 Paragraph 2
If the end-of-file indicator for the input stream pointed to by stream is not set and a next character is present, the fgetc function obtains that character as an unsigned char converted to an int and advances the associated file position indicator for the stream (if defined).
So, the value read is an unsigned char, converted to int. Then you convert the int to char (which may be signed or unsigned), which you then cast to int.
The way I see it, you should be doing something like this instead:
Code:
int StreamFileToInt(FILE *fp, int num_bytes)
{
int x = 0x0000;
int c;
for (int i = 0; i < num_bytes && (c = fgetc(fp)) != EOF; i++)
{
x = (x << 8) | c;
}
return x;
}
-
September 5th, 2012, 10:16 AM
#5
Re: Streaming Bytes into an Integar
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|