November 25, 2020, 05:21:14 AM

### Author Topic: [bb] 2 and 4 byte compressor by Skully [ 1+ years ago ]  (Read 469 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] 2 and 4 byte compressor by Skully [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
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
1. Function crunch2\$(num)
2.         byte2=num Shr 8 And %11111111
3.         byte1=num And %11111111
4.         Return Chr\$(byte2)+Chr\$(byte1)
5. End Function
6.
7. Function uncrunch2(bytes\$)
8.         byte2=Asc(Left\$(bytes,1)) Shl 8
9.         byte1=Asc(Right\$(bytes,1))
10.         Return byte2 Or byte1
11. End Function
12.
13. Function crunch4\$(num)
14.         byte4=num Shr 24 And %11111111
15.         byte3=num Shr 16 And %11111111
16.         byte2=num Shr 8 And %11111111
17.         byte1=num And %11111111
18.         Return Chr\$(byte4)+Chr\$(byte3)+Chr\$(byte2)+Chr\$(byte1)
19. End Function
20.
21. Function uncrunch4(bytes\$)
22.         byte4=Asc(Left\$(bytes,1)) Shl 24
23.         byte3=Asc(Mid\$(bytes,2,1)) Shl 16
24.         byte2=Asc(Mid\$(bytes,3,1)) Shl 8
25.         byte1=Asc(Right\$(bytes,1))
26.         Return byte4 Or byte3 Or byte2 Or byte1
27. End Function
28.

SimplePortal 2.3.6 © 2008-2014, SimplePortal