HELP - how to convert a string representation of a number to a number ?

Discussion in 'C++' started by cpptutor2000@yahoo.com, May 21, 2005.

  1. Guest

    Could some C++ guru please help me with this problem?
    Suppose I have a string representation of a very large number
    as: char *strNum = "1234";

    now suppose I want to store this number with each digit as an
    element of a large array: unsigned int Num[4];

    Now I want to transfer them digit by digit to the unsigned int
    array, with code as the following, but it does NOT work.

    for(int i = 0; i < 4; i++){
    if(isdigit(strNum) Num = strNum;
    }

    If I now print out the contents of the array Num, I get only
    the ASCII values to be printed out, in this case:
    49, 50, 51, 52

    How do I make sure that the values stored are the digits 1, 2,
    3 and 4, and NOT the ASCII values.

    Could someone please point out what exactly I am doing wrong?
    Thanks in advance for your help.
     
    , May 21, 2005
    #1
    1. Advertising

  2. Re: HELP - how to convert a string representation of a number toa number ?

    wrote:
    > Could some C++ guru please help me with this problem?
    > Suppose I have a string representation of a very large number
    > as: char *strNum = "1234";
    >
    > now suppose I want to store this number with each digit as an
    > element of a large array: unsigned int Num[4];
    >
    > Now I want to transfer them digit by digit to the unsigned int
    > array, with code as the following, but it does NOT work.
    >
    > for(int i = 0; i < 4; i++){
    > if(isdigit(strNum) Num = strNum;
    > }
    >
    > If I now print out the contents of the array Num, I get only
    > the ASCII values to be printed out, in this case:
    > 49, 50, 51, 52
    >
    > How do I make sure that the values stored are the digits 1, 2,
    > 3 and 4, and NOT the ASCII values.
    >
    > Could someone please point out what exactly I am doing wrong?
    > Thanks in advance for your help.
    >


    IF you only have to handle ASCII chars, this will work:

    if(isdigit(strNum) Num = strNum - 48;

    It may not work with charsets other than ASCII.

    Regards,
    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
     
    Larry I Smith, May 21, 2005
    #2
    1. Advertising

  3. "Larry I Smith" <> wrote in message
    news:lyJje.823$yO1.615@trnddc05

    > IF you only have to handle ASCII chars, this will work:
    >
    > if(isdigit(strNum) Num = strNum - 48;
    >
    > It may not work with charsets other than ASCII.


    Doesn't the Standard guarantee, that the numbers 0-9 of the
    character set will be continous? I think I read that somewhere, but I am
    not sure. If so, the above would be portable, if you used '0' instead of
    48:

    if(isdigit(strNum) Num = strNum - '0';

    Maybe someone can verify if my above statement is correct.

    regards
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, May 21, 2005
    #3
  4. Chris Theis Guest

    Re: HELP - how to convert a string representation of a number toa number ?

    Jakob Bieling wrote:
    > "Larry I Smith" <> wrote in message
    > news:lyJje.823$yO1.615@trnddc05
    >
    >
    >>IF you only have to handle ASCII chars, this will work:
    >>
    >> if(isdigit(strNum) Num = strNum - 48;
    >>
    >>It may not work with charsets other than ASCII.

    >
    >
    > Doesn't the Standard guarantee, that the numbers 0-9 of the
    > character set will be continous? I think I read that somewhere, but I am
    > not sure. If so, the above would be portable, if you used '0' instead of
    > 48:
    >
    > if(isdigit(strNum) Num = strNum - '0';
    >
    > Maybe someone can verify if my above statement is correct.
    >
    > regards


    Yes Jakob, you're right. The standard mandates that the value of each
    character in the list of decimal digits starting from '0' shall be
    greater than the previous by one .

    Cheers
    Chris
     
    Chris Theis, May 21, 2005
    #4
  5. Re: HELP - how to convert a string representation of a number toa number ?

    Jakob Bieling wrote:
    > "Larry I Smith" <> wrote in message
    > news:lyJje.823$yO1.615@trnddc05
    >
    >>IF you only have to handle ASCII chars, this will work:
    >>
    >> if(isdigit(strNum) Num = strNum - 48;
    >>
    >>It may not work with charsets other than ASCII.

    >
    > Doesn't the Standard guarantee, that the numbers 0-9 of the
    > character set will be continous? I think I read that somewhere, but I am
    > not sure. If so, the above would be portable, if you used '0' instead of
    > 48:
    >
    > if(isdigit(strNum) Num = strNum - '0';
    >
    > Maybe someone can verify if my above statement is correct.
    >
    > regards


    Does that apply to the EBCDIC charset? Actually it does. :)

    You may be correct (that 0-9 must be contiguous per the Std).
    Even if they're not, your approach still works fine for ASCII
    (and is a better solution).

    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
     
    Larry I Smith, May 21, 2005
    #5
  6. Pete Becker Guest

    Re: HELP - how to convert a string representation of a number toa number ?

    Larry I Smith wrote:

    >
    > You may be correct (that 0-9 must be contiguous per the Std).


    It is required for both C and C++.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, May 21, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hessam
    Replies:
    0
    Views:
    2,194
    Hessam
    Aug 8, 2003
  2. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,811
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  3. Replies:
    5
    Views:
    534
  4. A. L.
    Replies:
    7
    Views:
    354
    Joe Wright
    Sep 20, 2005
  5. Hessam
    Replies:
    1
    Views:
    243
    Teemu Keiski
    Aug 16, 2003
Loading...

Share This Page