Thread: Formatting 64 bit integers

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

    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
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    Thanks Matsp. I'll give it a go.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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...
    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,195
    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

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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, 06: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, 06: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