Solved

Converting an unsigned short or int to a char

Posted on 2003-11-11
11
769 Views
Last Modified: 2010-04-01
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!
0
Comment
Question by:lapchern
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9727831
> 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 Comment

by:lapchern
ID: 9727876
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 Comment

by:lapchern
ID: 9727899
also, yes you are correct. i am trying to store the high byte of the short.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Accepted Solution

by:
brettmjohnson earned 70 total points
ID: 9727973
> 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
 

Author Comment

by:lapchern
ID: 9728000
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
 
LVL 5

Expert Comment

by:waelothman
ID: 9728131
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

this mean if you made
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9728208
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
 
LVL 3

Expert Comment

by:freewell
ID: 9728272
>> 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 Comment

by:lapchern
ID: 9729134
to be honest i'm a c++ newbie but i think the parameter is supposed to take in a char*
0
 

Author Comment

by:lapchern
ID: 9729186
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9733575
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question