Author |
Message |
qazm #1 / 23
|
 char --->int
char c = '0x12' ; int i = ???? I want i to be assigned the number 0x12. How do you do that? Should I use atoi()?
|
Tue, 30 Nov 2004 13:37:19 GMT |
|
 |
Joona I Palast #2 / 23
|
 char --->int
Quote:
>> char c = '0x12' ; >> int i = ???? >> I want i to be assigned the number 0x12. How do you do that? Should I use atoi()? > You cannot assign '0x12' to a char, because '0x12' is four characters. > But you don't need your char variable anyway. To assign 0x12 to an int > variable, use: > int i = 0x12;
More on this topic: If you meant, how can you change the character whose code is 0x12 to an int, you can just assign it: char c = 0x12; /* or char c = '\x12'; */ int i = c; If you meant, how can you change the string "0x12" into an int value, you can use strtol: char *c = "0x12"; int i = strtol(c, NULL, 16); --
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ | \----------------------------------------- Finland rules! ------------/ "I am lying." - Anon
|
Tue, 30 Nov 2004 14:36:52 GMT |
|
 |
Zoran Cutur #3 / 23
|
 char --->int
Quote: > char c = '0x12' ; > int i = ???? > I want i to be assigned the number 0x12. How do you do that? Should I use
atoi()? You are mixing up things! '0x12' isn't a string. It isn't a literal character either. You probably meant char *c="0x12"? In this case you can use atoi(), but even better would be strtol() to convert the string into an integer value. --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Tue, 30 Nov 2004 14:45:07 GMT |
|
 |
Martin Ambuh #4 / 23
|
 char --->int
Quote:
> char c = '0x12' ; > int i = ???? > I want i to be assigned the number 0x12. How do you do that?
i = c; Quote: > Should I use atoi()?
The variable c is not a string: it has the numeric value 0x12. atoi() is not only pointless, but is meaningless when called for anything not a string.
|
Tue, 30 Nov 2004 15:42:13 GMT |
|
 |
Richard B #5 / 23
|
 char --->int
Quote:
> char c = '0x12' ; > int i = ???? > I want i to be assigned the number 0x12. How do you do that? Should I use atoi()?
This'll do it: int i=0x12; Your char initialisation, however, is broken. It assigns something to c whose value is implementation-defined. You probably meant either char c='\x12'; or char c=0x12; which are equivalent, and both assign the number 0x12, alias 18, to c. Richard
|
Tue, 30 Nov 2004 16:40:22 GMT |
|
 |
CBFalcone #6 / 23
|
 char --->int
Quote:
> > char c = '0x12' ; > > int i = ???? > > I want i to be assigned the number 0x12. How do you do that? > > Should I use atoi()? > You are mixing up things! '0x12' isn't a string. It isn't a > literal character either. You probably meant char *c="0x12"? > In this case you can use atoi(), but even better would be > strtol() to convert the string into an integer value.
Harrumph, I would expect atoi to return zero from that string. --
Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address!
|
Tue, 30 Nov 2004 19:56:46 GMT |
|
 |
Thomas Stege #7 / 23
|
 char --->int
Quote:
>>char c = '0x12' ; >>int i = ???? >>I want i to be assigned the number 0x12. How do you do that? Should I use atoi()? > You cannot assign '0x12' to a char...
You can, but the result is implementation defined. -- Thomas Stegen http://www.geocities.com/thinkoidz
|
Tue, 30 Nov 2004 21:07:35 GMT |
|
 |
Richard B #8 / 23
|
 char --->int
Quote:
> > char c = '0x12' ; > > int i = ???? > > I want i to be assigned the number 0x12. How do you do that? Should I use atoi()? > You cannot assign '0x12' to a char, because '0x12' is four characters.
Yes, you can, and no, it isn't. "0x12" is four characters, plus a null terminator. '0x12' is _one_ character whose value is implementation- dependent. IOW, you can assign '0x12' to a char variable, but ISO C doesn't tell you anything about which character it will turn out to be. Richard
|
Tue, 30 Nov 2004 20:09:53 GMT |
|
 |
Dan P #9 / 23
|
 char --->int
Quote: >> You are mixing up things! '0x12' isn't a string. It isn't a >> literal character either. ^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^ Why? Chapter and verse, please. Dan -- Dan Pop DESY Zeuthen, RZ group
|
Tue, 30 Nov 2004 23:33:06 GMT |
|
 |
Dave Vandervi #10 / 23
|
 char --->int
Quote:
>> You cannot assign '0x12' to a char, because '0x12' is four characters. >Yes, you can, and no, it isn't. "0x12" is four characters, plus a null >terminator. '0x12' is _one_ character whose value is implementation- >dependent.
Isn't it an int? dave --
Because C needs to know how long the array elements are, otherwise pointer arithmetic goes all woozy (don't you just love a precise, detailed technical term?). --Joona I Palaste in comp.lang.c
|
Wed, 01 Dec 2004 02:26:45 GMT |
|
 |
Zoran Cutur #11 / 23
|
 char --->int
Quote:
>>> You are mixing up things! '0x12' isn't a string. It isn't a >>> literal character either. ^^^^^^^^^^ > ^^^^^^^^^^^^^^^^^^^^^^^^ > Why? Chapter and verse, please.
Which literal character is it? --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Fri, 03 Dec 2004 17:19:15 GMT |
|
 |
Zoran Cutur #12 / 23
|
 char --->int
Quote:
>> > char c = '0x12' ; >> > int i = ???? >> > I want i to be assigned the number 0x12. How do you do that? >> > Should I use atoi()? >> You are mixing up things! '0x12' isn't a string. It isn't a >> literal character either. You probably meant char *c="0x12"? >> In this case you can use atoi(), but even better would be >> strtol() to convert the string into an integer value. > Harrumph, I would expect atoi to return zero from that string.
Ahh, I see, I thought atoi() was equivalent to strtol with base 0 (which would mean octal, decimal, or hexadecimal) but it acts as with base 10. My fault. Actually I never really used atoi on my own (something close to an excuse but it really isn't). --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Fri, 03 Dec 2004 17:28:23 GMT |
|
 |
Richard B #13 / 23
|
 char --->int
Quote:
> >> You cannot assign '0x12' to a char, because '0x12' is four characters. > >Yes, you can, and no, it isn't. "0x12" is four characters, plus a null > >terminator. '0x12' is _one_ character whose value is implementation- > >dependent. > Isn't it an int?
Erm, yes, of course you're right. Worse, it could be an int that isn't a valid char value. You can still assign it to a char, however, although its value is still (even more!) implementation-dependent. Richard
|
Fri, 03 Dec 2004 18:30:38 GMT |
|
 |
Thomas Stege #14 / 23
|
 char --->int
Quote:
>>>>You are mixing up things! '0x12' isn't a string. It isn't a >>>>literal character either. ^^^^^^^^^^ >> ^^^^^^^^^^^^^^^^^^^^^^^^ >>Why? Chapter and verse, please. > Which literal character is it?
An implementation defined one. -- Thomas Stegen http://www.geocities.com/thinkoidz
|
Fri, 03 Dec 2004 20:22:48 GMT |
|
|