Solved

Converting an unsigned short or int to a char

Posted on 2003-11-11
11
764 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
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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now