0

I am currently writing a parser for Asterix Cat 240 (Open standard for transmitting radar video data) and it works perfectly, but I do not know, why do I need type conversion?

rawAsterix.videoHR = static_cast<quint8>(buffer->at(i));
rawAsterix.videoHR = (rawAsterix.videoHR << 8) + static_cast<quint8>(buffer->at(i+1));
rawAsterix.videoHR = (rawAsterix.videoHR << 8) + static_cast<quint8>(buffer->at(i+2));
rawAsterix.videoHR = (rawAsterix.videoHR << 8) + static_cast<quint8>(buffer->at(i+3));
i+=4

buffer is a QByteArray, whish holds the data received by QUdpSocket. At first I had no type conversion whish resulted (sometimes not every time) in strange behaviour. Why is that so?

Ilya
  • 4,583
  • 4
  • 26
  • 51
honiahaka10
  • 772
  • 4
  • 9
  • 29
  • define "sometimes not every time" and "strange behaviour". Do you have a self-contained test that can be used to analyse what is going wrong? – rubenvb Mar 15 '16 at 08:35
  • Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Mar 15 '16 at 08:35
  • @rubenvb I cannot see any logic in the strange behaviour its just that some bits have not the right value. – honiahaka10 Mar 15 '16 at 08:53

1 Answers1

2

QByteArray::at() returns char. And static_cast<quint8> of this char returns unsigned char. It seems that in this code you need unsigned value.

In fact, the standard does not specify if plain char is signed or unsigned (see Is char signed or unsigned by default?). But if you need to be sure that you add unsigned value, you need to use static_cast<quint8> here.

Community
  • 1
  • 1
Ilya
  • 4,583
  • 4
  • 26
  • 51