草庐IT

unsigned-char

全部标签

c++ - 内存: "signed char *" vs "unsigned char *"的字节读取

人们经常需要一次从内存中读取一个字节,就像在这个幼稚的memcpy()中一样实现:void*memcpy(void*dest,constvoid*src,size_tn){char*from=(char*)src;char*to=(char*)dest;while(n--)*to++=*from++;returndest;}但是,我有时会看到人们明确使用unsignedchar*而不仅仅是char*.当然,char和unsignedchar可能不相等。但是我是否使用char*有区别吗?,signedchar*,或unsignedchar*什么时候按字节读/写内存?更新:实际上,我完全知

c++ - wchar_t 只是 unsigned short 的 typedef 吗?

例如,做:wchar_tx;翻译成:unsignedshortx; 最佳答案 简而言之:在C中可能在C++中没有。广泛。C将wchar_t定义为typedef,但在Unix中它通常为4个字节(因此通常不短),而在Windows2中则可能很短。在C++下,它是唯一的内置类型,如char或int,因此您可以合法地重载voidfoo(shortx)和voidfoo(wchar_tx) 关于c++-wchar_t只是unsignedshort的typedef吗?,我们在StackOverflow

c++ - wchar_t 只是 unsigned short 的 typedef 吗?

例如,做:wchar_tx;翻译成:unsignedshortx; 最佳答案 简而言之:在C中可能在C++中没有。广泛。C将wchar_t定义为typedef,但在Unix中它通常为4个字节(因此通常不短),而在Windows2中则可能很短。在C++下,它是唯一的内置类型,如char或int,因此您可以合法地重载voidfoo(shortx)和voidfoo(wchar_tx) 关于c++-wchar_t只是unsignedshort的typedef吗?,我们在StackOverflow

c++ - 为什么使用 constexpr、__PRETTY_FUNCTION__ 和 char * 的这两段代码会有不同的结果?

我有这段代码,如果你注释掉注释“但这不起作用?!”的行,它编译得很好,但如果你不这样做,编译器会产生错误。至少,gcc8.2generatesanerror.但是,他们看起来和我一模一样。有什么问题?这是法律法规吗?templatestructtest_template{staticintsize(){returnx;}};constexprintce_strlen(charconst*s){inti=0;while(s[i])++i;returni;}intjoe(){constexprintplen=ce_strlen(__PRETTY_FUNCTION__);//Thiswork

c++ - 为什么使用 constexpr、__PRETTY_FUNCTION__ 和 char * 的这两段代码会有不同的结果?

我有这段代码,如果你注释掉注释“但这不起作用?!”的行,它编译得很好,但如果你不这样做,编译器会产生错误。至少,gcc8.2generatesanerror.但是,他们看起来和我一模一样。有什么问题?这是法律法规吗?templatestructtest_template{staticintsize(){returnx;}};constexprintce_strlen(charconst*s){inti=0;while(s[i])++i;returni;}intjoe(){constexprintplen=ce_strlen(__PRETTY_FUNCTION__);//Thiswork

c++ - 在没有 reinterpret_cast 的情况下将 unsigned char 转换为 std::string 的方法?

我在std::string中有一个我需要的unsignedchar数组,但我目前的方式使用我想避免的reinterpret_cast。有没有更清洁的方法来做到这一点?unsignedcharmy_txt[]={0x52,0x5f,0x73,0x68,0x7e,0x29,0x33,0x74,0x74,0x73,0x72,0x55}unsignedintmy_txt_len=12;std::stringmy_std_string(reinterpret_cast(my_txt),my_txt_len); 最佳答案 使用迭代器构造函数:s

c++ - 在没有 reinterpret_cast 的情况下将 unsigned char 转换为 std::string 的方法?

我在std::string中有一个我需要的unsignedchar数组,但我目前的方式使用我想避免的reinterpret_cast。有没有更清洁的方法来做到这一点?unsignedcharmy_txt[]={0x52,0x5f,0x73,0x68,0x7e,0x29,0x33,0x74,0x74,0x73,0x72,0x55}unsignedintmy_txt_len=12;std::stringmy_std_string(reinterpret_cast(my_txt),my_txt_len); 最佳答案 使用迭代器构造函数:s

c++ - 为什么 C++ 允许从 int 隐式转换为 unsigned int?

考虑以下代码:voidfoo(unsignedintx){}intmain(){foo(-5);return0;}此代码编译没有问题。像这样的错误会导致很多问题并且很难找到。为什么C++允许这样的转换? 最佳答案 简短的回答是因为C最初支持此类转换,并且他们不想破坏C++中的现有软件。请注意,某些编译器会对此发出警告。例如g++-Wconversion会对该构造发出警告。在许多情况下,隐式转换很有用,例如在计算中使用int时,但最终结果永远不会是负数(从算法中知道并且可以选择断言)。编辑:其他可能的解释:请记住,最初C是一种比现在C

c++ - 为什么 C++ 允许从 int 隐式转换为 unsigned int?

考虑以下代码:voidfoo(unsignedintx){}intmain(){foo(-5);return0;}此代码编译没有问题。像这样的错误会导致很多问题并且很难找到。为什么C++允许这样的转换? 最佳答案 简短的回答是因为C最初支持此类转换,并且他们不想破坏C++中的现有软件。请注意,某些编译器会对此发出警告。例如g++-Wconversion会对该构造发出警告。在许多情况下,隐式转换很有用,例如在计算中使用int时,但最终结果永远不会是负数(从算法中知道并且可以选择断言)。编辑:其他可能的解释:请记住,最初C是一种比现在C

c++ - 从 char** 到 const char** 的隐式转换

为什么我的编译器(GCC)没有从char**隐式转换为constchar**?以下代码:#includevoidprint(constchar**thing){std::cout给出以下错误:oi.cpp:Infunction‘intmain(int,char**)’:oi.cpp:8:12:error:invalidconversionfrom‘char**’to‘constchar**’[-fpermissive]oi.cpp:3:6:error:initializingargument1of‘voidprint(constchar**)’[-fpermissive]