且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从符号的字符转换为unsigned char型,然后再返回?

更新时间:2023-09-30 13:39:52

这也是为什么C ++引入的新投的风格,其中包括的static_cast 和的原因之一 reinter pret_cast

This is one of the reasons why C++ introduced the new cast style, which includes static_cast and reinterpret_cast

有就是你可能意味着两件事情说转换,从符号到无符号,你可能会说你希望的无符号变量包含已签署的可变模的值(你的无符号类型+ 1的最大值)。也就是说,如果你的符号字符有-128那么 MAX_CHAR + 1 添加了对值为128,如果它有值-1,则 MAX_CHAR + 1 添加了对值为255,这是什么的static_cast完成。在另一方面,你可能意味着无论再presentation符号整数的系统上使用国米preT一些变量引用是PTED为一个无符号字节间$ P $内存的位值,也就是说,如果有位值 0b10000000 应该评估为价值128和255位值 0b11111111 ,这是完成与reinter pret_cast。

There's two things you can mean by saying conversion from signed to unsigned, you might mean that you wish the unsigned variable to contain the value of the signed variable modulo (the maximum value of your unsigned type + 1). That is if your signed char has a value of -128 then MAX_CHAR+1 is added for a value of 128 and if it has a value of -1, then MAX_CHAR+1 is added for a value of 255, this is what is done by static_cast. On the other hand you might mean to interpret the bit value of the memory referenced by some variable to be interpreted as an unsigned byte, regardless of the signed integer representation used on the system, i.e. if it has bit value 0b10000000 it should evaluate to value 128, and 255 for bit value 0b11111111, this is accomplished with reinterpret_cast.

现在,两个补重presentation发生这种情况是完全一样的东西,因为-128重新psented为 0b10000000 $ P $和-1是psented为 0b11111111 重新$ p $,同样为所有在两者之间。但是,其他计算机(通常较旧的架构)可以使用不同的重新签订presentation如标志和幅度或一补数。在一补数的 0b10000000 bitvalue不会是-128,-127,但是,这样一个静态强制转换为无符号的字符将使这个129,而reinter pret_cast会使这个128.另外在一补数的 0b11111111 bitvalue不会是-1,但-0,(是这个值存在于一补数,)和将被转换为0与的static_cast的值,但是255与reinter pret_cast的值。注意,在一个符号的字符psented一补数的128的无符号的值实际上不被重新$ P $的情况下,由于它的范围从-127到127,由于-0值

Now, for the two's complement representation this happens to be exactly the same thing, since -128 is represented as 0b10000000 and -1 is represented as 0b11111111 and likewise for all in between. However other computers (usually older architectures) may use different signed representation such as sign-and-magnitude or ones' complement. In ones' complement the 0b10000000 bitvalue would not be -128, but -127, so a static cast to unsigned char would make this 129, while a reinterpret_cast would make this 128. Additionally in ones' complement the 0b11111111 bitvalue would not be -1, but -0, (yes this value exists in ones' complement,) and would be converted to a value of 0 with a static_cast, but a value of 255 with a reinterpret_cast. Note that in the case of ones' complement the unsigned value of 128 can actually not be represented in a signed char, since it ranges from -127 to 127, due to the -0 value.

我不得不说,绝大多数的电脑将使用补做的任何地方你的code将永远运行整个问题没有实际意义。您可能会只看到过比补之外的任何系统很老的架构,认为60年代的时间表。

I have to say that the vast majority of computers will be using two's complement making the whole issue moot for just about anywhere your code will ever run. You will likely only ever see systems with anything other than two's complement in very old architectures, think '60s timeframe.

语法归结为以下几点:

signed char x = -100;
unsigned char y;

y = (unsigned char)x;                    // C static
y = *(unsigned char*)(&x);               // C reinterpret
y = static_cast<unsigned char>(x);       // C++ static
y = reinterpret_cast<unsigned char&>(x); // C++ reinterpret

要在一个不错的C ++的方式与数组做到这一点:

To do this in a nice C++ way with arrays:

jbyte memory_buffer[nr_pixels];
unsigned char* pixels = reinterpret_cast<unsigned char*>(memory_buffer);

或Ç方式:

unsigned char* pixels = (unsigned char*)memory_buffer;