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

  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
    28,413
    It sounds like you just want to write:
    Code:
    c = a * 10000 + b;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10: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