用 C++ 中的十六进制值初始化一个无符号字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19946660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing an unsigned char array with hex values in C++
提问by hatgirl
I would like to initialize an unsigned char array with 16 hex values. However, I don't seem to know how to properly initialize/access those values. When I try to access them as I might want to intuitively, I'm getting no value at all.
我想用 16 个十六进制值初始化一个无符号字符数组。但是,我似乎不知道如何正确初始化/访问这些值。当我尝试以直觉方式访问它们时,我根本没有任何价值。
This is my output
这是我的输出
The program was run with the following command: 4
Please be a value! -----> p
Here's some plaintext
when run with the code below -
当使用下面的代码运行时 -
int main(int argc, char** argv)
{
int n;
if (argc > 1) {
n = std::stof(argv[1]);
} else {
std::cerr << "Not enough arguments\n";
return 1;
}
char buff[100];
sprintf(buff,"The program was run with the following command: %d",n);
std::cout << buff << std::endl;
unsigned char plaintext[16] =
{0x0f, 0xb0, 0xc0, 0x0f,
0xa0, 0xa0, 0xa0, 0xa0,
0x00, 0x00, 0xa0, 0xa0,
0x00, 0x00, 0x00, 0x00};
unsigned char test = plaintext[1]^plaintext[2];
std::cout << "Please be a value! -----> " << test << std::endl;
std::cout << "Here's some plaintext " << plaintext[3] << std::endl;
return 0;
}
By way of context, this is part of a group project for school. We are ultimately trying to implement the Serpent cipher, but keep on getting tripped up by unsigned char arrays. Our project specification says that we must have two functions that take what would be Byte arrays in Java. I assume the closest relative in C++ is an unsigned char[]. Otherwise I would use vector. Elsewhere in the code I've implemented a setKey function which takes an unsigned char array, packs its values into 4 long long ints (the key needs to be 256 bits) and performs various bit-shifting and xor operations on those ints to generate the keys necessary for the cryptographic algorithm. Hope that's enough background on what I'm looking to do. I'm guessing I'm just overlooking some basic C++ functionality here. Thanks for any and all help!
就上下文而言,这是学校小组项目的一部分。我们最终试图实现 Serpent 密码,但不断被无符号字符数组绊倒。我们的项目规范说我们必须有两个函数来接受 Java 中的字节数组。我假设 C++ 中最接近的亲戚是 unsigned char[]。否则我会使用向量。在代码的其他地方,我实现了一个 setKey 函数,它采用一个 unsigned char 数组,将其值打包成 4 个 long long int(密钥需要为 256 位)并对这些 int 执行各种位移和异或运算以生成加密算法所需的密钥。希望这对我正在做的事情有足够的背景。我猜我只是在这里忽略了一些基本的 C++ 功能。感谢您的任何帮助!
回答by kfsone
A char
is an 8-bit value capable of storing -128 <= n <= +127, frequently used to store character representations in different encodings and commonly - in Western, Roman-alphabet installations - char
is used to indicate representation of ASCII or utf encoded values. 'Encoded' means the symbols/letter in the character set have been assigned numeric values. Think of the periodic table as an encoding of elements, so that 'H' (Hydrogen) is encoded as 1, Germanium as 32. In the ASCII (and UTF-8) tables, position 32 represents the character we call "space".
Achar
是一个 8 位值,能够存储 -128 <= n <= +127,经常用于存储不同编码的字符表示,通常 - 在西方,罗马字母安装 -char
用于表示 ASCII 或 utf 编码的表示值。'Encoded' 表示字符集中的符号/字母已被分配数值。将元素周期表视为元素的编码,因此“H”(氢)编码为 1,锗编码为 32。在 ASCII(和 UTF-8)表中,位置 32 表示我们称为“空格”的字符。
When you use operator <<
on a char value, the default behavior is to assume you are passing it a character encoding, e.g. an ASCII character code. If you do
当您operator <<
在 char 值上使用时,默认行为是假定您向它传递字符编码,例如 ASCII 字符代码。如果你这样做
char c = 'z';
char d = 122;
char e = 0x7A;
char f = '\x7a';
std::cout << c << d << e << f << "\n";
All four assignments are equivalent. 'z' is a shortcut/syntactic-sugar for char(122)
, 0x7A
is hex for 122, and '\x7a' is an escape that forms the ascii character with a value of 0x7a or 122 - i.e. z.
所有四个作业都是等价的。'z' 是 的快捷方式/语法糖char(122)
,0x7A
是 122 的十六进制,而 '\x7a' 是形成值为 0x7a 或 122 的 ascii 字符的转义符 - 即
Where many new programmers go wrong is that they do this:
许多新程序员出错的地方是他们这样做:
char n = 8;
std::cout << n << endl;
this does not print "8", it prints ASCII character at position 8 in the ASCII table.
这不会打印“8”,而是在 ASCII 表中的位置 8 打印 ASCII 字符。
Think for a moment:
想一想:
char n = 8; // stores the value 8
char n = a; // what does this store?
char n = '8'; // why is this different than the first line?
Lets rewind a moment: when you store 120
in a variable, it canrepresent the ASCII character 'x'
, but ultimately what is being stored is just the numeric value 120
, plain and simple.
让我们回顾一下:当您存储120
在变量中时,它可以表示 ASCII 字符'x'
,但最终存储的只是数值120
,简单明了。
Specifically: When you pass 122
to a function that will ultimately use it to look up a font entry from a character set using the Latin1, ISO-8859-1, UTF-8 or similar encodings, then 120
means 'z'
.
具体来说:当您传递122
给最终使用它从使用 Latin1、ISO-8859-1、UTF-8 或类似编码的字符集中查找字体条目的函数时,则120
表示'z'
.
At the end of the day, char
is just one of the standard integer value types, it can store values -128 <= n <= +127
, it can trivially be promoted to a short
, int
, long
or long long
, etc, etc.
归根结底,char
它只是标准整数值类型之一,它可以存储值-128 <= n <= +127
,也可以简单地提升为 a short
、int
、long
orlong long
等,等等。
While it is generallyused to denote characters, it also frequently gets used as a way of saying "I'm only storing very small values" (such as integer percentages).
虽然它通常用于表示字符,但它也经常被用作“我只存储非常小的值”(例如整数百分比)的一种方式。
int incoming = 5000;
int outgoing = 4000;
char percent = char(outgoing * 100 / incoming);
If you want to print the numeric value, you simply need to promote it to a different value type:
如果要打印数值,只需将其提升为不同的值类型:
std::cout << (unsigned int)test << "\n";
std::cout << unsigned int(test) << "\n";
or the preferred C++ way
或首选的 C++ 方式
std::cout << static_cast<unsigned int>(test) << "\n";
回答by john
I think (it's not completely clear what you are asking) that the answer is as simple as this
我认为(不完全清楚你在问什么)答案就是这样简单
std::cout << "Please be a value! -----> " << static_cast<unsigned>(test) << std::endl;
If you want to output the numeric value of a char or unsigned char, you have to cast it to an int or unsigned first.
如果要输出 char 或 unsigned char 的数值,则必须先将其转换为 int 或 unsigned。
Not surprisingly, by default, chars are output as characters not integers.
毫不奇怪,默认情况下,字符被输出为字符而不是整数。
BTW this funky code
顺便说一句,这个时髦的代码
char buff[100];
sprintf(buff,"The program was run with the following command: %d",n);
std::cout << buff << std::endl;
is more simply written as
更简单地写为
std::cout << "The program was run with the following command: " << n << std::endl;
回答by TerrenceSun
std::cout
and std::cin
always treats char variable as a char
If you want to input or output as int, you must manually do it like below.
std::cout
并且std::cin
始终将 char 变量视为 char
如果要输入或输出为 int,则必须手动执行如下操作。
std::cin >> int_var; c = int_var;
std::cout << (int)c;
If using scanf or printf, there is no such problem as the format parameter ("%d", "%c", "%s") tells howto covert input buffer (integer, char, string).
如果使用 scanf 或 printf,则没有格式参数(“%d”、“%c”、“%s”)告诉如何转换输入缓冲区(整数、字符、字符串)的问题。