简体   繁体   English

在未签名和已签名字符之间进行转换。 如何?

[英]Cast between unsigned and signed char. How to?

I have a question, being a student I have to use OpenSSL. 我有一个问题,作为一名学生,我必须使用OpenSSL。 My task is to calculate a RSA signature with blinding, and it uses both char and unsigned char . 我的任务是计算带有盲目性的RSA签名,它同时使用charunsigned char I said this in case anyone is wondering why I'm using these types, char and unsigned char , so intricately. 我说这是为了防止任何人想知道为什么我如此复杂地使用charunsigned char这些类型。

My question is what are the rules and good practice advice to using a cast from and to char / unsigned char applied to itself without a pointer, to vector/pointers and, why not, to matrices. 我的问题是,在不使用指针,向量/指针以及为什么不使用矩阵的情况下,将charunsigned char用于char的规则和良好实践建议是什么。

If anymore details are needed I am happy to comply. 如果需要更多详细信息,我很乐意遵守。 It goes without saying that I have searched but couldn't find a satisfying answer to anything else than casting int and unsigned int, thus if this is a repost point me in the right direction without hesitation. 不用说,我已经搜索了,但是除了强制转换int和unsigned int以外,找不到其他任何令人满意的答案,因此,如果这是重新发布,请毫不犹豫地将我指向正确的方向。

char yourChar = 'a';
unsigned char yourUChar = static_cast<unsigned char>(yourChar);

int yourInt = 1;
unsigned int yourUInt = static_cast<unsigned int>(yourInt);

A vector is just multiple ints, you'd cast each member in the vector. 向量只是多个整数,您需要将向量中的每个成员强制转换。 Same goes with a matrix. 矩阵也是如此。

If you cast a signed value that has a negative value, to an unsigned type, it will likely produce undesired behavior. 如果将具有负值的有符号值转换为无符号类型,则可能会产生不良行为。 For example, casting -1 to an unsigned int would give you the bitwise value of -1 cast to an unsigned int. 例如,将-1强制转换为无符号的int会为您提供-1的按位值,强制转换为无符号的int。 A 32-bit signed int that is -1 is 0xFFFFFFFF in hex, all 1s in binary, and casting that to unsigned would leave the bits as 1s but not you have the largest possible 32-bit unsigned int, which is 4,294,967,295. 一个-1的32位有符号整数以十六进制表示0xFFFFFFFF,二进制全为1,并将其强制转换为无符号将使这些位保留为1,但是您没有最大的32位无符号整数,即4,294,967,295。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM