Skip to content

Instantly share code, notes, and snippets.

@parsibox
Created March 31, 2024 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsibox/440d0694570db5dda3f2e3780f8f7d66 to your computer and use it in GitHub Desktop.
Save parsibox/440d0694570db5dda3f2e3780f8f7d66 to your computer and use it in GitHub Desktop.
convert receive sms from kannel to utf8 with emoji
package main
import (
"fmt"
"net/url"
"strings"
"unicode/utf16"
)
func main() {
encodedString := "%06*%063%06*%D8%3D%DE%06%D8%3E%DDy%D8%3D%DE%0E"
decodedBytes, err := url.QueryUnescape(encodedString)
if err != nil {
fmt.Println("Error decoding:", err)
return
}
// Remove non-percent-encoded bytes
decodedString := strings.Join(strings.Split(string(decodedBytes), "\\"), "")
var utf16Bytes []uint16
for i := 0; i < len(decodedString); i += 2 {
if i+1 < len(decodedString) {
r := rune(uint16(decodedString[i])<<8 | uint16(decodedString[i+1]))
if !utf16.IsSurrogate(r) {
utf16Bytes = append(utf16Bytes, uint16(r))
} else {
utf16Bytes = append(utf16Bytes, uint16(decodedBytes[i])<<8|uint16(decodedBytes[i+1]))
}
}
}
decodedString = string(utf16.Decode(utf16Bytes))
fmt.Println(decodedString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment