17

I have a variable unsigned char that contains a value, 40 for example. I want a int variable to get that value. What's the simplest and most efficient way to do that? Thank you very much.

1
  • 2
    unsigned char is essentially a one byte of memory interpreted by the computer as an integer it is from 0 to 255. An integer type is usually 4 bytes with range -2147483648 to 2147483647. Conversion usually involves assignments from one value to another. unsigned char to integer assignment is no problem, but the other way around will have over flow problems at the high end. And it not meaning full to convert negative number to unsigned char. Commented Dec 9, 2018 at 4:34

6 Answers 6

21
unsigned char c = 40; int i = c; 

Presumably there must be more to your question than that...

Sign up to request clarification or add additional context in comments.

2 Comments

if I write printf("c: %x", c) I get 32. Then i write int i=c and when I print "i" I get 50!
@user1346664: %x prints in hexadecimal. 32 hex is equal to 50 decimal. Use %d instead.
7

Try one of the followings, it works for me. If you need more specific cast, you can check Boost's lexical_cast and reinterpret_cast.

unsigned char c = 40; int i = static_cast<int>(c); std::cout << i << std::endl; 

or:

unsigned char c = 40; int i = (int)(c); std::cout << i << std::endl; 

1 Comment

Prefer C++ constructor style int(c) over C-cast style: (int)(c)
3

Google is a useful tool usually, but the answer is incredibly simple:

unsigned char a = 'A' int b = a 

Comments

2

Actually, this is an implicit cast. That means that your value is automatically casted as it doesn't overflow or underflow.

This is an example:

unsigned char a = 'A'; doSomething(a); // Implicit cast double b = 3.14; doSomething((int)b); // Explicit cast neccesary! void doSomething(int x) { ... } 

3 Comments

Why do you need an explicit cast for double->int? Won't it just truncate implicitly?
Yes it does, but you'll get a compiler warning, so it is recommended to do that.
Odd, I don't get any warnings on the highest level of GCC.
2

Depends on what you want to do:

to read the value as an ascii code, you can write

char a = 'a'; int ia = (int)a; /* note that the int cast is not necessary -- int ia = a would suffice */ 

to convert the character '0' -> 0, '1' -> 1, etc, you can write

char a = '4'; int ia = a - '0'; /* check here if ia is bounded by 0 and 9 */ 

Comments

-1
char *a="40"; int i= a; 

The value in 'a' will be the ASCII value of 40 (won't be 40).

Instead try using strtol() function defined in stdlib.h

Just be careful because this function is for string. It won't work for character

3 Comments

The value of a (and of i) will be 40, as that is the value you just assigned.
If you ignore the compiler warning you'll discover that i gets the address where the string "40" is stored, which is of course what 'a' has in it. Ah - brings back to good ol' days before that wimpy ANSI standard thing - back when C was weakly typed, programmers had stronger minds, and Coca-Cola was made with real sugar! Bah! Kids these days..! Bah!!
The question was about unsigned char. Why are you using a char*?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.