smalltalk
/
osmo-st-gsm
Archived
1
0
Fork 0
This repository has been archived on 2022-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
osmo-st-gsm/GSMEncoding.st

58 lines
2.0 KiB
Smalltalk
Raw Normal View History

2011-03-31 17:18:10 +00:00
"
(C) 2011 by Holger Hans Peter Freyther
All Rights Reserved
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"
String extend [
asGSM7Bit [
| bits rest bytes |
"I convert a string into a 7bit encoded string. I should
also be in UnicodeString but I am not. This impl. is just
basic and does not deal with difficult bits."
<category: '*-osmo-gsm'>
bits := OrderedCollection new.
"Split it into bits"
self do: [:char | | val |
val := char value.
1 to: 7 do: [:digit | bits add: (val bitAt: digit)].
].
"Turn bits into bytes again..."
rest := 8 - (bits size \\ 8).
rest to: 1 by: -1 do: [:each | bits add: 0].
bytes := ByteArray new: bits size / 8.
1 to: bits size by: 8 do: [:each | | byte |
byte := 0.
byte := (byte bitShift: 1) bitOr: (bits at: each + 7).
byte := (byte bitShift: 1) bitOr: (bits at: each + 6).
byte := (byte bitShift: 1) bitOr: (bits at: each + 5).
byte := (byte bitShift: 1) bitOr: (bits at: each + 4).
byte := (byte bitShift: 1) bitOr: (bits at: each + 3).
byte := (byte bitShift: 1) bitOr: (bits at: each + 2).
byte := (byte bitShift: 1) bitOr: (bits at: each + 1).
byte := (byte bitShift: 1) bitOr: (bits at: each + 0).
bytes at: (each // 8) + 1 put: byte.
].
^ bytes
]
]