Question: Ok I have a two part question about converting a element int a QByteArray (should be a char right?) to an unsigned integer.

Here is my code:

```
QByteArray data_read_buffer;

unsigned int data_recieved_size = 0;

/* the code below is an external function that populates the QbyteArray. shouldn't be important putting only to show that I assign a value to the byteArray./*

readFromComPortSafe(data_read_buffer); 

data_recieved_size  = static_cast<unsigned int>((data_read_buffer.at(2)));

```

Ok. So the result I am getting back in data\_recieved\_size is wrong "every once in a while"I go into the debugger and when i inspect the value of data\_read\_buffer i get this:

\000\003\203\00\205.... (ect ect we with some character with umlaut)

When I inspect the value of data\_recieved\_size i get 4294967171

So my first question is this

Am I doing something wrong with the conversion from a char to an unsigned int? 

My second question is this:

What does \203 represent in the qByteArray?I guess i don't understand what I am looking at. It is my understanding that the char type is a unicode character???? I was expecting the second element in the QByteArray to be 0x83 or 131. How does this translate to \203?

If anyone could provide some insight on this one i would appreciate it.

Thanks

Answer: 
`char`s can be signed or unsigned depending on your platform, in your case they are signed.

Think about what happens when you call `data_read_buffer.at` and the return value is `-125`:

* `-125` as a `char` is `0x83`
* This value is then converted to an `int` inside the `static_cast`, i.e. `0xffffff83`
* `static_cast` now casts this value to `unsigned int`, which simply copies the bits (well on two's complement machines at least).
* `0xffffff83` as an `unsigned int` is 4,294,967,171

If you want to cast to `unsigned int` and avoid the sign extension, you'll need to do something like

```
static_cast<unsigned int>(static_cast<unsigned char>(data_read_buffer.at(2)));

```
The `char` type is usually signed. Although it is not obvious, you are widening it to 32 bits as a signed value and then reinterpreting that as an unsigned value. If you insert a `static_cast< unsigned char >`, then you will be widening an unsigned value.