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

TO convert a character into integer

5
How can we convert a character into integer
char a[40]
my input to this may be like 21466845687552
now i want to access each element and want to convert it into integer and then apply aritmatic operation can i do this???????
Jul 27 '07 #1
10 3746
How can we convert a character into integer
char a[40]
my input to this may be like 21466845687552
now i want to access each element and want to convert it into integer and then apply aritmatic operation can i do this???????
Use function atoi()

This will convert character to integer.

Regards,
Girish.
Jul 27 '07 #2
archonmagnus
113 100+
I try to avoid atoi(). In C++, it is better (in my opinion) to use stringstreams. Try this:

Expand|Select|Wrap|Line Numbers
  1. #include <sstream>
  2.  
  3. char a[40];
  4. int  integerValue[40];
  5. stringstream foo;
  6.  
  7. // ... read in character array...
  8.  
  9. // This connverts the character array from char to int
  10. for (int i = 0; i < 40; i++)
  11. {
  12.     foo<<a[i];
  13.     foo>>integerValue[i];
  14. }
  15.  
  16. // ... do your manipulations, etc. ...
  17.  
Of course, the code is a bit more robust if you use vectors instead of arrays. Then, you aren't limited to fixed sizes.
Jul 27 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
I think the OP said each element of the array had top be converted to an integer.

If that's the case, all you do is:
Expand|Select|Wrap|Line Numbers
  1. char a[40];
  2.  
  3. int i = a[1] - 48;
  4.  
Fashion a loop around this and off you go.
Jul 27 '07 #4
JosAH
11,448 Expert 8TB
I think the OP said each element of the array had top be converted to an integer.

If that's the case, all you do is:
Expand|Select|Wrap|Line Numbers
  1. char a[40];
  2.  
  3. int i = a[1] - 48;
  4.  
Fashion a loop around this and off you go.
Yuck, that fails miserably on EBCDIC machines (IBM). Better make that:

Expand|Select|Wrap|Line Numbers
  1. int i = a[1] - '0';
  2.  
kind regards,

Jos ( <--- nitpicker ;-)
Jul 27 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Yuck, that fails miserably on EBCDIC machines (IBM). Better make that:
Really? Are you saying that C on a 370 isn't ASCII??

EBCDIC is a 16 bit character set. So, sizeof(char) is 2 bytes and all the C library funcitons (and the sorts since EBCDIC sorts differently from ASCII) have been rewritten to EBCDIC??

Really??
Jul 27 '07 #6
JosAH
11,448 Expert 8TB
Really? Are you saying that C on a 370 isn't ASCII??

EBCDIC is a 16 bit character set. So, sizeof(char) is 2 bytes and all the C library funcitons (and the sorts since EBCDIC sorts differently from ASCII) have been rewritten to EBCDIC??

Really??
Last time I checked, EBCDIC was also an 8 bit character encoding. Have a look
at this table. EBCDIC used 8 bits all the time where ASCII used only 7 bits;
the 8th bit came into play when they realized that they had to go 'international' ;-)
IBM never had those intentions; quite smart actually because ASCII lacks the
bitwidth for true Unicode anyway (except for those UTF/8 hacks).

This is a nice link that clearly shows where EBCDIC came from.
And where it's going ;-)

kind regards,

Jos

ps. There's no need to re-implement anything w.r.t. the sort method, i.e. the
things just get sorted according to EBCDIC, that's all.
Jul 27 '07 #7
JosAH
11,448 Expert 8TB
EBCDIC is a 16 bit character set. So, sizeof(char) is 2 bytes and all the C library funcitons (and the sorts since EBCDIC sorts differently from ASCII) have been rewritten to EBCDIC??
(forgot to reply to this part of your reply)

If doesn't matter how many bits are in a char. The sizeof() operator returns 1 by
definition. All that matters is that sizof(char) <= sizeof(short) <= sizeof(int) etc.
and their minimum maximal values as defined in limits.h.

e.g. on a typical old ARM processor a char might be 32bits and all sizeof()
values would be equal to 1.

kind regards,

Jos

ps. I don't think there were C implementations for those old Arms; I might be wrong.
Jul 27 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
I guess I remembered EBCDIC as more than 8 bits.

However, it was really cruel to bring up an 029 key punch. To this day I can still read those Hollerith punches.

Thank you for straightening me out again.
Jul 28 '07 #9
JosAH
11,448 Expert 8TB
I guess I remembered EBCDIC as more than 8 bits.

However, it was really cruel to bring up an 029 key punch. To this day I can still read those Hollerith punches.

Thank you for straightening me out again.
I remember those IBM 29s too; remember that 'control card' behind that little
window in the middle of that noisy thing? You could actually program that darn
thing a bit, like advance to position so-and-so before typing; release the card
after position so-and-so; oh the utmost fun we had sabotaging these things! ;-)

kind regards,

Jos
Jul 28 '07 #10
maan
3
i hope this programe will convert character into integer

<complete spoonfed code deleted>

Don't supply full code solutions. Read this part of the guidelines.

Please don't do this again; you stand the risk of a ban.

kind regards,

Jos
Jul 29 '07 #11

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

Similar topics

4
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
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
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...
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 ---->...
8
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
4
by: sarada purkait | last post by:
hiii i am taking input from a file as a character and have to convert it into integer to do some operations eg the character input may be 1 , 0 , 2 etc. and i need to convert them into integers...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
4
by: sagar | last post by:
Hi Guys.. I Wanted to convert an int to a string in C#...but i cannot use any built in functions.So, can any one tell me an algorithm or a program to do so....please is urgent....
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
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.