how do I convert a long long int into a char array?

This is a discussion on how do I convert a long long int into a char array? within the C Programming forums, part of the General Programming Boards category; Without lossing information?...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    150

    how do I convert a long long int into a char array?

    Without lossing information?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Quote Originally Posted by Once-ler View Post
    Without lossing information?
    To convert a long long int into a char array, use the sprintf function with the long long int specifier
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    How do I get this to work?
    Code:
      #include <stdio.h>
       int main()
       {
          char b[1000];
          long long int i = 42393848474;
          sprintf( b, "Formatted data:  %d", i );
          puts( b );
       }
    returns a value of -555824486
    and larger numbers like long long int i = 88842393848474; shows 995330714
    I have no idea what is happening here.
    if I increase char b[] much more the compiler gives me the too big error
    I need it to do the full spectrum of 18446744073709551615
    Thanks
    Last edited by Once-ler; 02-22-2013 at 04:55 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,677
    %d format is for int. Not for long long int. Use the wrong format specifier, and the result is undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Thanks you guys.
    add ll in %lld fixed the problem. Thankyou so much.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    except that it dosen't work all the way to 18446744073709551615
    I keep getting a negative number

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by Once-ler View Post
    except that it dosen't work all the way to 18446744073709551615
    I keep getting a negative number
    Because long long range (64-bit) is from -9223372036854775808 to 9223372036854775807
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    what if I want to work with all positive numbers what would the range be then?

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    You can use "unsigned long long" in that case.
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I tried that with 18446744073709551615 and it keeps giving me a negative number
    Thanks for your help.

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    sprintf should also be changed to "%ulld" ( sorry, I forgot about that )
    Devoted my life to programming...

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Code:
    #include <stdio.h>
       int main()
       {
         char b[1000];
         unsigned long long int i =18446744073709551615 ;
          sprintf( b, "Formatted data:  %ulld", i );
          puts( b );
          if (i&1) printf("is odd");
       }
    doing that gives me back a number that is 4294967295lld

  14. #14
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    They mean %llu. Check "man 3 sprintf".

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Thank you.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert char array of 64 bytes to long long int
    By pkumarn in forum C Programming
    Replies: 19
    Last Post: 03-06-2012, 02:23 AM
  2. [C]Convert Char* to Unsigned Long
    By MaSSaSLaYeR in forum C Programming
    Replies: 10
    Last Post: 11-17-2011, 06:46 AM
  3. Convert two 32-bit longs to one 64-bit long long
    By chriscapitolo in forum C Programming
    Replies: 2
    Last Post: 04-27-2011, 02:43 PM
  4. cannot convert parameter 1 from 'char *' to 'long'
    By MrLucky in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2006, 07:31 AM
  5. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 12:33 PM

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