javaandroidussdsim900pdu

PDU and UCS2 in java ussd


I use SIM-900 in my device.

I need to get a balance from sim card.

I use this command and get this result.

Command Modem: AT+CUSD=1,"*555*1*2#" 

result:

+CUSD: 0,"06270639062A062806270631002006270635064406CC002000340039003900380039063106CC06270644002E00200031062F064206CC064206470020062A064506270633003D003100300030062F064206CC064206470020062A06450627063300200631062706CC06AF062706460020062F063106470645062706460020063106480632",72

I know this result is PDU format and UCS2, but I can't read text.

String pdu = "06270639062A062806270631002006270635064406CC002000340039003900380039063106CC06270644002E00200031062F064206CC064206470020062A064506270633003D003100300030062F064206CC064206470020062A06450627063300200631062706CC06AF062706460020062F063106470645062706460020063106480632"

SmsMessage smsMessage = SmsMessage.createFromPdu(pdu);

I use this site. and work but I can't in java android. enter image description here


Solution

  • 06270639062A... is 0627 0639 062A ....
    0627 is hex for Unicode character ا (ARABIC LETTER ALEF)
    0639 is hex for Unicode character ع (ARABIC LETTER AIN)
    062A is hex for Unicode character ت (ARABIC LETTER TEH)
    and so on...

    So, grap hex digits in blocks of 4, parse as hex and assign to char (since char is UTF-16 aka UCS2, like this:

    String input = "06270639062A062806270631002006270635064406CC002000340039003900380039063106CC06270644002E00200031062F064206CC064206470020062A064506270633003D003100300030062F064206CC064206470020062A06450627063300200631062706CC06AF062706460020062F063106470645062706460020063106480632";
    
    char[] buf = new char[input.length() / 4];
    for (int i = 0; i < buf.length; i++)
        buf[i] = (char) Integer.parseUnsignedInt(input.substring(i * 4, i * 4 + 4), 16);
    String output = new String(buf);
    
    System.out.println(output);
    

    Output

    اعتبار اصلی 49989ریال. 1دقیقه تماس=100دقیقه تماس رایگان درهمان روز