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

c how to convert string to integer

196 100+
firstly i have tried typecasting a string to an integer or rather an element of an array which is a string e.g.

Expand|Select|Wrap|Line Numbers
  1. string[0] = "2";
  2. int a = (int)string[0];
  3.  
and i know that for c++ you can use atoi which works fine;

But i need the conversion to work on c, atoi doesn't seem to be working and when i try the typecast conversion it goes from the number 2 to some like 23465???


Any help is greatly appreciated this completely seperate from any other of my posts just to clarify.
May 4 '08 #1
4 1780
Savage
1,764 Expert 1GB
firstly i have tried typecasting a string to an integer or rather an element of an array which is a string e.g.

Expand|Select|Wrap|Line Numbers
  1. string[0] = "2";
  2. int a = (int)string[0];
  3.  
and i know that for c++ you can use atoi which works fine;

But i need the conversion to work on c, atoi doesn't seem to be working and when i try the typecast conversion it goes from the number 2 to some like 23465???


Any help is greatly appreciated this completely seperate from any other of my posts just to clarify.
Well ofcourse atoi doesn't work when you are trying to call it with string[0] as a parameter.Atoi takes a const char *ptr,adn you are giving him a single char.

Conversion from char to int is automatic so if you say

a=string[0];

and string[0]='2'(which is ASCII value of 50(usually) or '0'+2),a will become 50 or '0'+2.But you need it to be 2,right?
So what are you going to do?

There is a way to do it with atoi,but it's rather cumbersome.It's better to stick with this one.
May 4 '08 #2
chazzy69
196 100+
Ok, if string[0] = "2" and a = string[0]

i do get a = 50

So could i possible say then that a = a - 48, but this would only work number is between 0 and 9.

Any sugestions how to convert ascii value of 2 back to 2 but keeping it as a string.
May 4 '08 #3
JosAH
11,448 Expert 8TB
Ok, if string[0] = "2" and a = string[0]

i do get a = 50

So could i possible say then that a = a - 48, but this would only work number is between 0 and 9.

Any sugestions how to convert ascii value of 2 back to 2 but keeping it as a string.
atof(), atoi() and atol() also work fine in C, they're functions in stdlib. For the
other way around (number to string) simply use the sprintf() function.

kind regards,

Jos
May 4 '08 #4
chazzy69
196 100+
Sweat it was a case of forgetting to link to the right header file Thanks heaps everyone for their help.
May 4 '08 #5

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

Similar topics

5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
8
by: Carlos | last post by:
I have a strinf with comma separated field. Is is possible to convert it to a Structure ? Thanks
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
5
by: Mika M | last post by:
Hi! I've made little code to convert string into hex string... Public ReadOnly Property ToHexString(ByVal text As String) As String Get Dim arrBytes As Integer() = CharsToBytes(text) Dim sb...
14
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
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!
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.