Created
March 31, 2024 13:28
-
-
Save parsibox/440d0694570db5dda3f2e3780f8f7d66 to your computer and use it in GitHub Desktop.
convert receive sms from kannel to utf8 with emoji
This file contains 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 ( | |
"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