开发者

Convert char array to unsigned char*

开发者 https://www.devze.com 2023-02-10 00:17 出处:网络
Is there a way to convert char[] to unsigned char*? char buf[50] = \"this is a test\"; unsigned char* conbuf = // what should I开发者_如何学Python add here

Is there a way to convert char[] to unsigned char*?

char buf[50] = "this is a test"; 
unsigned char* conbuf = // what should I开发者_如何学Python add here


Although it may not be technically 100% legal this will work reinterpret_cast<unsigned char*>(buf).


The reason this is not 100% technically legal is due to section 5.2.10 expr.reinterpret.cast bullet 7.

A pointer to an object can be explicitly converted to a pointer to an object of a different type. original type yields the original pointer value, the result of such a pointer conversion is unspecified.

Which I take to mean that *reinterpret_cast<unsigned char*>(buf) = 'a' is unspecified but *reinterpret_cast<char*>(reinterpret_cast<unsigned char*>(buf)) = 'a' is OK.


Just cast it?

unsigned char *conbuf = (unsigned char *)buf;
0

精彩评论

暂无评论...
验证码 换一张
取 消