casting int to char using C style casting duplicate

0 votes

In traditional C you can do:

int i = 48;
char c = (char)i;
//Now c holds the value of 48. 
//(Of course if i > 255 then c will not hold the same value as i).  

Which of the c++ casting methods (static_cast, reinterpret_cast) is suited for getting this job done?

Jun 13 in C++ by Nicholas
• 6,240 points
157 views

1 answer to this question.

0 votes

Even though the precision is lost, you can implicitly convert between numerical types:

char c = i;

However, to avoid potentially lossy conversions like these, you might want to activate compiler warnings. 

If that's the case, the conversion should be done with static cast.

The rest of the cast includes:

dynamic cast can only convert between pointers or references to polymorphic class types; const cast can only modify const or volatile qualifiers; and reinterpret cast can only convert between pointers or references to wholly unrelated types in specific circumstances. It will not, for example, perform numeric conversions.

Static cast, const cast, and reinterpret cast are used in a combination of C-style and function-style casts to get the task done.

answered Jun 13 by Damon
• 4,960 points

Related Questions In C++

0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20 in C++ by Damon
• 4,960 points
323 views
0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1 in C++ by Nicholas
• 6,240 points
26 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 5 in C++ by Nicholas
• 6,240 points
34 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 7 in C++ by Nicholas
• 6,240 points
19 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10 in Others by Rahul
• 9,240 points
49 views
0 votes
1 answer

outputting ascii table in C++

This line doesn't do the right thing: ch ...READ MORE

answered Jun 13 in C++ by Damon
• 4,960 points
53 views
0 votes
1 answer

What range of values can integer types store in C++

You may rely on the following minimal ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
38 views
0 votes
0 answers

C++ convert from 1 char to string?

I simply need to cast one character to string.  The inverse method is as easy as str[0]. The following suggestions did not work for me: char c = 34; string(1,c); //this doesn't work, the ...READ MORE

Jun 29 in C++ by Damon
• 4,960 points
31 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
163 views
0 votes
1 answer

Easiest way to convert int to string in C++

std::stoi (and variations for each numeric type) ...READ MORE

answered Jun 15 in C++ by Damon
• 4,960 points
86 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP