如何在 C++ 中创建一个字节数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16489407/
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
How to create a byte array in C++?
提问by Soldier
Please have a look at the followng header file
请看下面的头文件
#pragma once
class MissileLauncher
{
public:
MissileLauncher(void);
private:
byte abc[3];
};
This generated the error
这产生了错误
Error 1 error C2143: syntax error : missing ';' before '*'
I tried to do it in this way
我试着这样做
byte *abc;
but it also failed, same error. However, I noticed I can call other built in tyes arrays in this way for an example, an int array. Why this is happening to byte array? How to solve this? I would like to assign the values in cpp file. Any ideas?
但它也失败了,同样的错误。但是,我注意到我可以以这种方式调用其他内置的 tyes 数组,例如 int 数组。为什么这会发生在字节数组上?如何解决这个问题?我想在 cpp 文件中分配值。有任何想法吗?
回答by Michael Price
Try
尝试
class MissileLauncher
{
public:
MissileLauncher(void);
private:
unsigned char abc[3];
};
or
或者
using byte = unsigned char;
class MissileLauncher
{
public:
MissileLauncher(void);
private:
byte abc[3];
};
**Note: In older compilers (non-C++11) replace the using
line with typedef unsigned char byte;
**注意:在较旧的编译器(非 C++11)中,将该using
行替换为typedef unsigned char byte;
回答by TractorPulledPork
If you want exactly one byte, uint8_t defined in cstdint would be the most expressive.
如果您只需要一个字节,cstdint 中定义的 uint8_t 将是最具表现力的。
回答by Escualo
Maybe you can leverage the std::bitset
type available in C++11. It can be used to represent a fixed sequence of N
bits, which can be manipulated by conventional logic.
也许您可以利用std::bitset
C++11 中可用的类型。它可用于表示固定的N
位序列,可由常规逻辑操作。
#include<iostream>
#include<bitset>
class MissileLauncher {
public:
MissileLauncher() {}
void show_bits() const {
std::cout<<m_abc[2]<<", "<<m_abc[1]<<", "<<m_abc[0]<<std::endl;
}
bool toggle_a() {
// toggles (i.e., flips) the value of `a` bit and returns the
// resulting logical value
m_abc[0].flip();
return m_abc[0];
}
bool toggle_c() {
// toggles (i.e., flips) the value of `c` bit and returns the
// resulting logical value
m_abc[2].flip();
return m_abc[2];
}
bool matches(const std::bitset<3>& mask) {
// tests whether all the bits specified in `mask` are turned on in
// this instance's bitfield
return ((m_abc & mask) == mask);
}
private:
std::bitset<3> m_abc;
};
typedef std::bitset<3> Mask;
int main() {
MissileLauncher ml;
// notice that the bitset can be "built" from a string - this masks
// can be made available as constants to test whether certain bits
// or bit combinations are "on" or "off"
Mask has_a("001"); // the zeroth bit
Mask has_b("010"); // the first bit
Mask has_c("100"); // the second bit
Mask has_a_and_c("101"); // zeroth and second bits
Mask has_all_on("111"); // all on!
Mask has_all_off("000"); // all off!
// I can even create masks using standard logic (in this case I use
// the or "|" operator)
Mask has_a_and_b = has_a | has_b;
std::cout<<"This should be 011: "<<has_a_and_b<<std::endl;
// print "true" and "false" instead of "1" and "0"
std::cout<<std::boolalpha;
std::cout<<"Bits, as created"<<std::endl;
ml.show_bits();
std::cout<<"is a turned on? "<<ml.matches(has_a)<<std::endl;
std::cout<<"I will toggle a"<<std::endl;
ml.toggle_a();
std::cout<<"Resulting bits:"<<std::endl;
ml.show_bits();
std::cout<<"is a turned on now? "<<ml.matches(has_a)<<std::endl;
std::cout<<"are both a and c on? "<<ml.matches(has_a_and_c)<<std::endl;
std::cout<<"Toggle c"<<std::endl;
ml.toggle_c();
std::cout<<"Resulting bits:"<<std::endl;
ml.show_bits();
std::cout<<"are both a and c on now? "<<ml.matches(has_a_and_c)<<std::endl;
std::cout<<"but, are all bits on? "<<ml.matches(has_all_on)<<std::endl;
return 0;
}
Compiling using gcc 4.7.2
使用 gcc 4.7.2 编译
g++ example.cpp -std=c++11
I get:
我得到:
This should be 011: 011
Bits, as created
false, false, false
is a turned on? false
I will toggle a
Resulting bits:
false, false, true
is a turned on now? true
are both a and c on? false
Toggle c
Resulting bits:
true, false, true
are both a and c on now? true
but, are all bits on? false
回答by Joel
Byte is not a standard type in C or C++. Try char, which is usually and at least 8 bits long.
Byte 不是 C 或 C++ 中的标准类型。试试 char,它通常至少有 8 位长。
回答by Anthony Oyovwe
Byte is not a standard data type in C/C++ but it can still be used the way i suppose you want it. Here is how: Recall that a byte is an eight bit memory size which can represent any of the integers between -128 and 127, inclusive. (There are 256 integers in that range; eight bits can represent 256 -- two raised to the power eight -- different values.). Also recall that a char in C/C++ is one byte (eight bits). So, all you need to do to have a byte data type in C/C++ is to put this code at the top of your source file: #define byte char So you can now declare byte abc[3];
Byte 不是 C/C++ 中的标准数据类型,但它仍然可以按照我想你想要的方式使用。方法如下: 回想一下,一个字节是一个八位内存大小,它可以表示 -128 和 127 之间的任何整数,包括在内。(在该范围内有 256 个整数;8 位可以表示 256——2 的 8 次方——不同的值。)。还记得 C/C++ 中的字符是一个字节(八位)。因此,要在 C/C++ 中拥有字节数据类型,您所需要做的就是将此代码放在源文件的顶部: #define byte char 现在您可以声明 byte abc[3];
回答by Jared Price
You could use Qt which, in case you don't know, is C++ with a bunch of additional libraries and classes and whatnot. Qt has a very convenient QByteArray class which I'm quite sure would suit your needs.
您可以使用 Qt,以防万一您不知道,它是带有一堆附加库和类的 C++ 等等。Qt 有一个非常方便的 QByteArray 类,我很确定它会满足您的需求。
回答by Chris Redford
Byte is not a standard type in C/C++, so it is represented by char
.
Byte 不是 C/C++ 中的标准类型,所以用char
.
An advantage of this is that you can treat a basic_string
as a byte array allowing for safe storage and function passing. This will help you avoid the memory leaks and segmentation faults you might encounter when using the various forms of char[]
and char*
.
这样做的一个优点是您可以将 abasic_string
视为允许安全存储和函数传递的字节数组。这将帮助你避免内存泄漏和段错误使用各种形式的时候,你可能会遇到char[]
和char*
。
For example, this creates a string as a byte array of null values:
例如,这将创建一个字符串作为空值的字节数组:
typedef basic_string<unsigned char> u_string;
u_string bytes = u_string(16,'u_string otherBytes = "some more chars, which are just bytes";
for(int i = 0; i < otherBytes.length(); i++)
bytes[i%16] ^= (int)otherBytes[i];
');
This allows for standard bitwise operations with other char
values, including those stored in other string
variables. For example, to XOR the char
values of another u_string
across bytes
:
这允许对其他char
值进行标准的按位运算,包括存储在其他string
变量中的值。例如,为了将异或char
另一个的值u_string
跨越bytes
: