Connecting Tech Pros Worldwide Forums | Help | Site Map

Converting from char to integer value

hantie
Guest
 
Posts: n/a
#1: Nov 13 '05
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.



Martin Dickopp
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Converting from char to integer value


"hantie" <hantie@hotmail.com> writes:
[color=blue]
> 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 ?[/color]

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
Randy Howard
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Converting from char to integer value


In article <7ho5b.80468$bo1.38474@news-server.bigpond.net.au>,
hantie@hotmail.com says...[color=blue]
> 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.[/color]

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

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: postmaster@sco.com
Eric Sosman
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Converting from char to integer value


hantie wrote:[color=blue]
>
> 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.[/color]

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';

--
Eric.Sosman@sun.com
shantia
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Converting from char to integer value


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" <expires-nov2003@zero-based.org> wrote in message
news:bj53r3$oq3$05$2@news.t-online.com...[color=blue]
> "hantie" <hantie@hotmail.com> writes:
>[color=green]
> > In my program, for reducing the complexity, the integer was stored as[/color][/color]
char,[color=blue][color=green]
> > 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 ?[/color]
>
> 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[/color]


Martin Dickopp
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Converting from char to integer value


"shantia" <shantia@yahoo.com> writes:
[color=blue]
> 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?[/color]

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
hantie
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Converting from char to integer value


Thanks Martin.

"Martin Dickopp" <expires-nov2003@zero-based.org> wrote in message
news:bj58lo$oq3$05$3@news.t-online.com...[color=blue]
> "shantia" <shantia@yahoo.com> writes:
>[color=green]
> > 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?[/color]
>
> 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[/color]


Default User
Guest
 
Posts: n/a
#8: Nov 13 '05

re: Converting from char to integer value


hantie wrote:[color=blue]
>
> 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 ?[/color]


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
hantie
Guest
 
Posts: n/a
#9: Nov 13 '05

re: Converting from char to integer value


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" <first.last@company.com> wrote in message
news:3F562A44.DEF3F75D@company.com...[color=blue]
> hantie wrote:[color=green]
> >
> > In my program, for reducing the complexity, the integer was stored as[/color][/color]
char,[color=blue][color=green]
> > 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 ?[/color]
>
>
> 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[/color]


MikeyD
Guest
 
Posts: n/a
#10: Nov 13 '05

re: Converting from char to integer value


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" <hantie@hotmail.com> wrote in message
news:7ho5b.80468$bo1.38474@news-server.bigpond.net.au...[color=blue]
> In my program, for reducing the complexity, the integer was stored as[/color]
char,[color=blue]
> 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.
>
>[/color]


Irrwahn Grausewitz
Guest
 
Posts: n/a
#11: Nov 13 '05

re: Converting from char to integer value


"MikeyD" <m_donaghy50@hotmail.com> wrote in
<1062617690.57606.0@demeter.uk.clara.net>:
Please, don't top-post!
<top-post fixed>[color=blue]
>"hantie" <hantie@hotmail.com> wrote in message
>news:7ho5b.80468$bo1.38474@news-server.bigpond.net.au...[color=green]
>> In my program, for reducing the complexity, the integer was stored as[/color]
>char,[color=green]
>> 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 ?
>>[/color]
>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.[/color]
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.
August Derleth
Guest
 
Posts: n/a
#12: Nov 13 '05

re: Converting from char to integer value


"hantie" <hantie@hotmail.com> wrote in message news:<olq5b.80494$bo1.39881@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.
Martin Ambuhl
Guest
 
Posts: n/a
#13: Nov 13 '05

re: Converting from char to integer value


hantie wrote:
[color=blue]
> 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 ?[/color]

('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

Default User
Guest
 
Posts: n/a
#14: Nov 13 '05

re: Converting from char to integer value


hantie wrote:[color=blue]
>
> Hi Brian,[/color]

Hi. Don't top-post.
[color=blue]
> 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.[/color]

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.
[color=blue]
> Do you have any practical advice for a tree to contain EITHER char or int
> type without union ?[/color]

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
hantie
Guest
 
Posts: n/a
#15: Nov 13 '05

re: Converting from char to integer value


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" <hantie@hotmail.com> wrote in message
news:7ho5b.80468$bo1.38474@news-server.bigpond.net.au...[color=blue]
> In my program, for reducing the complexity, the integer was stored as[/color]
char,[color=blue]
> 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.
>
>[/color]


Joona I Palaste
Guest
 
Posts: n/a
#16: Nov 13 '05

re: Converting from char to integer value


hantie <hantie@hotmail.com> scribbled the following:[color=blue]
> Sorry about the top-post issue. I did not know that. I tried to reply this
> way, hopefully it is the right way.[/color]

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

This is top-posting:
--------------------------
Yes.[color=blue]
> Are you there?[/color]
--------------------------
This is bottom-posting:[color=blue]
> Are you there?[/color]
Yes.
--------------------------

--
/-- Joona Palaste (palaste@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
MikeyD
Guest
 
Posts: n/a
#17: Nov 13 '05

re: Converting from char to integer value


If you're using proper strings terminating in \0, use the standard function
atoi(char*) to convert them into int values.[color=blue]
> 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.
>[/color]


hantie
Guest
 
Posts: n/a
#18: Nov 13 '05

re: Converting from char to integer value



"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
news:bj6g8o$l2m$1@oravannahka.helsinki.fi...[color=blue]
> hantie <hantie@hotmail.com> scribbled the following:[color=green]
> > Sorry about the top-post issue. I did not know that. I tried to reply[/color][/color]
this[color=blue][color=green]
> > way, hopefully it is the right way.[/color]
>
> It's not. You're still top-posting.
>
> This is top-posting:
> --------------------------
> Yes.[color=green]
> > Are you there?[/color]
> --------------------------
> This is bottom-posting:[color=green]
> > Are you there?[/color]
> Yes.
> --------------------------
>
> --
> /-- Joona Palaste (palaste@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[/color]


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


Eric Sosman
Guest
 
Posts: n/a
#19: Nov 13 '05

re: Converting from char to integer value


Martin Ambuhl wrote:[color=blue]
>
> hantie wrote:
>[color=green]
> > 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 ?[/color]
>
> ('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.[/color]

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

--
Eric.Sosman@sun.com
Default User
Guest
 
Posts: n/a
#20: Nov 13 '05

re: Converting from char to integer value


MikeyD wrote:[color=blue]
>
> If you're using proper strings terminating in \0, use the standard function
> atoi(char*) to convert them into int values.[/color]


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
Joona I Palaste
Guest
 
Posts: n/a
#21: Nov 13 '05

re: Converting from char to integer value


hantie <hantie@hotmail.com> scribbled the following:[color=blue]
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
> news:bj6g8o$l2m$1@oravannahka.helsinki.fi...[color=green]
>> hantie <hantie@hotmail.com> scribbled the following:[color=darkred]
>> > Sorry about the top-post issue. I did not know that. I tried to reply[/color][/color]
> this[color=green][color=darkred]
>> > way, hopefully it is the right way.[/color]
>>
>> It's not. You're still top-posting.
>>
>> This is top-posting:
>> --------------------------
>> Yes.[color=darkred]
>> > Are you there?[/color]
>> --------------------------
>> This is bottom-posting:[color=darkred]
>> > Are you there?[/color]
>> Yes.
>> --------------------------[/color][/color]

(Sig snipped)
[color=blue]
> Thank you Joana. I hope I'm right this time... Sorry about that..really
> unaware about this.
> Sorry[/color]

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

--
/-- Joona Palaste (palaste@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
Joe Wright
Guest
 
Posts: n/a
#22: Nov 13 '05

re: Converting from char to integer value


Joona I Palaste wrote:[color=blue]
>[/color]
[ much snipping ]
[color=blue][color=green]
> > Thank you Joana. I hope I'm right this time... Sorry about that..really
> > unaware about this.
> > Sorry[/color]
>
> You got the hang of posting right. Now you only need to snip away my
> sig and stop calling me Joana. =)
>[/color]
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:joewwright@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Joona I Palaste
Guest
 
Posts: n/a
#23: Nov 13 '05

re: Converting from char to integer value


Joe Wright <joewwright@earthlink.net> scribbled the following:[color=blue]
> Joona I Palaste wrote:[color=green]
>>[/color]
> [ much snipping ][/color]
[color=blue][color=green][color=darkred]
>> > Thank you Joana. I hope I'm right this time... Sorry about that..really
>> > unaware about this.
>> > Sorry[/color]
>>
>> You got the hang of posting right. Now you only need to snip away my
>> sig and stop calling me Joana. =)
>>[/color]
> 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.?[/color]

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 (palaste@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
Closed Thread