From 9ce4aebeb265b5f9ffe189b8dd1dad9cc2c196e8 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Fri, 1 Apr 2011 01:14:46 +0200 Subject: GSM: Add basic decoding support to byte array --- GSMEncoding.st | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'GSMEncoding.st') diff --git a/GSMEncoding.st b/GSMEncoding.st index f82575a..03cb44f 100644 --- a/GSMEncoding.st +++ b/GSMEncoding.st @@ -16,6 +16,13 @@ along with this program. If not, see . " +ByteArray extend [ + decodeGSM7Bit [ + + ^ GSMDecoding decode: self. + ] +] + String extend [ asGSM7Bit [ "I convert a string into a 7bit encoded string. I should @@ -33,6 +40,57 @@ String extend [ ] ] +Object subclass: GSMDecoding [ + + + + GSMDecoding class >> decode: aByteArray [ + | bits bytes | + bits := self convertFromBytes: aByteArray. + bytes := self convertToBytes: bits. + ^ self handleBytes: bytes from: bits + ] + + GSMDecoding class >> handleBytes: bytes from: bits [ + ^ bytes asString + ] + + GSMDecoding class >> convertFromBytes: aByteArray [ + | bits | + "We convert the stream into single bits. It is the + easiest to do it like this." + + bits := OrderedCollection new. + aByteArray do: [:each | + 1 to: 8 do: [:pos | bits add: (each bitAt: pos)] + ]. + + ^ bits + ] + + GSMDecoding class >> convertToBytes: bits [ + | bytes | + + bytes := ByteArray new: (bits size // 7). + 1 to: bits size by: 7 do: [:pos | + (pos + 6 <= bits size) ifTrue: [ | byte | + byte := 0. + byte := (byte bitShift: 1) bitOr: (bits at: pos + 6). + byte := (byte bitShift: 1) bitOr: (bits at: pos + 5). + byte := (byte bitShift: 1) bitOr: (bits at: pos + 4). + byte := (byte bitShift: 1) bitOr: (bits at: pos + 3). + byte := (byte bitShift: 1) bitOr: (bits at: pos + 2). + byte := (byte bitShift: 1) bitOr: (bits at: pos + 1). + byte := (byte bitShift: 1) bitOr: (bits at: pos + 0). + bytes at: (pos // 7) + 1 put: byte. + ]. + ]. + + ^ bytes + ] +] + Object subclass: GSMEncoding [