M
Morris Keesan
s/3/4/ (assuming you want to output four bytes)
s/intvalue/ltemp/
Oops. Correct on both counts. That's what I get for hitting "send"
without proofreading.
s/3/4/ (assuming you want to output four bytes)
s/intvalue/ltemp/
Not quite. x should be unsigned long. If x is negative, then/*
write a 32 bit big endian integer to file
Params: x - the integer
fp - the open file
Returns: 0 on success, error code on fail
*/
int fput32bigendian(long x, FILE *fp)
{
for( i = 3; i >= 0; i-- ) bytes = what % 256, what /= 256;
What ??? Did you do this with a comma operator just to make it hard
to read? If so, it worked. I started to post this as a correction,
before I did a double-take and realized that the assignment has
higher priority than the comma.
It might zero-pad or sign extend the long, but that's OK because weNot quite. x should be unsigned long. If x is negative, then
the results of the right shifts are implementation-defined, and
(perhaps surprisingly) there are no constraints on what the implementation
may define as the result.
Bl0ckeduser said:Hi, I'm not a C expert, but the following worked for me on a
low-endian 32-bit machine, a low-endian 64-bit machine, and a
big-endian 32-bit machine:
int i = 12345678;
printf("%d\n", (i & 0xFF000000) >> 32);
printf("%d\n", (i & 0x00FF0000) >> 16);
printf("%d\n", (i & 0x0000FF00) >> 8);
printf("%d\n", i & 0x000000FF);
output (identical on all three machines):
0
188
97
78
Joe said:Except that he wants to write four bytes, not four strings.
[...]Stephen Sprunk said:MAX_INT is only guaranteed to be 32767;
and is spelled INT_MAX.
What's wrong with using htonl()? Let your implementation worry about
the endianness of your platform. Use ntohl() to convert it when
reading the data back from the file.
Yes, those little endians tried to take over the world, but they'veIn any event, just giving him such functions would mean he misses out on
the opportunity to learn something very important, which he will likely
need to know when he later runs into file formats or network protocols
that, for historical reasons, require numbers to be in little-endian
order rather than the standard order.
Morris Keesan said:for( i = 3; i >= 0; i-- ) bytes = what % 256, what /= 256;
What ??? Did you do this with a comma operator just to make it hard
to read?
If so, it worked. I started to post this as a correction,
before I did a double-take and realized that the assignment has
higher priority than the comma.
'fread' reads an array of bytes ['char *']
'fwrite' writes an array of bytes ['char *']
passing an 'int *' or 'struct foo *' [with a cast to defeat the type
system] is at best unportable and at worst really broken
'fread' reads an array of bytes ['char *']
'fwrite' writes an array of bytes ['char *']
passing an 'int *' or 'struct foo *' [with a cast to defeat the type
system] is at best unportable and at worst really broken
It might zero-pad or sign extend the long, but that's OK because we
ignore those bits. It wil break if it defines a right shift of a
negative number as some sort of runtime error, but it will also fail
on one's complement systems, or where the filing system uses 16-bit
binary bytes.
Morris Keesan said:On Sat, 10 Mar 2012 14:44:13 -0500, Tim Rentsch
for( i = 3; i >= 0; i-- ) bytes = what% 256, what /= 256;
What ??? Did you do this with a comma operator just to make it hard
to read?
No, I used a comma operator because I think of the two assignments
as a single conceptual unit (extract the low order bits and shift
those bits out of the word being worked on), so it seems natural
to express that conceptual unit as a single statement.
Are you replying to something?
Morris Keesan said:@alumni.caltech.edu> wrote:for( i = 3; i>= 0; i-- ) bytes = what % 256, what /= 256;
What ??? Did you do this with a comma operator just to make it hard
to read?
No, I used a comma operator because I think of the two assignments
as a single conceptual unit (extract the low order bits and shift
those bits out of the word being worked on), so it seems natural
to express that conceptual unit as a single statement.
If you worked under me I'd make you re-write that code properly.
"Clever" people tend to write code that holds up less under the test
of time.
Tom
Are you replying to something?
The subject line starts with "Re: ".
On my machine, you press left-arrow key.
In this here case, the subject line is fine, and the text is just a
recapitulation, explaining that he can't solve the problem [which is
implied, otherwise why the article?].
Not all Usenet clients make it easy to see the parent article.
I can fully understand why you do not like clever people.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.