Skip to content
Advertisement

Convert @ sign from byte in GSM 7-bit encoding to Java text

I have given a byte array [97, 98, 0, 99, 100] which is GSM 7-Bit encoded. This should be converted into ab@cd. When I tried to append this given array into a StringBuilder, I was not able to convert the @ sign.

Here is my code:

JavaScript

Advertisement

Answer

Based on your comments in other answers, the problem is caused by missing handling of GSM 7-bit encoding.

You can treat GSM 7 Bit as a different character encoding, and you shouldn’t use byte array of such encoding as-is and cast each byte to char. Casting byte to char only works iff your bytes are in UTF-8/ASCII or similar encoding, and the characters are less than code point 128.

It seems Java does not provide a built-in Charset for GSM 7-bit (else, you could have done something like String result = new String(byteFinal, GSM_7_BIT_CHARSET);).

You need to handcraft the logic, which looks something like https://mnujali.wordpress.com/2011/12/01/gsm-7-bit-encodingdecoding-used-for-sms-and-ussd-strings-java-code/:

JavaScript

Update 1:

With some searching it seems GSM 7 bit encoding is a bit more complicated than what implemented above https://www.developershome.com/sms/gsmAlphabet.asp (Eg escaping etc)

However this at least give you idea on the need for handcrafting some lookup, instead of just casting the byte to char


Update 2:

It seems someone has implemented charset for GSM 7 bit: https://github.com/OpenSmpp/opensmpp/blob/master/charset/src/main/java/org/smpp/charset/Gsm7BitCharset.java

By using it, you can simply do something like String result = new String(byteFinal, GSM_7_BIT_CHARSET); without struggling with all those internals of GSM 7 bit

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement