相关疑难解决方法(0)

uint8_t vs unsigned char

在C 中使用uint8_tover有什么好处unsigned char

我知道几乎每个系统uint8_t都只是一个typedef unsigned char,为什么要用呢?

c typedef

218
推荐指数
6
解决办法
28万
查看次数

什么平台有8位字符以外的东西?

不时有人在SO上指出char(也就是"字节")不一定是8位.

似乎8位char几乎是通用的.我原本认为,对于主流平台,必须有一个8位char才能确保其在市场上的可行性.

现在和历史上,哪些平台使用的char不是8位,为什么它们与"普通"8位不同?

在编写代码时,考虑跨平台支持(例如,对于通用库而言),对于非8位平台,值得考虑的是什么char

在过去,我遇到过一些char16位的ADI DSP .我认为DSP是一种利基架构.(然后,当时手工编写的汇编程序很容易击败可用的C编译器可以做的事情,所以我在该平台上没有真正获得C的经验.)

c c++ cross-platform

133
推荐指数
8
解决办法
2万
查看次数

有符号/无符号字符之间的区别

所以我知道有符号和无符号int之间的区别在于,有一些用于表示数字是正数还是负数,但这如何适用于char?角色怎样才能是积极的还是消极的?

c types

130
推荐指数
5
解决办法
23万
查看次数

C++中是否存在'byte'数据类型?

如果存在,是否包含头文件?

此代码提供编译错误:

#include <iostream>

using namespace std;

int main()
{
    byte b = 2;

    cout << b << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

73
推荐指数
5
解决办法
17万
查看次数

char!=(signed char),char!=(unsigned char)

下面的代码编译,但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)

c++ char

40
推荐指数
4
解决办法
2万
查看次数

将十六进制字符串转换为字节数组

将可变长度十六进制字符串转换为"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.

c++ string hex

35
推荐指数
7
解决办法
6万
查看次数

如何在需要旧式unsigned char的地方使用新的std :: byte类型?

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)

我试图表演天真的演员,但这也没有帮助.那么,如果它被设计为有用,它在旧的上下文中是否真的有用?我看到的唯一方法是 …

c++ vector c++17

31
推荐指数
1
解决办法
1万
查看次数

std :: byte指针是否与char*具有相同的别名含义?

C++(和C)严格别名规则包括a char*unsigned char*可以别名任何其他指针.

AFAIK没有类似的规则uint8_t*.

因此我的问题是:std::byte指针的别名规则是什么?

C++参考目前只是指定:

与字符类型(char,unsigned char,signed char)一样,它可用于访问其他对象占用的原始内存(对象表示),但与这些类型不同,它不是字符类型,也不是算术类型.

c++ strict-aliasing c++17

27
推荐指数
1
解决办法
1213
查看次数

std :: byte的目的是什么?

现在c ++ 17了std::byte,我正在寻找一种方法将读取文件的char代码转换为读取文件的代码byte.一个文件包含字节,而不是一堆整数.

然后我读到了这个问题另一个问题,其中人们认为阅读文件byte是错误的,并且阅读文件char是正确的.

如果byte不是为访问内存而设计的,并且通过类比,文件,那么它的目的是什么?正如其他两个问题所引述:

与char和unsigned char一样,它可以用于访问其他对象占用的原始内存(对象表示),但与这些类型不同,它不是字符类型,也不是算术类型.字节只是一个位集合,只为它定义了按位逻辑运算符.

这听起来像应该用于读取文件的确切类型,而不是字符.

c++ byte c++17

23
推荐指数
1
解决办法
2111
查看次数

我们是否违反了严格别名规则?

这段代码中是否存在严格别名规则违规?我认为int->charint->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)

c++ strict-aliasing language-lawyer

4
推荐指数
1
解决办法
147
查看次数