454,345 Members | 1,791 Online
Need help? Post your question and get tips & solutions from a community of 454,345 IT Pros & Developers. It's quick & easy.

# casting int to char

 P: n/a Is there a simple, standard statement that will reinterpret an int in the range of 0-9 as a char? I understand it's simple enough to write in a function, but I wonder if there's a more general approach. Nov 9 '06 #1
11 Replies

 P: n/a Eric wrote: Is there a simple, standard statement that will reinterpret an int in the range of 0-9 as a char? I understand it's simple enough to write in a function, but I wonder if there's a more general approach. If you add an int in the range 0 to 9 to the char '0' you are guaranteed to get the appropriate character in the range '0' to '9'. #include using namespace std; int main() { char c = '0'; for (int i = 0; i <= 9; ++i) cout << static_cast(c + i) << "\n"; } Does that answer your question? Gavin Deane Nov 9 '06 #2

 P: n/a "Eric"

 P: n/a Eric wrote in message ... >Is there a simple, standard statement that will reinterpret an int inthe range of 0-9 as a char? I understand it's simple enough to write ina function, but I wonder if there's a more general approach. In case the other answers were not what you wanted: int num( 9 ); char cnum( num ); or insure the int will fit in a char: num &= 0x7F; char cnum2( num ); or if you need to assign: cnum = static_cast( num ); Be aware that 'char' may be 'unsigned char' or 'signed char' depending on implementation/OS. You can check which is used (among other ways): #include { std::cout <<"numeric_limits::max() =" <::max())<::min() =" <::min())<::max() =" <::max())<::min() =" <::min())<

 P: n/a BobR skrev: [snip] > Be aware that 'char' may be 'unsigned char' or 'signed char' depending on implementation/OS. This answer is at best misleading. C++ has three character types: unsigned char, signed char and (plain) char. /Peter [snip] Nov 9 '06 #5

 P: n/a Gavin Deane wrote: Eric wrote: Is there a simple, standard statement that will reinterpret an int in the range of 0-9 as a char? I understand it's simple enough to write in a function, but I wonder if there's a more general approach. If you add an int in the range 0 to 9 to the char '0' you are guaranteed to get the appropriate character in the range '0' to '9'. Doesn't that depend on the character set you're using? I know ASCII has the digits in a contiguous range, and I believe EBCDIC does as well, but it is conceivable that a character set could be devised that does not have this property. Nate Nov 9 '06 #6

 P: n/a peter koch wrote in message ... >BobR skrev:[snip] >>Be aware that 'char' may be 'unsigned char' or 'signed char' depending onimplementation/OS. This answer is at best misleading. C++ has three character types:unsigned char, signed char and (plain) char./Peter Thanks for cleaning up that poor wording on my part. "I know you believe you understand what you think I said, but, I'm not sure you realize that what you heard is not what I meant!" -- Bob R POVrookie Nov 9 '06 #7

 P: n/a Nate Barney wrote: Gavin Deane wrote: Eric wrote: Is there a simple, standard statement that will reinterpret an int in the range of 0-9 as a char? I understand it's simple enough to write in a function, but I wonder if there's a more general approach. If you add an int in the range 0 to 9 to the char '0' you are guaranteed to get the appropriate character in the range '0' to '9'. Doesn't that depend on the character set you're using? I know ASCII has the digits in a contiguous range, and I believe EBCDIC does as well, but it is conceivable that a character set could be devised that does not have this property. It's required by the standard. Under section 2.2 (Character Sets) part 3: In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous. Brian Nov 9 '06 #8

 P: n/a Default User wrote: Nate Barney wrote: Gavin Deane wrote: Eric wrote: Is there a simple, standard statement that will reinterpret an int in the range of 0-9 as a char? I understand it's simple enough to write in a function, but I wonder if there's a more general approach. > If you add an int in the range 0 to 9 to the char '0' you are guaranteed to get the appropriate character in the range '0' to '9'. Doesn't that depend on the character set you're using? I know ASCII has the digits in a contiguous range, and I believe EBCDIC does as well, but it is conceivable that a character set could be devised that does not have this property. It's required by the standard. Under section 2.2 (Character Sets) part 3: In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous. Which standard are you quoting? 2.2/3 in the 1998 C++ standard doesn't say that. While I have seen enough independent sources state that this requirement is standard to be confident restating it to the OP in this thread, I don't think I have ever seen it in a standard for myself. I have wondered whether that is because C++ inherits the requirement from the C standard. Gavin Deane Nov 10 '06 #9

 P: n/a Gavin Deane wrote: > Which standard are you quoting? 2.2/3 in the 1998 C++ standard doesn't say that. The 1998 standard was replaced by the 2003 standard. -- -- Pete Roundhouse Consulting, Ltd. -- www.versatilecoding.com Author of "The Standard C++ Library Extensions: a Tutorial and Reference." For more information about this book, see www.petebecker.com/tr1book. Nov 10 '06 #10

 P: n/a Gavin Deane wrote: > Default User wrote: It's required by the standard. Under section 2.2 (Character Sets) part 3: Which standard are you quoting? 2.2/3 in the 1998 C++ standard doesn't say that. The one I have: INTERNATIONAL ISO/IEC STANDARD 14882 Second edition 2003-10-15 Programming languages - C++ Langages de programmation - C++ While I have seen enough independent sources state that this requirement is standard to be confident restating it to the OP in this thread, I don't think I have ever seen it in a standard for myself. I have wondered whether that is because C++ inherits the requirement from the C standard. I'd be rather surprised if it's not in the 1998 standard. It was in the original C standard. Brian Nov 10 '06 #11

 P: n/a Eric: Is there a simple, standard statement that will reinterpret an int in the range of 0-9 as a char? I understand it's simple enough to write in a function, but I wonder if there's a more general approach. Assuming "c" is an L-value char, and that "i" is an int: c = (assert(i>=0 && i<=9),'0'+i); In Release Mode, this will become: c = ((void)0,'0'+i); -- Frederick Gotham Nov 12 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion.