8

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

apaderno
  • 28,547
  • 16
  • 75
  • 90
Rella
  • 65,003
  • 109
  • 363
  • 636

3 Answers3

12

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).

Community
  • 1
  • 1
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
  • Though don't name your variables `array`. :) – Justin Ardini Aug 22 '10 at 22:58
  • 3
    or more conventionally, `unsigned char` – Yann Ramin Aug 22 '10 at 23:01
  • 3
    @theatrus, no, `char` is not guaranteed to be a 8-bit type. – Bertrand Marron Aug 22 '10 at 23:03
  • 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... – alternative Aug 22 '10 at 23:05
  • Bytes aren't always 8-bits, of course - it depends on the platform (http://en.wikipedia.org/wiki/Byte). – Will A Aug 22 '10 at 23:14
  • @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 Aug 22 '10 at 23:15
  • @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 Aug 22 '10 at 23:27
  • 2
    @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 Aug 22 '10 at 23:41
  • 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. – Anthony Aug 23 '10 at 00:16
  • 1
    @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 Aug 23 '10 at 03:03
  • 2
    @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 Aug 23 '10 at 04:44
6

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
  • 7,453
  • 1
  • 35
  • 56
  • write you are, thank you!) (helped me for my visual-C++ .net library) – Rella Aug 22 '10 at 23:24
  • 2
    Don't you mean `byte[] array = new byte[10]`? – Anthony Aug 23 '10 at 00:12
  • 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 Aug 23 '10 at 02:28
6

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 };
Sadeq
  • 7,795
  • 4
  • 34
  • 45