473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

casting int to char

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 3499

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 <iostream>
using namespace std;

int main()
{
char c = '0';
for (int i = 0; i <= 9; ++i)
cout << static_cast<char>(c + i) << "\n";
}

Does that answer your question?

Gavin Deane

Nov 9 '06 #2
"Eric" <no@thanks.comwrote in message
news:ei**********@geraldo.cc.utexas.edu...
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.
A character is number (are they called scalars? I forget) and as such you
can do math on them.

I take it you have a number such as 0 and you want to make this become the
character '0'. This is fairly simple since they are both types of numbers,
you can just add them. If you add 0 to the character '0' you get the value
for the character '0'. Add 1 and you get the value for the character '1',
etc...

So,

int MyNumber = 5;
char MyChar = '0' + MyNumber;
MyChar will now contain the character '5'.

If you are trying to do something else plese explain.
Nov 9 '06 #3

Eric wrote in message ...
>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.
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<char>( 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 <limits>
{
std::cout <<"numeric_limits<char>::max() ="
<<int(std::numeric_limits<char>::max())<<std::endl ;
std::cout <<"numeric_limits<char>::min() ="
<<int(std::numeric_limits<char>::min())<<std::endl ;

std::cout <<"numeric_limits<unsigned char>::max() ="
<<int(std::numeric_limits<unsigned char>::max())<<std::endl;
std::cout <<"numeric_limits<unsigned char>::min() ="
<<int(std::numeric_limits<unsigned char>::min())<<std::endl;
}

If the outputs match, 'char' == 'unsigned char'.

[ if you knew all this already, sorry. Maybe it might help some newbie.]
--
Bob R
POVrookie
Nov 9 '06 #4

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
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

peter koch wrote in message ...
>
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
19
by: Ramesh Tharma | last post by:
Hi, Is any one knows what's wrong with the following code, I was told that it will compile and run but it will crash for some values. Assume that variables are initilized. char* c; long*...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
2
by: Alex Vinokur | last post by:
classes that have virtual methods hold pointer to virtual table as additional implicit data member. So, sizeof of such classes is sizeof of all data (as struct-POD) plus 4. It seems that use of...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
24
by: Francine.Neary | last post by:
I've read that you should always cast the argument you pass to isupper(), isalnum(), etc. to unsigned char, even though their signature is int is...(int). This confuses me, for the following...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.