Formatting 64 bit integers

This is a discussion on Formatting 64 bit integers within the C++ Programming forums, part of the General Programming Boards category; I'm converting integers from 8-byte big endian to 8-byte little endian. Moving the bytes around is trivial to fix the ...

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309

    Formatting 64 bit integers

    I'm converting integers from 8-byte big endian to 8-byte little endian. Moving the bytes around is trivial to fix the endianness, but I can't seem to put my hands on a cross-platform data type for a 64 bit integer, and formatting routines to turn them into readable output.

    Cross platform = Mac & Windows primarily - Linux would be good too.

    Any advice?

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    gcc supports "long long", which is 64-bit.
    In MS compiler, you can use _int64.

    The best option is probably to make your own type:
    #typedef long long TINT64

    Then you can use #if to match whatever compiler version you are using, and thus get it "right" all the time.

    Likewise, you need a macro such as:
    #if MSCOMPILER
    #define PR64 "%i64d"
    #else
    #define PR64 "%lld"
    #endif

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Thanks Matsp. I'll give it a go.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,459
    long long support in gcc comes as an extension, though. And if I remember correctly you should prototype all functions with LL arguments or that are expected to receive an LL as an argument.

    Hopefully C99 is around the corner...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    int64 int128 and int256 are defined in the standard, although only int64 is actually supported by most 'compliant' compilers. The endianness is supposed to be handled by the compiler. Bitshift operations are supposed to work properly regardless of the target processor.

    And in a perfect world I would be rich, good looking and intelligent, I guess one out of 3 isnt too bad
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by abachler View Post
    And in a perfect world I would be rich, good looking and intelligent, I guess one out of 3 isnt too bad
    So, you're saying you have a lot of money??? lol.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by abachler View Post
    int64 int128 and int256 are defined in the standard, although only int64 is actually supported by most 'compliant' compilers.
    int64_t is optional but mentioned, int128_t and int256_t may be supported, but aren't even explicitly mentioned. The compiler may support intN_t for absolutely any N, provided it follows the other restrictions on the type.

    int_least64_t and int_fast64_t are required. Larger Ns are optional and not explicitly mentioned.

    This is C99.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. A few questions about 64 bit and Windows...
    By yaya in forum Tech Board
    Replies: 9
    Last Post: 08-28-2008, 08:49 AM
  3. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  5. If the RGB color is (64, 64, 255), change it to (64, 255, 64).
    By Grayson_Peddie in forum C# Programming
    Replies: 2
    Last Post: 06-14-2003, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21