Convert two 32-bit longs to one 64-bit long long

This is a discussion on Convert two 32-bit longs to one 64-bit long long within the C Programming forums, part of the General Programming Boards category; Hey guys, What I need is relatively simple. I need to convert two longs (each 4 digits, "0" padded) into ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Convert two 32-bit longs to one 64-bit long long

    Hey guys,

    What I need is relatively simple. I need to convert two longs (each 4 digits, "0" padded) into a single long long.

    e.g.
    long a, b;
    long long c;

    a = 3;
    b = 4;

    sprintf((char*) c,"%04ld%04ld",a,b);

    The result should be c is equal to 00030004. That method doesn't work though. Anyone got any ideas? Do I need to convert it to a string first then to a long long?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    It sounds like you just want to write:
    Code:
    c = a * 10000 + b;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Perfect! Makes sense, probably should have figured that out on my own. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  2. Convert a long to negative?
    By Jonnster in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2010, 09:35 AM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. printf conversion characters for long longs.
    By samus250 in forum C Programming
    Replies: 9
    Last Post: 04-13-2008, 08:37 PM
  5. STLport with MingW - Long Long error on project build
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 08-21-2006, 08:55 AM

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