Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > HELP - how to convert a string representation of a number to a number ?

Reply
Thread Tools

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

 
 
cpptutor2000@yahoo.com
Guest
Posts: n/a
 
      05-21-2005
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[i]) Num[i] = strNum[i];
}

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.

 
Reply With Quote
 
 
 
 
Larry I Smith
Guest
Posts: n/a
 
      05-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[i]) Num[i] = strNum[i];
> }
>
> 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[i]) Num[i] = strNum[i] - 48;

It may not work with charsets other than ASCII.

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      05-21-2005
"Larry I Smith" <(E-Mail Removed)> wrote in message
news:lyJje.823$yO1.615@trnddc05

> IF you only have to handle ASCII chars, this will work:
>
> if(isdigit(strNum[i]) Num[i] = strNum[i] - 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[i]) Num[i] = strNum[i] - '0';

Maybe someone can verify if my above statement is correct.

regards
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      05-21-2005
Jakob Bieling wrote:
> "Larry I Smith" <(E-Mail Removed)> wrote in message
> news:lyJje.823$yO1.615@trnddc05
>
>
>>IF you only have to handle ASCII chars, this will work:
>>
>> if(isdigit(strNum[i]) Num[i] = strNum[i] - 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[i]) Num[i] = strNum[i] - '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
 
Reply With Quote
 
Larry I Smith
Guest
Posts: n/a
 
      05-21-2005
Jakob Bieling wrote:
> "Larry I Smith" <(E-Mail Removed)> wrote in message
> news:lyJje.823$yO1.615@trnddc05
>
>>IF you only have to handle ASCII chars, this will work:
>>
>> if(isdigit(strNum[i]) Num[i] = strNum[i] - 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[i]) Num[i] = strNum[i] - '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.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      05-21-2005
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Can I get the 8bit-string representation of any unicode string wanghz@gmail.com Python 5 02-12-2006 06:54 PM
precise string representation of float number A. L. C Programming 7 09-20-2005 12:20 AM
Cannot create an object of type 'System.String[]' from its representation 'String[] Array' Hessam ASP .Net 0 08-08-2003 08:36 AM



Advertisments