在C 中使用uint8_t
over有什么好处unsigned char
?
我知道几乎每个系统uint8_t
都只是一个typedef unsigned char
,为什么要用呢?
不时有人在SO上指出char
(也就是"字节")不一定是8位.
似乎8位char
几乎是通用的.我原本认为,对于主流平台,必须有一个8位char
才能确保其在市场上的可行性.
现在和历史上,哪些平台使用的char
不是8位,为什么它们与"普通"8位不同?
在编写代码时,考虑跨平台支持(例如,对于通用库而言),对于非8位平台,值得考虑的是什么char
?
在过去,我遇到过一些char
16位的ADI DSP .我认为DSP是一种利基架构.(然后,当时手工编写的汇编程序很容易击败可用的C编译器可以做的事情,所以我在该平台上没有真正获得C的经验.)
所以我知道有符号和无符号int之间的区别在于,有一些用于表示数字是正数还是负数,但这如何适用于char?角色怎样才能是积极的还是消极的?
如果存在,是否包含头文件?
此代码提供编译错误:
#include <iostream>
using namespace std;
int main()
{
byte b = 2;
cout << b << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 下面的代码编译,但char类型的行为与int类型的行为不同.
特别是
cout << getIsTrue< isX<int8>::ikIsX >() << endl;
cout << getIsTrue< isX<uint8>::ikIsX >() << endl;
cout << getIsTrue< isX<char>::ikIsX >() << endl;
Run Code Online (Sandbox Code Playgroud)
导致三种类型的模板的3个实例化:int8,uint8和char.是什么赋予了?
对于ints来说也是如此:int和uint32导致相同的模板实例化,而signed int则是另一个.
原因似乎是C++将char,signed char和unsigned char视为三种不同的类型.而int与signed int相同.这是对的还是我错过了什么?
#include <iostream>
using namespace std;
typedef signed char int8;
typedef unsigned char uint8;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed long long int64;
typedef unsigned long long uint64;
struct TrueType {};
struct FalseType {};
template <typename T>
struct isX …
Run Code Online (Sandbox Code Playgroud) 将可变长度十六进制字符串转换为"01A1"
包含该数据的字节数组的最佳方法是什么.
即转换这个:
std::string = "01A1";
Run Code Online (Sandbox Code Playgroud)
进入这个
char* hexArray;
int hexLength;
Run Code Online (Sandbox Code Playgroud)
或这个
std::vector<char> hexArray;
Run Code Online (Sandbox Code Playgroud)
所以当我把hexdump -C
它写入文件时,我得到包含的二进制数据01A1
.
std::byte
是C++ 17中的新类型,它是作为enum class byte : unsigned char
.如果没有适当的转换,这将无法使用它.所以,我为这种类型的向量做了一个别名来表示一个字节数组:
using Bytes = std::vector<std::byte>;
Run Code Online (Sandbox Code Playgroud)
但是,它不可能在旧式中使用它:接受它作为参数的函数失败,因为这种类型不能轻易转换为旧std::vector<unsigned char>
类型,例如,zipper
库的用法:
/resourcecache/pakfile.cpp: In member function 'utils::Bytes resourcecache::PakFile::readFile(const string&)':
/resourcecache/pakfile.cpp:48:52: error: no matching function for call to 'zipper::Unzipper::extractEntryToMemory(const string&, utils::Bytes&)'
unzipper_->extractEntryToMemory(fileName, bytes);
^
In file included from /resourcecache/pakfile.hpp:13:0,
from /resourcecache/pakfile.cpp:1:
/projects/linux/../../thirdparty/zipper/zipper/unzipper.h:31:10: note: candidate: bool zipper::Unzipper::extractEntryToMemory(const string&, std::vector<unsigned char>&)
bool extractEntryToMemory(const std::string& name, std::vector<unsigned char>& vec);
^~~~~~~~~~~~~~~~~~~~
/projects/linux/../../thirdparty/zipper/zipper/unzipper.h:31:10: note: no known conversion for argument 2 from 'utils::Bytes {aka std::vector<std::byte>}' to 'std::vector<unsigned char>&'
Run Code Online (Sandbox Code Playgroud)
我试图表演天真的演员,但这也没有帮助.那么,如果它被设计为有用,它在旧的上下文中是否真的有用?我看到的唯一方法是 …
这段代码中是否存在严格别名规则违规?我认为int
->char
和int
->std::byte
都可以,但是呢int8_t
?
int main() {
int arr[8] = {1, 1, 1, 1, 1, 1, 1, 1}; // Little endian: 10000000 | 10000000 ....
int8_t *ptr = (int8_t*)arr; // Do we have a violation of the Strict Aliasing rule in this cast?
ptr += 3; // 100[0]0000 ....
cout << (int)*ptr << endl; // outputs 0
return 1;
}
Run Code Online (Sandbox Code Playgroud)