Character to Int?

This is a discussion on Character to Int? within the C Programming forums, part of the General Programming Boards category; Thanks for looking . I have a character array "address": address[0] = '0' address[1] = 'x' address[2] = 'f' address[3] ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Character to Int?

    Thanks for looking .

    I have a character array "address":

    address[0] = '0'
    address[1] = 'x'
    address[2] = 'f'
    address[3] = 'f'
    address[4] = 'f'
    address[5] = 'f'
    address[6] = 'f'
    address[7] = 'f'
    address[8] = 'f'

    How would I store this as an int? I want to have the equivalent of:

    int x = 0xfffffff

    I've tried atoi() to no avail. Any help would be awesome; thanks again .

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    strtol.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Don't forget the null char if you do it that way:
    Code:
    address[9] = '\0'
    Better yet:
    Code:
    address[0]char address[] = "0xffffff"; \\ null char is implicit here
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    No idea where that extra "address[0]" came from there. Just pretend it isn't there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Why not edit?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Thanks for the replies . It's still not working correctly - here's what my code does:

    Code:
    printf("HEX: %s ", line);
     >> outputs 0x430d70
    
    char *p;
    int test = (int)strtol(line, &p, 10);
    
    printf("DECIMAL: %d ", test);
     >> outputs 0
    Any idea why this isn't outputting the expected 4394352?

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Also - I'm a little confused regarding the variance in outputs here:

    Code:
    printf("HEX1: %s ", line);
    >> 0x430d70
    
    printf("HEX2: %X ", line);
    >> 22FF40
    Any brief explanation would be awesome. Thank you again for your time.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    For the first, strtol interprets the "x" in the "0x" as the end of the string, thus returning only 0.
    For the second, it is undefined. You are telling printf that you are passing an integer, but you are passing a char pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Thanks Elysia.

    If any character closes the strtol method then how can it be used to convert '0x430d70' to an integer or binary? I want to store this value (line) as a number, not as a string.

    If I do int x = 0x234, great. I can print x as hex, binary, int, etc.

    I'm trying to get this character array stored as a number, like x.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Actually, I do not know how exactly, hence that bit missing from my reply.
    I was simply too tired to look up strtol in the doc to see if there was a solution to the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    No worries - I have it working with
    Code:
    int val = 0;
    sscanf(line, "%x", &val);

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Good work. Problem solving is a very important trait for programmers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by deliveryguy View Post
    Thanks Elysia.

    If any character closes the strtol method then how can it be used to convert '0x430d70' to an integer or binary? I want to store this value (line) as a number, not as a string.

    If I do int x = 0x234, great. I can print x as hex, binary, int, etc.

    I'm trying to get this character array stored as a number, like x.
    Did you read the manual on strtol? Did you wonder what that parameter called "base" did? (I realize you got there with sscanf.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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