Converting from char to integer value

H

hantie

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.
 
M

Martin Dickopp

hantie said:
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
 
R

Randy Howard

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.
 
E

Eric Sosman

hantie said:
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';
 
S

shantia

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
 
M

Martin Dickopp

shantia said:
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
 
H

hantie

Thanks Martin.

Martin Dickopp said:
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
 
D

Default User

hantie said:
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
 
H

hantie

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 ?
 
M

MikeyD

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.
 
I

Irrwahn Grausewitz

<[email protected]>:
Please, don't top-post!
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
 
A

August Derleth

<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.
 
D

Default User

hantie said:
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
 
M

Martin Ambuhl

hantie said:
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.
 
H

hantie

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.
 
J

Joona I Palaste

hantie said:
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 ([email protected]) ---------------------------\
| 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
 
M

MikeyD

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

hantie

Joona I Palaste said:
It's not. You're still top-posting.

This is top-posting:
--------------------------
Yes.
--------------------------
This is bottom-posting:
Yes.
--------------------------

--
/-- Joona Palaste ([email protected]) ---------------------------\
| 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
 
E

Eric Sosman

Martin said:
('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 ...
 
D

Default User

MikeyD said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top