views:

203

answers:

3

What is the C++ (and or visual-C++) analog of C# byte[]?

+7  A: 

byte[], in C#, is an array of unsigned 8-bit integers (byte).

An equivalent would be uint8_t array[].

uint8_t is defined in stdint.h (C) and cstdint (C++), if they are not provided on your system, you could easily download them, or define them yourself (see this SO question).

Bertrand Marron
Though don't name your variables `array`. :)
Justin Ardini
or more conventionally, `unsigned char`
Yann Ramin
@theatrus, no, `char` is not guaranteed to be a 8-bit type.
Bertrand Marron
Since `sizeof(char) == 1`, if `char` is not 8 bits, can `uint8_t` technically be 8 bits? Because that would imply `sizeof(uint8_t) < 1`, meaning its not an integer, which would tell us that `size_t` is not an integer type...
mathepic
Bytes aren't always 8-bits, of course - it depends on the platform (http://en.wikipedia.org/wiki/Byte).
Will A
@Will A, C#'s `byte` type is guaranteed to be a 8-bit type. @mathepic, see [uint8_t vs unsigned char](http://stackoverflow.com/questions/1725855/uint8-t-vs-unsigned-char)
Bertrand Marron
@Bertrand: Of course C#'s byte is guaranteed to be 8-bit, as is Java's byte. But in C++ the size of a `char` is platform dependent and if your platform has 16-bit chars I don't see how stdint.h could possibly define `uint8_t` as an 8-bit data type.
Praetorian
@praetorian: CHAR_BITS >=8. So a char can't be smaller than 8 but it can be larger. uint8_t just has to support 8 bit operations. But who says the compiler need to model uint8_t on a char. http://linux.die.net/man/3/uint8_t
Martin York
Matrin York is correct. THe uint8_t should behave as an 8-bit unsigned integer even on systems where a byte != 8-bits. The way it is structured in memory may be different though. On system where a byte = 16 bits, the uint8_t may occupy 16 bits but only perform 8-bit arithmetic.
Duracell
@Martin York: I don't get it, the link you posted has `uint8_t` defined as follows: `typedef unsigned char uint8_t`, so it is modeled on a char. If char's are larger than 8 bits, so will the uint8_t. Or am I wrong? All I was saying earlier was that `uint8_t` simply indicates your intention that that variable should only hold values in the range [0 255] but on certain platforms there's nothing stopping you (poor programming practice aside) from sticking larger numbers into it.
Praetorian
@Praetorian: On this platform it is typedefed as a char as that works for this platform. But there is no standards requirement (I suspect) that it is required to be a char (just that it is required to be 8 bits). Which is another way of saying what Duracell said.
Martin York
+1  A: 

The closest equivalent type in C++ would be a dynamically created array of "unsigned char" (unless you're running on a processor that defines a byte as something other than 8 bits).

So for example

in C#

byte[] array = new byte[10];

in C++

unsigned char *array = new unsigned char[10];
MerickOWA
write you are, thank you!) (helped me for my visual-C++ .net library)
Blender
Don't you mean `byte[] array = new byte[10]`?
Duracell
Yes thanks for that catch.I would point out that there are alot of places were C++ and C# arrays differ. While both can't be resized, C# will give you an error if you try to access more data than is available. If you're lucky, C++ might crash.Also, C# can tell you the size of the array at any time, C++ can't tell you how big the array is once its created, you have to remember some how.
MerickOWA
+3  A: 

In C++ standard, char, signed char and unsigned char are three available char types. char maybe signed or unsigned, therefore:

typedef signed char sbyte;
typedef unsigned char byte;

byte bytes[] = { 0, 244, 129 };
PC2st