Thread: Converting string to unsigned long

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Converting string to unsigned long

    I'm passing a memory address as arguments to a program but when I try and convert the string to an unsigned long, I don't get anything.

    Code:
    int main(int argc,char *argv[])
    {
       unsigned long address = strtoul(argv[1])
       printf("Address: %ul\n",address);
    }
    It just prints out a 0. The address I tried passing was bfffffff.

    Can I not pass hex values as arguments?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    First of all, strtoul() takes 3 arguments. Secondly, the format specifier for unsigned long is %lu, not %ul. Try this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
       unsigned long address = strtoul(argv[1], NULL, 0);
       printf("Address: %lu\n",address);
       return EXIT_SUCCESS;
    }
    If the value you are passing is hex, pass it as "0xbfffffff", or change the last parameter to strtoul() to 16 (then you can leave off the preceding 0x).

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    thanks, that helped!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM