473,398 Members | 2,120 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,398 software developers and data experts.

Converting from char to integer value

In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?

Sorry for this newbie question.
Nov 13 '05 #1
22 53456
"hantie" <ha****@hotmail.com> writes:
In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?


c - '0'

This works because the character encodings of the digit characters are
guaranteed to be consecutive and in the correct order. Therefore, if
the '2' character has value 50, '0' is guaranteed to have value 48 on
the same system.

Martin
Nov 13 '05 #2
In article <7h*******************@news-server.bigpond.net.au>,
ha****@hotmail.com says...
In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?

Sorry for this newbie question.


The strtol() function from stdlib.h might be what you're looking for.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 13 '05 #3
hantie wrote:

In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?

Sorry for this newbie question.


The character codes for '0' through '9' are guaranteed
to be consecutive, so you can just subtract:

char two_as_char = '2';
int two_as_int = two_as_char - '0';

--
Er*********@sun.com
Nov 13 '05 #4
Thanks Martin. It worked, but it only works for digits 1-9 only since char
stores only 1 character. If say I am using char* to store digit 22.
How can I convert it to integer so I can use it as if it was an int type?

Thanks in advance

"Martin Dickopp" <ex*************@zero-based.org> wrote in message
news:bj*************@news.t-online.com...
"hantie" <ha****@hotmail.com> writes:
In my program, for reducing the complexity, the integer was stored as char, so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?


c - '0'

This works because the character encodings of the digit characters are
guaranteed to be consecutive and in the correct order. Therefore, if
the '2' character has value 50, '0' is guaranteed to have value 48 on
the same system.

Martin

Nov 13 '05 #5
"shantia" <sh*****@yahoo.com> writes:
It worked, but it only works for digits 1-9 only since char stores only
1 character. If say I am using char* to store digit 22. How can I
convert it to integer so I can use it as if it was an int type?


Use the `strtol', `strtoul', or `atoi' function. The last one has the most
simple interface of all three, but it doesn't allow error checking, so it
should only be used if you know for sure that the string represents a
number.

Martin
Nov 13 '05 #6
Thanks Martin.

"Martin Dickopp" <ex*************@zero-based.org> wrote in message
news:bj*************@news.t-online.com...
"shantia" <sh*****@yahoo.com> writes:
It worked, but it only works for digits 1-9 only since char stores only
1 character. If say I am using char* to store digit 22. How can I
convert it to integer so I can use it as if it was an int type?


Use the `strtol', `strtoul', or `atoi' function. The last one has the most
simple interface of all three, but it doesn't allow error checking, so it
should only be used if you know for sure that the string represents a
number.

Martin

Nov 13 '05 #7
hantie wrote:

In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?

This doesn't make any sense to me. What "complexity" are you supposed to
be reducing? Please lay out the actual use case, then we can give more
practical advice. I can't see any reason for doing this unless you are
talking about data imported from a stream.


Brian Rodenborn
Nov 13 '05 #8
Hi Brian,

Thank you for asking the question about the complexity. I had to modify the
whole program for the stub test, and it was having union inside a structure.
It is a binary tree with a choice of elements, either char or int. I had
problems with post order traversing with the tree sturctures I had. In
simpler way, I just modified the unions, and contain only char type for the
elements to check for other procedures in the whole program.

Do you have any practical advice for a tree to contain EITHER char or int
type without union ?

"Default User" <fi********@company.com> wrote in message
news:3F***************@company.com...
hantie wrote:

In my program, for reducing the complexity, the integer was stored as char, so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?

This doesn't make any sense to me. What "complexity" are you supposed to
be reducing? Please lay out the actual use case, then we can give more
practical advice. I can't see any reason for doing this unless you are
talking about data imported from a stream.


Brian Rodenborn

Nov 13 '05 #9
The standard function atoi() may be what you want, but it works with strings
rather than chars. If you know the next memory location is not going to be a
char containing a valid integer, you can get away with atoi(&character),
although obviously that's bad style.
"hantie" <ha****@hotmail.com> wrote in message
news:7h*******************@news-server.bigpond.net.au...
In my program, for reducing the complexity, the integer was stored as char, so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?

Sorry for this newbie question.

Nov 13 '05 #10
"MikeyD" <m_*********@hotmail.com> wrote in
<10****************@demeter.uk.clara.net>:
Please, don't top-post!
<top-post fixed>
"hantie" <ha****@hotmail.com> wrote in message
news:7h*******************@news-server.bigpond.net.au...
In my program, for reducing the complexity, the integer was stored as

char,
so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?

The standard function atoi() may be what you want, but it works with strings
rather than chars. If you know the next memory location is not going to be a
char containing a valid integer, you can get away with atoi(&character),
although obviously that's bad style.

Actually, this is a serious bug! Guess why ... :)

What one could write is:

char c = '2';
/* ... */
int i = c - '0'; /* But: make sure that c is in the
range of '0'..'9'!!! */

Irrwahn
--
No sig today.
Nov 13 '05 #11
"hantie" <ha****@hotmail.com> wrote in message news:<ol*******************@news-server.bigpond.net.au>...

<snip post>

We aren't playing Jeopardy. Don't top-post and try to trim the post
you're responding to (only leave enough of the old post to give
context to your own, and don't put your reply above the old post).

Following the basic rules shows respect for others and makes you look
more intelligent.
Nov 13 '05 #12
hantie wrote:
In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?


('2' - '0') == 2 in all C implementations. It is only the digits
"01234567889" for which such a guarantee holds; no other characters can be
confidently related to each other in such a way.
--
Martin Ambuhl

Nov 13 '05 #13
hantie wrote:

Hi Brian,
Hi. Don't top-post.
Thank you for asking the question about the complexity. I had to modify the
whole program for the stub test, and it was having union inside a structure.
It is a binary tree with a choice of elements, either char or int. I had
problems with post order traversing with the tree sturctures I had. In
simpler way, I just modified the unions, and contain only char type for the
elements to check for other procedures in the whole program.
What do you mean by "char"? A pointer to char? An array of char? One
char? From other answers, it seems as though you mean one of the first
two, but it still isn't 100% clear.

Are you trying to store strings, like "42" in the tree nodes? If so,
then strtol() will work, although you need to learn the ways to do error
checking.
Do you have any practical advice for a tree to contain EITHER char or int
type without union ?


IF you mean char array to store a string, then union will work. In that
case, your struct that represents a node must have a flag that tells
which type data it stores.

Brian Rodenborn
Nov 13 '05 #14
Sorry about the top-post issue. I did not know that. I tried to reply this
way, hopefully it is the right way.

By the way, the tree nodes will store "35", or "100", or "6897". Is it
possible to obtain the int value if storing those value as (char*) and use
strol to convert it to integer? or the using the union is better.

Thank you for the response and sorry for the top-post issue.

"hantie" <ha****@hotmail.com> wrote in message
news:7h*******************@news-server.bigpond.net.au...
In my program, for reducing the complexity, the integer was stored as char, so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?

Sorry for this newbie question.

Nov 13 '05 #15
hantie <ha****@hotmail.com> scribbled the following:
Sorry about the top-post issue. I did not know that. I tried to reply this
way, hopefully it is the right way.
It's not. You're still top-posting.

This is top-posting:
--------------------------
Yes. Are you there? --------------------------
This is bottom-posting: Are you there?

Yes.
--------------------------

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF
Nov 13 '05 #16
If you're using proper strings terminating in \0, use the standard function
atoi(char*) to convert them into int values.
By the way, the tree nodes will store "35", or "100", or "6897". Is it
possible to obtain the int value if storing those value as (char*) and use
strol to convert it to integer? or the using the union is better.

Nov 13 '05 #17

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bj**********@oravannahka.helsinki.fi...
hantie <ha****@hotmail.com> scribbled the following:
Sorry about the top-post issue. I did not know that. I tried to reply this way, hopefully it is the right way.


It's not. You're still top-posting.

This is top-posting:
--------------------------
Yes.
Are you there?

--------------------------
This is bottom-posting:
Are you there?

Yes.
--------------------------

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF

Thank you Joana. I hope I'm right this time... Sorry about that..really
unaware about this.
Sorry
Nov 13 '05 #18
Martin Ambuhl wrote:

hantie wrote:
In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as '2', which is decimal: 50
Is is possible for me to obtain the integer value of '2' in the program,
i.e. I want to process '2' as int: 2, not int: 50 ?


('2' - '0') == 2 in all C implementations. It is only the digits
"01234567889" for which such a guarantee holds; no other characters can be
confidently related to each other in such a way.


Actually, ('\123' - '\121') == 2 on all C implementations,
but that's sort of cheating ...

--
Er*********@sun.com
Nov 13 '05 #19
MikeyD wrote:

If you're using proper strings terminating in \0, use the standard function
atoi(char*) to convert them into int values.

Don't top-post.
This is NOT good advice, atoi() is a fairly dangerous function, as it
has no error checking ability. Use the more complicated but safer
strtol() instead.


Brian Rodenborn
Nov 13 '05 #20
hantie <ha****@hotmail.com> scribbled the following:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bj**********@oravannahka.helsinki.fi...
hantie <ha****@hotmail.com> scribbled the following:
> Sorry about the top-post issue. I did not know that. I tried to reply this > way, hopefully it is the right way.
It's not. You're still top-posting.

This is top-posting:
--------------------------
Yes.
> Are you there?

--------------------------
This is bottom-posting:
> Are you there?

Yes.
--------------------------


(Sig snipped)
Thank you Joana. I hope I'm right this time... Sorry about that..really
unaware about this.
Sorry


You got the hang of posting right. Now you only need to snip away my
sig and stop calling me Joana. =)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
Nov 13 '05 #21
Joona I Palaste wrote:

[ much snipping ]
Thank you Joana. I hope I'm right this time... Sorry about that..really
unaware about this.
Sorry


You got the hang of posting right. Now you only need to snip away my
sig and stop calling me Joana. =)

Priceless! But pray tell, how shall we pronounce 'Joona'? Finnish has
none of the Indo-European roots we know and love. Palaste might be
Italian.?
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #22
Joe Wright <jo********@earthlink.net> scribbled the following:
Joona I Palaste wrote:

[ much snipping ]

> Thank you Joana. I hope I'm right this time... Sorry about that..really
> unaware about this.
> Sorry


You got the hang of posting right. Now you only need to snip away my
sig and stop calling me Joana. =)

Priceless! But pray tell, how shall we pronounce 'Joona'? Finnish has
none of the Indo-European roots we know and love. Palaste might be
Italian.?


Joona is pronounced /jo:na/ if you can read ASCII-IPA. Spelled in
English pronunciation it would be something like "yaw-na". Palaste is
not Italian, it's a Finnish word, which is utterly devoid of meaning.
It was invented by my grandfather's brother in the period of the
Finnish-ising of names after World War II.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Insanity is to be shared."
- Tailgunner
Nov 13 '05 #23

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

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
7
by: djc | last post by:
I have typically always used one of the VB CType functions (not CType itself but CInt, CString, etc...) to cast/convert an input value to its proper type before passing it as a parameter to a SQL...
12
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
9
by: Jeremy Kitchen | last post by:
Are there any library functions that can help me to do this? If necessary I can convert the string to a byte array. I don't want to have to write my own Hex conversion if it isn't necessary. ...
12
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef ...
5
by: AZRebelCowgirl73 | last post by:
I am trying to accomodate for negative numbers, and as long as the negative is not being used, I have no problems with my input() ouput() and add() functions. I have tried this two different ways,...
9
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
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?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.