Public Group

signed/unsigned char

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

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 on other sites
It's safe but consider using std::string instead.

Share on other sites
It is safe to type cast them to/from signed/unsigned.

By the way, char, signed char and unsigned char are three distinct types (in Visual C++ 2005 - don't know about the others).

Share on other sites
Quote:
 Original post by CollageFolks, 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 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 = cbuf2;   }   for (int i = 0; i < length; ++i) {       if (convert == ' ')          ;       else {          buf2 = convert;       }}

i think....

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

Share on other sites
You can use memcpy ofcourse

Share on other sites
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 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

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• Forum Statistics

• Total Topics
632582
• Total Posts
3007207

×