[bb] 2 and 4 byte compressor by Skully [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : 2 and 4 byte compressor
Author : Skully
Posted : 1+ years ago

Description : Crunch2 turns a 2 byte (0-65535) value into a 2 byte string.  Crunch 4 turns a 4 byte (0-4294967295) value into a 4 byte string.

Uncrunch2 and Uncrunch4 restore the original values.

This is good for compressing numerical values for storage in files or transfer over the network.
 


Code :
Code (blitzbasic) Select
Function crunch2$(num)
byte2=num Shr 8 And %11111111
byte1=num And %11111111
Return Chr$(byte2)+Chr$(byte1)
End Function

Function uncrunch2(bytes$)
byte2=Asc(Left$(bytes,1)) Shl 8
byte1=Asc(Right$(bytes,1))
Return byte2 Or byte1
End Function

Function crunch4$(num)
byte4=num Shr 24 And %11111111
byte3=num Shr 16 And %11111111
byte2=num Shr 8 And %11111111
byte1=num And %11111111
Return Chr$(byte4)+Chr$(byte3)+Chr$(byte2)+Chr$(byte1)
End Function

Function uncrunch4(bytes$)
byte4=Asc(Left$(bytes,1)) Shl 24
byte3=Asc(Mid$(bytes,2,1)) Shl 16
byte2=Asc(Mid$(bytes,3,1)) Shl 8
byte1=Asc(Right$(bytes,1))
Return byte4 Or byte3 Or byte2 Or byte1
End Function


Comments : none...