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, 2022 in C++ by Nicholas
• 7,760 points
19,284 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, 2022 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, 2022 in C++ by Damon
• 4,960 points
5,603 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, 2022 in C++ by Nicholas
• 7,760 points
410 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, 2022 in C++ by Nicholas
• 7,760 points
400 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, 2022 in C++ by Nicholas
• 7,760 points
351 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, 2022 in Others by Rahul
• 9,680 points
527 views
0 votes
1 answer

outputting ascii table in C++

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

answered Jun 13, 2022 in C++ by Damon
• 4,960 points
1,040 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, 2022 in C++ by Damon
• 4,960 points
758 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, 2022 in C++ by Damon
• 4,960 points
1,093 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, 2022 in C++ by Damon
• 4,960 points
1,388 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, 2022 in C++ by Damon
• 4,960 points
1,089 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP