0

The following code gives me different outputs on different systems:

int fd = open(filename, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
byte_t data[] = { 0x00, 0x01, 0x02, 0x03, 0x0a, 0x0b 0x0c, 0x0d };

write(fd, data, sizeof(data));

On the mac a 'hexdump' of the filename gives what I would expect

00 01 02 03 0a 0b 0c 0d

On Ubuntu and on Windows ( the code is different but the effect is the same ) you get

01 00 03 02 0b 0a 0d 0c

I would like platforms to print

00 01 02 03 0a 0b 0c 0d

So how should I proceed?

user1660675
  • 149
  • 3
  • 10
  • is "byte_t" a consistent data type across the two platforms? – Shamim Hafiz - MSFT Aug 22 '13 at 19:23
  • 2
    "the code is different"? Please show us the *actual* code, not some approximation. – NPE Aug 22 '13 at 19:25
  • 3
    I think it's highly unlikely that the code you show would behave in the manner you describe on *any* platform (regardless of endianness etc). Please include an SSCCE (http://sscce.org/). – NPE Aug 22 '13 at 19:27
  • Looks like ENDIANNESS issue. Although not quite typical. Have a look at these [IMB write endian independent code](http://www.ibm.com/developerworks/aix/library/au-endianc/) and [some random doc I found on google](http://www.slashdocs.com/uquyk/writing-endian-independent-code-in-c.html) – David Tsulaia Aug 22 '13 at 19:38
  • Thank you for your help guys, I ended up learning a bit more about possible endianness issues than I would have otherwise. – user1660675 Aug 22 '13 at 19:47
  • @ShamimHafiz byte_t is consistent across all platforms I've used, I could have written unsigned char but I wanted to communicate that I was just passing in bytes – user1660675 Aug 22 '13 at 19:49
  • @JonathanLeffler Intel Mac, it's a mac mini and my code didn't stark getting weird until I tried to use other platforms – user1660675 Aug 22 '13 at 19:49
  • Thank you for the links NPE and David – user1660675 Aug 22 '13 at 19:50

1 Answers1

1

It looks like the hexdump utility swaps the bytes. Please show the hexdump command you used in each platform.

To confirm it, test with

byte_t data[] = { 'a', 'b', 'c', 'd', 'e' }

and compare the hexdump and cat(or type) outputs.

jaeheung
  • 1,208
  • 6
  • 7