473,386 Members | 1,864 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,386 software developers and data experts.

Write toNum() function to convert char to int

how to i write a funciton to convert a character into integer?

for eg: in a string the number 23456 is stored, and i want to read it char by char and convert each to it's correct int value... typecast is not helping....


Thanks a lot
Sep 20 '07 #1
4 3554
Meetee
931 Expert Mod 512MB
how to i write a funciton to convert a character into integer?

for eg: in a string the number 23456 is stored, and i want to read it char by char and convert each to it's correct int value... typecast is not helping....


Thanks a lot
Check this link where same issue is discussed.

http://www.thescripts.com/forum/thread664666.html
Sep 20 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
What language??

It's different for C than C++.
Sep 21 '07 #3
mattmao
121 100+
Spoonfeeding not allowed - though algorithms are

This is my own way of doing this char to int convert. Note that both array and digits are arrays declared outside this method.

The if statement tests the condition whether the converted value is in range 0-9. If so, then put them into a int array.
Sep 22 '07 #4
mattmao
121 100+
Okay, generally speaking:

Since in C ASCII chars are stored in memory with their corresponding int values, we can perform a simple check:

char c;
int temp;

and see the result of temp = c - '0';

If the char c was in range of '0' - '9', now temp would contain a int from zero to nine.
Sep 22 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tom Van Ginneken | last post by:
Hi, I need to write binary data to a serial port. I am using this function: #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); I am able to write a alpha-numeric...
3
by: Wen | last post by:
hello, now, i wanna port a c++ program into C# project. a DLL written by C++ and keep it no change, and UI wirtten by C#. my C++ UI code is as below: // error handlers --global function...
8
by: cdrsir | last post by:
like some useful windows commands, i.e., >>ping www.xxx.com if you only type >>ping it will give you just some options.
8
by: coosa | last post by:
Dear all, I have the following code: #include <iostream> #include <string> #include <sstream> using namespace std;
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Eric Lilja | last post by:
Consider this code that doesn't compile on two compilers I've tried: void foo(const char *) {} int main() { foo(new const char * ("bar")); } $ g++ -Wall -Wextra -std=c++98 -pedantic -g...
25
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple...
5
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.