Is There 'Byte' Data Type in C++

Is there 'byte' data type in C++?

No there is no byte data type in C++. However you could always include the bitset header from the standard library and create a typedef for byte:

typedef bitset<8> BYTE;

NB: Given that WinDef.h defines BYTE for windows code, you may want to use something other than BYTE if your intending to target Windows.

Edit: In response to the suggestion that the answer is wrong.
The answer is not wrong. The question was "Is there a 'byte' data type in C++?". The answer was and is: "No there is no byte data type in C++" as answered.

With regards to the suggested possible alternative for which it was asked why is the suggested alternative better?

According to my copy of the C++ standard, at the time:

"Objects declared as characters (char) shall be large enough to store any member of the implementations basic character set": 3.9.1.1

I read that to suggest that if a compiler implementation requires 16 bits to store a member of the basic character set then the size of a char would be 16 bits. That today's compilers tend to use 8 bits for a char is one thing, but as far as I can tell there is certainly no guarantee that it will be 8 bits.

On the other hand, "the class template bitset<N> describes an object that can store a sequence consisting of a fixed number of bits, N." : 20.5.1. In otherwords by specifying 8 as the template parameter I end up with an object that can store a sequence consisting of 8 bits.

Whether or not the alternative is better to char, in the context of the program being written, therefore depends, as far as I understand, although I may be wrong, upon your compiler and your requirements at the time. It was therefore upto the individual writing the code, as far as I'm concerned, to do determine whether the suggested alternative was appropriate for their requirements/wants/needs.

C programming byte data type

your decoder seems to have some errors like: local_38[local_40] = local_38[local_40] - 2;

It should be like this: local_38[local_40] = local_38[local_40] + 2;

I have written a decoder for above question in python

key="lp`7a<qLw\x1ekHopt(f-f*,o}V\x0f\x15J"
check=list(key)
string=str()
string=''
for i in range(26,-1,-1):
j=i+10
k=(ord(check[i])+2)
string=(chr(k^j)+string)
print(string)

Hope this will help

what does the byte datatype function return in C

In that context the function is prototyped to return a type called byte. Because C does not have a native type byte, it must be a typedef, for example:

typedef unsigned char byte;  

The likely reason for this type is in the comment:

//return required number in percent

where because the function will return a integer value somewhere is the range of 0 to 100, 8 bits is more than sufficient. (in hex: 0x0 to 0x64, so if 100 is indeed the maximum needed value for the application the MSB will never be used.)
If however the full range of 8 bits is desired then return value could be up to 255.

How can i use 6 byte integer data type in C?

There is a way to have exact number of bits for something in C, so in theory it's doable, but it will take more than 48 bits in memory.

To do so you can use bit fields from C, to do so you can:

struct your_type {
uint64_t your_value : 48;
};

With this you can create such struct, and access your_value which will have 48 bit representation. It will be treated as uint64_t under the hood.

Saying that I strongly recommend reading Ansi C or any other basic C book to get to know basics better.

Please mind that creating parsers with bitfields is generally bad idea due to padding, packing and endianess issues issues. If you need to have any protocol please take interest in messagepack or any other protocol library.

Which C/C++ header file defines a BYTE data type?

I'm guessing it's from Windows.

A byte (8 bits).

This type is declared in WinDef.h as follows:

typedef unsigned char BYTE;

What is the byte?

3. Terms, definitions, and symbols

3.6 byte addressable unit of data storage large enough to
hold any member of the basic character set of the execution
environment

NOTE 1 It is possible to express the address of each individual byte
of an object uniquely.

NOTE 2 A byte is composed of a contiguous sequence of bits, the number
of which is implementation- defined. The least significant bit is
called the low-order bit; the most significant bit is called the
high-order bit.

This is a byte according to the C standard. Its minimum size is just the amount of bits required to hold the basic character set of the execution environment, i.e. a minimum of 8 nowadays IIRC. The exact size of a byte in bits is encoded in the CHAR_BIT macro.

how to create a new type with custom bytes in C?

You can just use char instead of int

Or, you can create structure, it is the most commonly used custom data type in C.
For example:

struct customStructure {
char c;
};

Type to use to represent a byte in ANSI (C89/90) C?

char is always a byte , but it's not always an octet. A byte is the smallest addressable unit of memory (in most definitions), an octet is 8-bit unit of memory.

That is, sizeof(char) is always 1 for all implementations, but CHAR_BIT macro in limits.h defines the size of a byte for a platform and it is not always 8 bit. There are platforms with 16-bit and 32-bit bytes, hence char will take up more bits, but it is still a byte. Since required range for char is at least -127 to 127 (or 0 to 255), it will be at least 8 bit on all platforms.

ISO/IEC 9899:TC3

6.5.3.4 The sizeof operator

  1. ...
  2. The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. [...]
  3. When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. [...]

Emphasis mine.



Related Topics



Leave a reply



Submit