converting char to unsigned int
-
Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you
thepersonof wrote:
char c = 250;
thepersonof wrote:
How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way?
unsigned char c = 250;
Signed char limit is127
.
Nibu thomas Software Developer
-
Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you
thepersonof wrote:
How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way?
char c = 250; int a = (int)(unsigned int)c;
-
Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you
thepersonof wrote:
char c = 250; int a = int(c)
char c = 250; unsigned int a = (int)c; ??
-
Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you
Make c an unsigned char. (Yes, there are such a thing as signed and unsigned chars in C). Or cast c to an unsigned char. So: 1. unsigned char c = 250; Or: 2. int a = (int) (unsigned char) c; Or both... -- modified at 7:49 Thursday 6th April, 2006 OR: int a = 250; :laugh:
-
Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you
do you want the value of a to be 250 in this case? We Believe in Excellence www.aqueelmirza.cjb.net
-
do you want the value of a to be 250 in this case? We Believe in Excellence www.aqueelmirza.cjb.net
Thankyou ... converted to an unsigned char and it works great
-
Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you
union { char c[sizeof (int)]; int n; } Convert; char c = 250; Convert.n = 0; Convert.c[0] = c; Then, in Convert.n you will get the proper value. Good luck! William
-
Thankyou ... converted to an unsigned char and it works great
Yah i misunderstood. They are right. We Believe in Excellence www.aqueelmirza.cjb.net
-
union { char c[sizeof (int)]; int n; } Convert; char c = 250; Convert.n = 0; Convert.c[0] = c; Then, in Convert.n you will get the proper value. Good luck! William
Geez :omg: That's a really complicated way of doing that !
-
union { char c[sizeof (int)]; int n; } Convert; char c = 250; Convert.n = 0; Convert.c[0] = c; Then, in Convert.n you will get the proper value. Good luck! William
Chaa gia hai bhai! We Believe in Excellence www.aqueelmirza.cjb.net
-
Chaa gia hai bhai! We Believe in Excellence www.aqueelmirza.cjb.net
Kya cha gaya hai Yar
-
Kya cha gaya hai Yar
hahahaha What a solution yaar! Nice Unique and innovative :-D We Believe in Excellence www.aqueelmirza.cjb.net
-
hahahaha What a solution yaar! Nice Unique and innovative :-D We Believe in Excellence www.aqueelmirza.cjb.net
-
i said "Chaa gia hai bhai" which means "Great job! brother!". This is Urdu language idiom. It is used for a person who does something great. :) We Believe in Excellence www.aqueelmirza.cjb.net
-
Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you
thepersonof wrote:
int a = int(c)
A cast is actually not necessary. You could accomplish the same with:
int a = c;
because a
char
gets internally promoted to anint
during such operations. Achar
can hold values in the range -128 to 127, whereas anunsigned char
can hold values in the range 0 to 255.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you
Hi there. As the other replies have stated, you need to use the unsigned char for values above 127. To perform explicit conversions in C++, you should use the static_cast instead of the old C-Style casts.
unsigned char c = 250; int a = static_cast<int>(c);
Cheers -
Hi there. As the other replies have stated, you need to use the unsigned char for values above 127. To perform explicit conversions in C++, you should use the static_cast instead of the old C-Style casts.
unsigned char c = 250; int a = static_cast<int>(c);
Cheersabbiyr wrote:
To perform explicit conversions in C++, you should use the static_cast instead of the old C-Style casts.
For integral types, this is not necessary.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
abbiyr wrote:
To perform explicit conversions in C++, you should use the static_cast instead of the old C-Style casts.
For integral types, this is not necessary.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb