Type Conversion Problem.

Hello To ALL...

This Is my textbook Question I am facing problem with...

Write a C++ program that reads any given character code(a positive integer) from the keyboard and displays the corresponding character and the character code as decimal, an octal,and a hexadecimal on screen.
Why do you think the character P is output when the number 336 is entered?


Here is the program I made as solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;

int main()
{
 int code;   
 char ch;
 
 cout<<"Enter a character code:";
 cin>>code;
 ch=code;
 
 cout<<"The Character for the entered" 
        " code is:"<<ch<<endl<<endl;
 
        code=ch;
 
 cout<<"The character code in"
       "\nDecimal    :"<<dec<<code
     <<"\nOctal      :"<<oct<<code
     <<"\nHexadecimal:"<<hex<<code;
 
 cin.sync();
 cin.get();
 cin.sync();                          
 
 return 0;
}


And the reason I thought for output P when number 336 is enterd is that I am trying to store number 336 in variable ch which is of type char, which stores only 8 bits(1 byte format range 0 to 255) but the binary equivqlent of 336 is 101010000 (that is 9 bits)
so only the first 8 bits are stored (HSB 1 is ignored) which gives 01010000(=80 in decimal) Which represents the character P...

But the book says:
When Entering 336, the value 80 is stored in the low byte of variable code
(336=256+80).Thus after the assignment,the variable ch contains the value 80,representing the character P.

I am confused as what the Answer mentioned in the book means... can someone please help me with explaining the reason mentioned in book and tell me if the reason I figured out is WRONG or what???

There is no error with program it works fine but it is very very slightly different from the program given in solution of my book...

My Sincere regards and thanks to all of you trying to help me out...

Forgive me for my mistakes because this is my FIRST POST ON THE FORUM....
Both you and the book are saying the same thing.
Code is an integer. When you put 336 in it, it looks like this:
00000000 | 00000000 | 00000001 | 01010000
|s show where each of the 4 bytes ends. Low byte is the one which holds the lest significant digits - that is, the first one.
i cant understand what will you do but char dont store 0 to 255 it store -127 to -127 !! unsigned char store 0 to 255
and of course this is not a good idea
ch = code; i dont know why you want use this !! if you want you must use static_cast and if you want share the memory you can use union to share one location with several variable
char dont store 0 to 255 it store -127 to -127
That's a fair point. It makes no difference here though..

and of course this is not a good idea
It's quite alright.

you must use static_cast
no.
Last edited on
closed account (1vRz3TCk)
char dont store 0 to 255 it store -127 to -127 !!
that is a small range.

but also the standard does not say if a plain char is signed or not:

Plain char, signed char, and unsigned char are three distinct types. A char, a
signed char, and an unsigned char occupy the same amount of storage and
have the same alignment requirements (3.9); that is, they have the same
object representation. For character types, all bits of the object representation
participate in the value representation. For unsigned character types, all possible
bit patterns of the value representation represent numbers. These requirements
do not hold for other types. In any particular implementation, a plain char object
can take on either the same values as a signed char or an unsigned char; which
one is implementation-defined.
Last edited on
First My Sincere thanks to all for giving your precious time,helping me and sharing the knowledge...

The range of char is not -127 to -127 it is -127 to 127...
thanks for reminding me...

well that was the only difference between my program and the solution of the book in the book all the variables were declared as unsigned(I was wandering why??)..

hamsterman i got your explianation... the low bytes are copied first and then the higher ones but here there is no space for it to be copied right?? thank you for explaining it sir...
but int is stored in two bytes as i am using dev C++ which runs program in the DOS which is a 16 bit OS... so int is 2 bytes on 16 bit OS(depends on the enviorment right??) this is what i was told by my 'C' tutor and also given in my book...

i do have another question...

when i use the variable ch instead of using variable code in these lines of code:

cout<<"The character code in"
"\nDecimal :"<<dec<<ch(here!)
<<"\nOctal :"<<oct<<ch(here!)
<<"\nHexadecimal:"<<hex<<ch(here!);

i get the following output:

Enter a charactyer code:336
The character for enterd code is:P

The character code in
Decimal :P(should be 80 here)
Octal :P(120 here)
Hexadecimal:P(50 here)

why does this thing happens i mean that the value stored in both the variables is same(10100000)=80.. so i should get the same output

I dont know what is static_cast but i have used typecast operator sometimes in 'C' language..
i did this way because this way was tought in this chapter of the book(or i should say this is the only way explained in the book upto the point i have read) i am at chapter 4 of the book...

codemonkey, i did not understand anything you said i think its some kind of advanced thing you are talking about... and if it is not please take a moment to explain it again properly...
closed account (1vRz3TCk)
codemonkey, i did not understand anything you said i think its some kind of advanced thing
you are talking about...
The first bit about the small range (-127 to -127) possibly a typo
on ahura24s part but still the signed char range is -128 to +127.

The quote is from the standard and basicaly say that char, signed char and
unsigned char are three types i.e. char and signed char are not the same type. It also says
that it is up to the implementers of the compiler as to the range of the char (-128,127) or (0,255).
Last edited on
About the output.
ostream operator << acts differently when it's argument is int, char, char*, etc.
This is known as function overloading. Try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

void foo( int arg ){
   std::cout << "an int was passed\n";
}

void foo( char arg ){
   std::cout << "a char was passed\n";
}

int main(){
   int a;
   char b;
   foo(a);
   foo(b);
   std::cin.get();
}
Same idea in iostream is applied to operator << (and >>).
Last edited on
I got it...
I am REaly thankfull to all...
THanks once again... I feel SO happy my doubts are cleared....THAnks...
Topic archived. No new replies allowed.