4

I need convert unsigned char array into unit8_t array maybe someone has an idea how to do it?

I tried to look for information on the Internet, but unfortunately I did not succeed.

:)

  • There's nothing to convert. **Iff** `uint8_t` exists, it is the same as `unsigned char`. Or the other way around: If `unsigned char` has more than 8 bits, there's no `uint8_t` available. –  Jul 28 '17 at 11:01

2 Answers2

6

So, have you tried uint8_t* myuint8array = (uint8_t*)myuchararray; ?

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
1

You have

unsigned char arr[size];

and you want

uint8_t arr[size];

uint8_t is simply defined as

typedef unsigned char uint8_t;

:-)

Robert
  • 277
  • 1
  • 16
  • 1
    Or not defined at all, in case `char` has more than 8 bits. –  Jul 28 '17 at 11:02
  • And when would a character possibly have more than 8 bits? – Robert Jul 28 '17 at 11:03
  • @Robert - It could for example be 9 bits [on a 36-bit computer](https://stackoverflow.com/a/6972551/597607). – Bo Persson Jul 28 '17 at 11:05
  • character != `char`. And then, the answer is "*when it has more than 8 bits*". Yes, such platforms exist and they can't provide `uint8_t` for obvious reasons. –  Jul 28 '17 at 11:05
  • This was complete news to me, i have never heard of this before. Thanks, will read this straight away. :-) – Robert Jul 28 '17 at 11:07
  • @RobertK because such platforms exist, C only mandates a *minimum* of 8 bits for `char`. But `uint8_t` must have **exactly** 8 bits, so this type will be missing on a platform having larger `char`s. –  Jul 28 '17 at 11:10
  • @RobertK that is why `#define CHAR_BIT 8` (for example) appears in `limits.h` – Weather Vane Jul 28 '17 at 11:20
  • @WeatherVane Interesting. Learn something new every day :-) – Robert Jul 28 '17 at 11:31
  • @RobertK funny you should say that ... so do I! – Weather Vane Jul 28 '17 at 11:42