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

Convert char to int

Hi. I have such a question, how can I convert some char into int?
Example: char a ='5';
and I need to get the same "5" but int type. And if it's possible, then actually is required some how from type string or char convert 2 chars to int value(for example convert "35" to int 35.

Can someone help please?
May 17 '07 #1
7 30030
AdrianH
1,251 Expert 1GB
Hi. I have such a question, how can I convert some char into int?
Example: char a ='5';
and I need to get the same "5" but int type. And if it's possible, then actually is required some how from type string or char convert 2 chars to int value(for example convert "35" to int 35.

Can someone help please?
int aa = '5';

For your second question, lookup atoi(), atof() or sscanf().


Adrian
May 17 '07 #2
Oh, really, I forgot about "atoi()" function. Thanks.
May 17 '07 #3
ilikepython
844 Expert 512MB
Hi. I have such a question, how can I convert some char into int?
Example: char a ='5';
and I need to get the same "5" but int type. And if it's possible, then actually is required some how from type string or char convert 2 chars to int value(for example convert "35" to int 35.

Can someone help please?
If the char is between 0-9 you can use this:
Expand|Select|Wrap|Line Numbers
  1. char mychar = '2';
  2. int myint = mychar - '0';
  3.  
If it's a bigger number however, you can use the atoi() function:
Expand|Select|Wrap|Line Numbers
  1. char mychar[] = "35";
  2. int myint = atoi(mychar);
  3.  
Is that what you're looking for?
May 17 '07 #4
ilikepython
844 Expert 512MB
Oops, too late. sorry Adrian
May 17 '07 #5
AdrianH
1,251 Expert 1GB
Oops, too late. sorry Adrian
No need to appologies. The more that help, the sooner they get help. ;)


Adrian
May 17 '07 #6
Banfa
9,065 Expert Mod 8TB
For your second question, lookup atoi(), atof() or sscanf().
My preference for a conversion function is strtol (or strtoul) because you are able to tell if and how things went wrong (if and when they go wrong) using this funcation as opposed to atoi.
May 17 '07 #7
AdrianH
1,251 Expert 1GB
My preference for a conversion function is strtol (or strtoul) because you are able to tell if and how things went wrong (if and when they go wrong) using this funcation as opposed to atoi.
Cool, I'll have to rember that one. I usually use scanf and its relitives for that same reason.


Adrian
May 17 '07 #8

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

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
1
by: Sam Smith | last post by:
Hi, I wan't a function to take a const char*, a start bit position and number of bits and convert that bit-stream into a primitive of desired type. I.e. something like: char convert(const...
5
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
19
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h>...
3
by: Eric BOUXIROT | last post by:
hi, i must convert all of these eVC++ prototypes to use with VB.NET.... DLLEXPORT long F_BDO_MessageBoxOK(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_MessageBoxOUINON(char *IN_title,...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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

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.