TStream ReadShort

BlitzMax Forums/BlitzMax Programming/TStream ReadShort

richardc(Posted 2011) [#1]
Hello,

I'm using a TStream to read a binary file format. The format uses a signed Short, and Short in BlitzMax is unsigned.

I realise the top bit indicates the number is negative, but is there an easy way to get the value in the standard library without having to do the bit manipulations?

Many thanks,

Richard


ziggy(Posted 2011) [#2]
BlitzMax shorts are signed :D


richardc(Posted 2011) [#3]
Hi,

Thanks for the reply, but they are definitely unsigned...

According to the manual.

16 bit unsigned integer Short

Cheers,

Richard


Floyd(Posted 2011) [#4]
16-bit unsigned Short is in the range 0 to 65535.

A signed Short reinterprets this Mod 2^16 so the result is -32768 to +32767.
So to convert unsigned to signed just check if the value is greater than 32767. If it is then subtract 65536.

Last edited 2011


Zeke(Posted 2011) [#5]
Local a:Short
a = $7fff '32767 'max positive short
a = $8000 '-32768 'max negative short
a = $FFFF '-1

Local b:Int = a 'unsigned short to signed int

If b > 32767 Then b = b-65536  'convert to negative if over range

Print b



richardc(Posted 2011) [#6]
I'll do that then.

I thought there maybe a function to do this though, given it must come up reasonably often.

Thanks,

Richard


Floyd(Posted 2011) [#7]
This can be simplified if you don't mind "bit twiddling".

We need to sign extend the short into a signed integer. This means to fill the high 16 bits of the integer with copies of the sign bit.

If s is a signed short then ( s Shl 16 ) Sar 16 is the desired signed integer.

Last edited 2011