How to "build" a negative 32-bit integer from bytes without a range check error?

I need to convert a 21-bit signed integer (represented in three 7-bit characters) to a 32-bit signed integer. I am trying to make the function below work. If I define outval as an integer, I get a range check in the outval: = outval or $ FFF00000 statement. If I change outval to longword, I get a range check on "Val21bit: = outval". Range checking occurs only when the value is negative.

Besides disabling range checking around the job, is there a β€œright” way to do this job?

function Val21bit(sx:string):integer; {Input of 3 character string, Vh, Vm, Vl} var outval : longword; // define as Longword to allow or-ing in high bit { valueH : 0scccccc valueM : 0bbbbbbb valueL : 0aaaaaaa int : ssssssss sssscccc ccbbbbbb baaaaaaa } begin outval := byte(sx[1]); // 00000000 00000000 00000000 0scccccc highest order first outval := (outval shl 7) or ($7F and byte(sx[2])); // 00000000 00000000 00sccccc cbbbbbbb outval := (outval shl 7) or ($7F and byte(sx[3])); // 00000000 000scccc ccbbbbbb baaaaaaa if (outval and $00100000) <> 0 then // ^ if sign bit is high, fill in to left outval := outval or $FFF00000; // ssssssss sssscccc ccbbbbbb baaaaaaa Val21bit := outval; end; 
+4
source share
1 answer

Yes, just explicitly stated:

  Val21bit := Integer(outval); 
+9
source

All Articles