Professional Web Applications Themes

endian question - UNIX Programming

I run an intel i686 processor. I just wanted to know weather it mattered because of endian how I put data in a file. If I open a FILE *, say conf, and then I fwrite() to it, will I mave to make any conversions if I want to fread() from it? example by c code: //assume conf is opened for writing and is a new file int j = 12793; fwrite(&j, sizeof(int), 1, conf); //conf is same file, opened for reading and at the start int j fread(&j, sizeof(int), 1, conf); printf("%d", j); //should output 12793 It is my ...

  1. #1

    Default endian question

    I run an intel i686 processor.

    I just wanted to know weather it mattered because of endian how I put data
    in a file. If I open a FILE *, say conf, and then I fwrite() to it, will I
    mave to make any conversions if I want to fread() from it?

    example by c code:
    //assume conf is opened for writing and is a new file
    int j = 12793;
    fwrite(&j, sizeof(int), 1, conf);

    //conf is same file, opened for reading and at the start
    int j
    fread(&j, sizeof(int), 1, conf);
    printf("%d", j); //should output 12793

    It is my understanding that I should be able to do this without
    a problem, but because I have little endian, the actual numbers will be
    stored when read in a hex editor as somthing else. (ie 12793 = 0x0000n31F9
    will be stored as F9310000) Will this affect the program if I stick to
    fread()ing and fwrite()ing?

    Thanks
    --
    -J. Leddy (a.k.a. iustitia)
    James Leddy Guest

  2. #2

    Default Re: endian question

    On Wed, 16 Jul 2003 02:32:49 GMT, James Leddy <iustitiaoptonline.net>
    wrote:
    > I run an intel i686 processor.
    >
    > I just wanted to know weather it mattered because of endian how I put
    > data
    > in a file.
    [snip]

    It matters for the same reasons and under the same cirstances as it
    matters when you're passing data over a network. Namely:

    1. Never for characters, but always for binary data that's more than a
    byte.
    2. Never when you're internal to a machine, but possibly (which means you
    have to account for it) when going between machines.

    So... feel free to write binary data however you like if the binary file
    stays within a machine, or another machine with the same hardware. But,
    take endian into account if you're writing a binary file that's supposed to
    be portable.

    For this reason, binary interchange formats like, say, TIFF, record the
    endian state in the file, and the reading program must take it into
    account.

    Or... use a 100% character based format, like, say, XML, and the problem
    goes away.

    --Marc
    Marc Rochkind Guest

  3. #3

    Default Re: endian question

    >>> Marc Rochkind wrote:

    MR> It matters for the same reasons and under the same cirstances as it
    MR> matters when you're passing data over a network. Namely:
    [...]
    MR> 2. Never when you're internal to a machine, but possibly (which means you
    MR> have to account for it) when going between machines.

    There are little exceptions for some unusual;) architectures as ia64 and
    power[pc]: endianness can be switched on them, and different OSes can use
    different endianness.

    MR> 1. Never for characters, but always for binary data that's more than a
    MR> byte.

    UTF-16 have two different endiannesses.

    [...]
    MR> Or... use a 100% character based format, like, say, XML, and the problem
    MR> goes away.

    XML with UTF-8, of course.


    -netch-
    Valentin Nechayev Guest

  4. #4

    Default Re: endian question

    Valentin Nechayev wrote:
    > UTF-16 have two different endiannesses.
    True enough, but any sane UTF-16 stream is going to have the endianness
    marker in it to eliminate any question.

    --
    Erik Max Francis && [email]maxalcyone.com[/email] && [url]http://www.alcyone.com/max/[/url]
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ You are inspiration to my life / You are the reason why I smile
    \__/ India Arie
    Erik Max Francis Guest

Similar Threads

  1. #33227 [Com]: zend_strtod() broken on big-endian arm
    By terry dot s dot duncan at intel dot com in forum PHP Bugs
    Replies: 0
    Last Post: January 23rd, 01:29 AM
  2. Replies: 9
    Last Post: April 27th, 04:44 AM
  3. .Net webservice and java client little endian/big endian
    By pallavi in forum ASP.NET Web Services
    Replies: 0
    Last Post: April 5th, 07:21 PM
  4. Pen Tool Use Question. (Embarrassingly Newbie Question)
    By Bozo Schmozo in forum Macromedia Flash
    Replies: 0
    Last Post: November 12th, 10:00 PM
  5. newB question: related tables question
    By Blue man in forum Microsoft SQL / MS SQL Server
    Replies: 1
    Last Post: June 30th, 04:13 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139