Sign in to follow this  

signed/unsigned char

This topic is 4223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Folks, im just learning c++, havent got too far yet but i've come upon a peculiarity with signed/unsigned char*'s. Say I have an unsigned char*: unsigned char buf1[]="José"; (unsigned because of the extended character 'é', which is ascii code 233 unless placed in a signed char in which case it is "wrapped around" to -23) I want to copy it to another buffer: unsigned char buf2[32]; strcpy(buf2,buf1); the above does not work because strcpy expects char*'s not unsigned char*'s. It will work however if cast them to char*'s i.e. strcpy((char*)buf2,(char*)buf1); is this safe? Will my extended character come out as the correct 233 acsii code or the "wrapped round" -23 code? If this will cause problems how would I store my list as unsigned char*'s and copy between them? In case anyone is wondering, the reason I need the extended characters to retain their correct ascii code is because _toupper will not work if they end up wrapped around i.e. ascii code for é is 233 when put in an unsigned char, _toupper('é') correctly capitalises the letter. When put in a signed char 'é' ends up as -23 and _toupper leaves it alone and does not capitalise it. Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by Collage
Folks, im just learning c++, havent got too far yet but i've come upon a peculiarity with signed/unsigned char*'s.

Say I have an unsigned char*:


I'd rather not.


#include <string>

std::string jose = "José";
std::string jose2 = jose; // actually copied the text! :o


To answer your question, though, it's "safe" - in the sense that it means anything to ask. The thing is that the data in memory is the same regardless; typing a variable as signed or unsigned (or for that matter, int or char, etc.) affects how those bits are *interpreted*.


unsigned char x = 200;

int* y = &x;
unsigned int* z = &x;

cout << "At " << y << " we see a signed value of " << *y << endl;
cout << "At " << z << " we see an unsigned value of " << *z << endl;


The pointers are to the same address in memory, but a different value is "found" there. It's the same bits, differently interpreted.

And yes, "char" is a different type from either "signed char" or "unsigned char", for legacy reasons. That means it might be either signed or unsigned on any given compiler, and the compiler will (or at least is supposed to) type-check, for example, if you try to assign a char to a signed char, *even if char happens to be signed on that platform*.

Share this post


Link to post
Share on other sites
probably an extended strcpy function?


unsigned char buf1[]="José";
unsigned char buf2[32];

int length = strlen(buf1);
unsigned char convert[length];
unsigned char;

T ext_strcpy (char *buf1) {

char cbuf2 [32];

for (char *iter = buf1; *iter != 0; ++i) {
if (*iter > 127) {
*convert = *iter;
++convert;
}
else {
*convert = ' ';
++convert;
}

strcpy(cbuf2,(char*)buf1);

for (int i = 0; i < length; ++i) {
buf2[i] = cbuf2[i];
}

for (int i = 0; i < length; ++i) {
if (convert[i] == ' ')
;
else {
buf2[i] = convert[i];
}
}


i think....

*yeah it took me forever to write this mock code up. i'm at work [sad]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:


unsigned char x = 200;

int* y = &x;
unsigned int* z = &x;

cout << "At " << y << " we see a signed value of " << *y << endl;
cout << "At " << z << " we see an unsigned value of " << *z << endl;



Don't do this, unless of course int and char are of the same size which I doubt that they are. You will most likely get some interesting results from this unless the compiler pads the variable, but don't count on it. It's better to stay safe than to be sorry later.

- AC

Share this post


Link to post
Share on other sites
Er, yes, wasn't thinking that one through :) I wanted to avoid having the char overload called for outputting the value, so the numeric value can be seen instead.

Revised :o


unsigned char x = 200;

signed char* y = &x;
unsigned char* z = &x;

cout << "At " << y << " we see a signed value of " << static_cast<int>(*y) << endl;
cout << "At " << z << " we see an unsigned value of " << static_cast<int>(*z) << endl;


Er... I *think* that will do it... :s

Share this post


Link to post
Share on other sites

This topic is 4223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this