# Converting an unsigned short or int to a char

Hello,

I'm trying to write this large program and a small part of it requires me to perform bitwise operations such as masking. Unfortunately what I must work with is a character array, and I must mask it with an unsigned short array or int.

Suppose I'm working with a short.

unsigned short x = 32768; //1000000000000000 in binary
char *p;

Since short is 16 bits and char is 8 bits, i tried to shift x to the right 8 bits so...

x = x >> 8;
p = x; //this doesn't seem to work, i've tried other types of casting on this also.

otherFunction(p); //otherFunction requires that I pass in a char*

Also note I'm working with a bit mapping assignment.

Thanks!
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
> p = x; //this doesn't seem to work, i've tried other types of casting on this also.

It is not completely obvious what your intent is.  Assigning x to p does not work
because p is a pointer and x as a short has only 1/2 the number of bits required
for a pointer.  In your example, the resulting value would be (char *)(128).  Most
real OS's will not allow user-level programs to access low memory.

I will therefore assume that you want to store the high byte of the short into the
character array.  If that is the case, the syntax would be:    * p = (char)x;
Depending on any future use of x, you can likely replace both lines with:
*p = (char)(x >> 8);

0
Author Commented:
thanks for the response, i tried

unsigned short x = 32768; //1000000000000000 in binary
char *p;
*p = (char)(x >> 8);

and i got a runtime error...something about "the variable p is being used without being defined."
0
Author Commented:
also, yes you are correct. i am trying to store the high byte of the short.
0
Commented:
> and i got a runtime error...something about "the variable p is being used without being defined."

p is a pointer to an array of char, however you have not pointed it at anything, making it "uninitialized".

Either point p at an existing array:
char buffer[BUFFER_SIZE);
char *p = buffer
or allocate an array of characters:
char * p = (char *)malloc(BUFFER_SIZE);

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
to clarify my last statement,

suppose i want
unsigned short x = 32768; //1000000000000000 in binary

to store into char *p....

so i shift it over 8 bits to make
x = //0000000010000000

i want to store the lower 8 bits into my p*
0
Commented:
p is pointer to char i.e only 8 bits will appear if you point it on even 100 bit it will only c the 1st 8 bits

unsigned short x = 32768;
char * p = x;
then p will pint to the 1st 8 bits on x and it will contain only //10000000
since it is not unsgined this no will be negative

but if your write for example the value of *((unsigned short *)p) it will be 32768
0
Commented:
waelothman says:
> unsigned short x = 32768;
> char * p = x;
> then p will pint to the 1st 8 bits on x and it will contain only //10000000
> since it is not unsgined this no will be negative

This is quite incorrect.  p will  not point to the 1st 8 bits of x.  The memory address in p
remains unchanged in the above assignment (it is merely dereferenced).  The assignment
will store the low order 8 bits of x in the byte of memory pointed to by p.  And contrary
to your comment, the low order 8 bits of x is 0.  In lapchem's exmaple, p is an uninitialized
pointer, so the attempt to write through it will generate a segmentation fault.

0
Commented:
>> suppose i want unsigned short x to store into char *p....

You don't need to shift. Pointer has size of 4 bytes, and it is sufficient to hold data of 2 bytes short data type;

int nByte;
nByte = sizeof(char*); // 4 bytes
nByte = sizeof(unsigned short); // 2 bytes

otherFunction(p); //otherFunction requires that I pass in a char*
The p of otherFunction is used as character pointer or just a 4 bytes input parameter?
0
Author Commented:
to be honest i'm a c++ newbie but i think the parameter is supposed to take in a char*
0
Author Commented:
anyhow here's the test code i came up with, with the help of brettmjohnson. It seems to do what i want. in case your curious, i'm writing a simulation of a file system, which utilizes a disk driver and a simulated disk. the write_block function of the disk takes in a char* parameter, which was the otherFunction i was alluding to.

char buffer2[BUFFER_SIZE];
unsigned char buffer[BUFFER_SIZE];
unsigned char *p = buffer;
unsigned short x = 32768;
*p = (char)(x >> 8);
cout <<(unsigned short)p[0]<<endl; //1111111110000000
char *k = buffer2;
*k = (char)p[0];
cout<<(unsigned short)((unsigned char)k[0])<<endl;

if there is something logically wrong please let me know...thanks all for your generous effort.
0
Commented:
Since I don't know your intended logic, I can't say if the code fragment is
logically correct.  There is nothing obviously wrong with the code - it just
doesn't do anything very useful.  The comment is wrong, however.  Casting
an unsigned char to an unsigned short will not result in sign extension.  The
actual binary value will be 0000000010000000 (128).  I am confused as to
the goal of the second array and char pointer.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.