Thread: Command Line String Conversion?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Command Line String Conversion?

    I have a hex value that is entered on the command line.

    ./test 0x50800460280250a9

    What would be the best way to convert /parse? I need to use this hex value.

  2. #2
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    What's wrong with sscanf()?

    Edit2: Removed bullsh...
    Last edited by TactX; 01-12-2006 at 03:38 PM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    int i = strtol(argv[1],NULL,0);

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    That number is too big for any C normal size integer 64-bit or smaller. This compiles but displays the wrong number
    Code:
    int main(int argc, char* argv[])
    {
    	unsigned _int64 n = 0x50800460280250a9;
    	printf("%u64\n", n);
    	return 0;
    }

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    strtoll()

    Assuming that your compiler supports long long ints, something like:
    Code:
    long long cmdval = strtoll(argv[1], NULL, 0);
    will work for values that can be expressed in 64 bits. Values beyond that must be handled using arbitrary number packages, which are not a standard part of the C library.
    Last edited by filker0; 01-12-2006 at 05:12 PM. Reason: reread the Ancient_Dragon's remark
    Insert obnoxious but pithy remark here

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    AD: shouldn't it be...?
    Code:
       unsigned __int64 n = 0x50800460280250a9;
       printf("%I64x\n", n);
    (I needed such changes for gcc anyways.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    >>int i = strtol(argv[1],NULL,0);

    unsigned long long i = strtoull ((argv[1],NULL,16);

    The above provided what I need.

    Thanks!

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Ancient Dragon
    That number is too big for any C normal size integer 64-bit or smaller.
    A long long is at least 64 bits, capable of holding values upto at least 0x7FFFFFFFFFFFFFFF. This is greater than 0x50800460280250a9.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM