Created
May 10, 2024 21:09
-
-
Save parsibox/1cae692ab114e69a6c91995c58b92242 to your computer and use it in GitHub Desktop.
unpackGSM7 converts GSM 7-bit packed data to a UTF-8 string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/hex" | |
"fmt" | |
) | |
func main() { | |
pdu := "b11a" // Example PDU hex string | |
decodedBytes, err := hex.DecodeString(pdu) | |
if err != nil { | |
fmt.Println("Error decoding hex:", err) | |
return | |
} | |
// Convert GSM-7 encoded data to string | |
result := unpackGSM7(decodedBytes) | |
fmt.Println("Decoded string:", result) | |
} | |
// unpackGSM7 converts GSM 7-bit packed data to a UTF-8 string | |
func unpackGSM7(data []byte) string { | |
// Calculate the number of 7-bit characters | |
length := len(data) * 8 / 7 | |
result := make([]byte, length) | |
var value uint | |
var bits int | |
for i, b := range data { | |
// Place new byte into value, shifting left by 8 bits | |
value |= uint(b) << bits | |
bits += 8 | |
// Extract as many 7-bit groups as possible | |
for bits >= 7 { | |
result[i] = byte(value & 0x7F) // Extract the 7-bit group | |
value >>= 7 // Remove the extracted bits | |
bits -= 7 // We've used 7 bits | |
} | |
} | |
return string(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment