Type "char" in C++

Is there an easy way to convert between char and unsigned char if you don't know the default setting of the machine your code is running on? (On most architectures, char is signed by default and thus has a range from -128 to +127 . On some other architectures, such as ARM, char is unsigned by default and has a range from 0 to 255) I am looking for a method to select the correct signedness or to convert between the two transparently, preferably one that doesn't involve too many steps since I would need to do this for all elements in an array.

Using a pre-processor definition would allow the setting of this at the start of my code.
As would specifying an explicit form of char such as signed char or unsigned char as only char is variable between platforms.

The reason is, there are library functions I would like to use (such as strtol ) that take in char as an argument but not unsigned char .

I am looking for some advice or perhaps some pointers in the right direction as to what would be a practical efficient way to do this to make the code portable, as I intend to run the code on a few machines with different default settings for char .


I don't feel any actual issue on this point.

It's not a matter of the architecture being signed or unsigned by default. It's rather a matter of the compiler, and the default setting can be changed between the two options as you wish.

Also, there's no need to convert between the types. Both have the same representation in memory, on the same number of bits (usually 8). It's only a matter of your program and the libraries it uses to interpret the bits. If you're going to call strtol , then your data is a character array and you ought to use plain char .

If you ever use char to store not a character ( A , b , f ...) but an actual value (-1, 0, 42 ...) then the range matters. In such cases, you have to use signed char or unsigned char . However in such a case, there's little use for the libraries functions that want a char * .

For these libraries that do actually want a char * with an actual binary blob, there's no issue. Create your binary buffer with the type you prefer, signed, unsigned, or undecided, and send it, possibly with a cast. It will run perfectly.


C++ has three char types however only char is allowed to vary between compilers/architectures, as the other two are explicit version, and char is implicit, so it is allowed to default to signed or unsigned .

To make your code portable the most straightforward thing to do is explicitly to use either signed or unsigned char as you require them, however for readability you may prefer to redefine char as a the type you need, or even make your own definition of a char (for demonstration purposes I will use RLChar )

1st version - un-define char and redefine

#ifdef __arm__
#undef char
#define char signed char
#endif

2nd version - define your own custom char type to use in your code

#ifndef RLChar
#define RLChar signed char
#endif

(personally I would tend to do the second)

You can also create another macro to allow changes between the two:

#define CLAMP_VALUE_TO_255(v) ((v) > 255 ? 255 : ((v) < 0 ? 0 : (v)))

then you can use:

unsigned char clampedChar = CLAMP_VALUE_TO_255((unsigned char)pixel)

or use casts such as (these are the way to go if all the compilers you will use have the support for it):

signed char myChar = -100;
unsigned char mySecondChar;
mySecondChar = static_cast<unsigned char>(myChar); // uses a static cast 
mySecondChar = reinterpret_cast<unsigned char&>(myChar); // uses a reinterpretation cast

so for your array scenario you could do

unsigned char* RLArray;
RLArray = reinterpret_cast<unsigned char*>(originalSignedCharArray); 

Let me know if you need more info as this is just what I can remember off the top of my head, especially if you need C equivalents or more details. :)

链接地址: http://www.djcxy.com/p/17384.html

上一篇: iOS大块上传

下一篇: 在C ++中键入“char”