How to create an array of bytes in C ++?

Please see the followng header file

#pragma once class MissileLauncher { public: MissileLauncher(void); private: byte abc[3]; }; 

This caused an error.

 Error 1 error C2143: syntax error : missing ';' before '*' 

I tried to do it this way

 byte *abc; 

but he also failed, with the same error. However, I noticed that I can call other tyes built-in arrays this way, for example, for an int array. Why is this happening with a byte? How to solve this? I would like to assign values ​​in a cpp file. Any ideas?

+7
source share
7 answers

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 (not C ++ 11), replace the using string with typedef unsigned char byte;

+17
source

If you want exactly one byte, then uint8_t defined in cstdint will be most expressive.

http://www.cplusplus.com/reference/cstdint/

+10
source

A byte is not a standard type in C or C ++. Try char, which is usually at least 8 bits.

+7
source

Perhaps you can use the std::bitset type available in C ++ 11. It can be used to represent a fixed sequence of N bits, which can be manipulated using common logic.

 #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 (ie, flips) the value of `a` bit and returns the // resulting logical value m_abc[0].flip(); return m_abc[0]; } bool toggle_c() { // toggles (ie, 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 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

 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 
+6
source

A byte is not a standard data type in C / C ++, but it can still be used the way I assume you want it. Here's how to do it: Recall that a byte is an eight-bit memory size that can represent any of the integers from -128 to 127 inclusive. (In this range of 256 integers, eight bits can represent 256 β€” two raised to eight β€” different values.). Also recall that a char in C / C ++ is one byte (eight bits). So, all you have to do to have a byte data type in C / C ++ is to have this code at the top of the source file: #define byte char So now you can declare the byte abc [3] ;

+2
source

You can use Qt, which, if you do not know, is C ++ with a bunch of additional libraries and classes and much more. Qt has a very convenient QByteArray class that, I am sure, would suit your needs.

http://qt-project.org/

+1
source

A byte is not a standard type in C / C ++, so it is represented by char .

The advantage of this is that you can treat basic_string as a byte array that allows safe storage and transfer of functions. This will help to avoid memory leaks and segmentation failures that may occur when using various forms of char[] and char* .

For example, this creates a string as a byte array with zero values:

 typedef basic_string<unsigned char> u_string; u_string bytes = u_string(16,'\0'); 

This allows you to perform standard bitwise operations with other char values, including those stored in other string variables. For example, for XOR char values ​​of another u_string bytes :

 u_string otherBytes = "some more chars, which are just bytes"; for(int i = 0; i < otherBytes.length(); i++) bytes[i%16] ^= (int)otherBytes[i]; 
-one
source

All Articles