سؤال

I got a short value(which can just be limited to a UINT8) and want to assign it to a UINT8(/unsigned char).
But I thought of no better choices, but to do this:

unlatch.FaultBits.Value[0] = ui->lineEditFaultBits->text().toShort();

But can this achieves what I want when I use unlatch.FaultBits.Value[0]? (Vaule[] is an array in a struct: UINT8 Value[64];)

هل كانت مفيدة؟

المحلول

Can this achieve what you want? Well, I can assume one of four avenues, because you haven't told us exactly what you want (grumble).

  1. You want to assign the short value to a uint8_t, without signed-to-unsigned conversion. In this case, you must be sure that the old value is within the range of a uint8_t: assert(old_value >= 0 && old_value <= 255);. If this point describes the behaviour you desire and that assertion fails, then no, your code doesn't do what you want.
  2. You want to assign a possibly negative or out-of-range short value to a uint8_t and you don't see a problem with the reduction process R described in his comment. If this point describes the behaviour you desire, then yes, your code does what you want.
  3. You want to assign a possibly negative or out-of-range short value to a uint8_t and the reduction process R described in his comment doesn't work for you, but you are prepared to express your own reduction algorithm in your code. If this point describes the behaviour you desire, then no, you haven't expressed your custom reduction algorithm.
  4. You want to assign a possibly negative or out-of-range short value to a uint8_t and you don't want want the value to be reduced. In this case, uint8_t is unsuitable. Select a different type.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top