English 中文(简体)
Convert @ sign from byte in GSM 7-bit encoding to Java text
原标题:Convert @ sign from byte in GSM 7-bit encoding to Java text
  • 时间:2018-12-17 06:48:36
  •  标签:
  • java

我已给出一个星号<代码>[97、98、0、99、100],即GSSM 7-Bit encoded. 更正应作在印发的记录上,由有关的代表团成员一人署名,送交逐字记录处处长(C-178)。 当我试图将这一阵列改成<代码>StringBuilder时,我无法转换@

我的守则是:

byte[] byteFinal ={97, 98, 0, 99, 100};
char ch;
StringBuilder str = new StringBuilder();
for(byte b : byteFinal){
    ch  = (char)b;
    System.out.println("ch:"+ch);
    str.append(ch);

}
System.out.println(str.toString());
最佳回答

根据你在其他答复中的评论,问题是由于对GSM 7-bit编码处理不当造成的。

你们可以治疗男权运动 7 比方是一种不同的特性,因此,你必须使用 by形式的 array形,并 cast取。 只有在UTF-8/ASCII或类似的编码中,才选择代谢,其特性低于法典第128条。

Java似乎没有为GSM 7-bit(因此,你本可以做像这样的事情)。 String results = 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/:

static final char[] GSM7CHARS = {
        0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
        0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
        0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
        0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
        0x0020, 0x0021, 0x0022, 0x0023, 0x00A4, 0x0025, 0x0026, 0x0027,
        0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
        0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
        0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
        0x00A1, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
        0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
        0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
        0x0058, 0x0059, 0x005A, 0x00C4, 0x00D6, 0x00D1, 0x00DC, 0x00A7,
        0x00BF, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
        0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
        0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
        0x0078, 0x0079, 0x007A, 0x00E4, 0x00F6, 0x00F1, 0x00FC, 0x00E0};

static final char[] ESCAPE = {
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000,  
   , 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000,  ^    , 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
         {    ,  }    , 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,  \ ,
        0x0000, 0x0000, 0x0000, 0x0000,  [    ,  ~    ,  ]    , 0x0000,
         |    , 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x20AC, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
        // or use -1 instead of 0x0000, depending on your preference

//...

byte[] byteFinal ={97, 98, 0, 99, 100};
StringBuilder sb = new StringBuilder();
boolean escape = false
for(byte b : byteFinal){
    if (b >= 0) {
        if (escape) {
            sb.append(ESCAPE[b] > 0 ? ESCAPE[b] : GSMCHARS[b]);
            escape = false;
        } else {
            if (b == 27) {  // escape
                escape = true;
            } else { 
                sb.append(GSM7CHARS[b]);
            }
        }
    }
}
System.out.println(sb.toString());

更新:

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)

然而,这至少使大家认为,必须进行一些研究,而不是仅仅 by忙地进行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

通过使用这一条,你只能做像<代码”这样的事情。 String results = new String (byteFinal, GSM_7_BIT_CHARSET); withoutughing with all those Internals of GSM 7 bit


更新:

由于对@user432024的评论,似乎还有另一个焦炭平衡含有GSM-7-Bit:

问题回答

变化阵列:

byte[] byteFinal ={97, 98, 64, 99, 100};

Ascii代码@ 64。 顺便提一下:. Caret notation of NUL nature (ascii Code 0) is ^@, 似乎在此混淆了你。

You are using ascii values of characters in your byte array.

这里,64 相当于你后面的@特性的二倍值。

因此,你们的阵容应当:

byte[] byteFinal ={97, 98, 64, 99, 100};
                           ^^

查阅wiki 。 零的乙.价值等于无效。

此外,为了创造力,你只能制造如下面纱,而不是使用斯特林:

System.out.println(new String(byteFinal));

因此,你们都需要两条法典:

byte[] byteFinal ={97, 98, 64, 99, 100};
System.out.println(new String(byteFinal));

您也可在校准中安装果园,并使用getBytes (“SCGSM”)

There is the library jCharset. When the library is on the class path it will be automatically added to the available charsets.

import java.io.UnsupportedEncodingException;

class Scratch {
    public static void main(String[] args) throws UnsupportedEncodingException {
        byte[] encoded = "something".getBytes("GSM7");
        System.out.println(new String(new byte[]{97, 98, 0, 99, 100}, "GSM7"));
    }
}

ab@cd Here are the Maven coordinates.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签