473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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.

Jul 23 '05 #1
5 1758
cp**********@yahoo.com 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.
Jul 23 '05 #2
"Larry I Smith" <la***********@verizon.net> 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)
Jul 23 '05 #3
Jakob Bieling wrote:
"Larry I Smith" <la***********@verizon.net> 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.
Jul 23 '05 #4
Jakob Bieling wrote:
"Larry I Smith" <la***********@verizon.net> 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
Jul 23 '05 #5
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)
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
8
by: te509 | last post by:
Hi guys, does anybody know how to convert a long sequence of bits to a bit-string? I want to avoid this: '949456129574336313917039111707606368434510426593532217946399871489' I would...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.