Hex String to a Long

This is a discussion on Hex String to a Long within the C Programming forums, part of the General Programming Boards category; I'm getting a hexadecimal input from the command line and I can't seem to get it to convert. I know, ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Hex String to a Long

    I'm getting a hexadecimal input from the command line and I can't seem to get it to convert. I know, I know, use strtol(). The problem is I keep getting zero as the result. I hard-code hex strings ("0x13","0XF") and they work fine. I print out the string on the line before the conversion and it looks fine too.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    strtol can absolutely do this, so perhaps if you post your code, we can figure out what is going wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Code:
       result = GetFlag(nargs, args, 'S'); /* gets argument proceeding "-S" */
       fprintf(stdout, "seed=%u \n",seed); /* prints default seed value */
       fprintf(stdout, "result=%s \n",result); /* prints result string */
       if (result != NULL)
          seed = strtoul(result, NULL, 16); /* convert hex string to long */
       fprintf(stdout, "seed=%u \n",seed); /* comes back zero */
    result is a char*, and GetFlag returns a char*.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what is the exact content of result (as per the printf)?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    I'm the dumbest man alive... on my console it's hard to tell the difference between a '0' and an 'O'. I've been preceeding the numbers with the letter O this whole time. I guess auto-complete isn't always a good thing.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yup, that would of cours make a difference - and give the result you describe. Congrats to finding it - and I think you'll find that there are at least a few others who are equally dumb [and the dumbest may actually go to a lady I worked with who wondered why the copies didn't show up - turned out that if you copy what is on the back of a single-sided page, you get pretty much "nothing" ]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

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