public static String adnStringFieldToString(byte[] data,int offset,int length){
  if (length == 0) {
    return "";
  }
  if (length >= 1) {
    if (data[offset] == (byte)0x80) {
      int ucslen=(length - 1) / 2;
      String ret=null;
      try {
        ret=new String(data,offset + 1,ucslen * 2,"utf-16be");
      }
 catch (      UnsupportedEncodingException ex) {
        Log.e(LOG_TAG,"implausible UnsupportedEncodingException",ex);
      }
      if (ret != null) {
        ucslen=ret.length();
        while (ucslen > 0 && ret.charAt(ucslen - 1) == '\uFFFF')         ucslen--;
        return ret.substring(0,ucslen);
      }
    }
  }
  boolean isucs2=false;
  char base='\0';
  int len=0;
  if (length >= 3 && data[offset] == (byte)0x81) {
    len=data[offset + 1] & 0xFF;
    if (len > length - 3)     len=length - 3;
    base=(char)((data[offset + 2] & 0xFF) << 7);
    offset+=3;
    isucs2=true;
  }
 else   if (length >= 4 && data[offset] == (byte)0x82) {
    len=data[offset + 1] & 0xFF;
    if (len > length - 4)     len=length - 4;
    base=(char)(((data[offset + 2] & 0xFF) << 8) | (data[offset + 3] & 0xFF));
    offset+=4;
    isucs2=true;
  }
  if (isucs2) {
    StringBuilder ret=new StringBuilder();
    while (len > 0) {
      if (data[offset] < 0) {
        ret.append((char)(base + (data[offset] & 0x7F)));
        offset++;
        len--;
      }
      int count=0;
      while (count < len && data[offset + count] >= 0)       count++;
      ret.append(GsmAlphabet.gsm8BitUnpackedToString(data,offset,count));
      offset+=count;
      len-=count;
    }
    return ret.toString();
  }
  Resources resource=Resources.getSystem();
  String defaultCharset="";
  return GsmAlphabet.gsm8BitUnpackedToString(data,offset,length,defaultCharset.trim());
}

public static String adnStringFieldToString ( byte [ ] data , int offset , int length ) { if ( length == 0 ) { return "" ; } if ( length >= 1 ) { if ( data [ offset ] == ( byte ) 0x80 ) { int ucslen = ( length - 1 ) / 2 ; String ret = null ; try { ret = new String ( data , offset + 1 , ucslen * 2 , "utf-16be" ) ; } catch ( UnsupportedEncodingException ex ) { Log . e ( LOG_TAG , "implausible UnsupportedEncodingException" , ex ) ; } if ( ret != null ) { ucslen = ret . length ( ) ; while ( ucslen > 0 && ret . charAt ( ucslen - 1 ) == '' ) ucslen -- ; return ret . substring ( 0 , ucslen ) ; } } } boolean isucs2 = false ; char base = '\0' ; int len = 0 ; if ( length >= 3 && data [ offset ] == ( byte ) 0x81 ) { len = data [ offset + 1 ] & 0xFF ; if ( len > length - 3 ) len = length - 3 ; base = ( char ) ( ( data [ offset + 2 ] & 0xFF ) << 7 ) ; offset += 3 ; isucs2 = true ; } else if ( length >= 4 && data [ offset ] == ( byte ) 0x82 ) { len = data [ offset + 1 ] & 0xFF ; if ( len > length - 4 ) len = length - 4 ; base = ( char ) ( ( ( data [ offset + 2 ] & 0xFF ) << 8 ) | ( data [ offset + 3 ] & 0xFF ) ) ; offset += 4 ; isucs2 = true ; } if ( isucs2 ) { StringBuilder ret = new StringBuilder ( ) ; while ( len > 0 ) { if ( data [ offset ] < 0 ) { ret . append ( ( char ) ( base + ( data [ offset ] & 0x7F ) ) ) ; offset ++ ; len -- ; } int count = 0 ; while ( count < len && data [ offset + count ] >= 0 ) count ++ ; ret . append ( GsmAlphabet . gsm8BitUnpackedToString ( data , offset , count ) ) ; offset += count ; len -= count ; } return ret . toString ( ) ; } Resources resource = Resources . getSystem ( ) ; String defaultCharset = "" ; return GsmAlphabet . gsm8BitUnpackedToString ( data , offset , length , defaultCharset . trim ( ) ) ; }
decodes a string field that ' s formatted like the ef [ adn ] alpha identifier < p / > from ts 51 . 011 10 . 5 . 1 : coding : this alpha tagging shall use either - the sms default 7 bit coded alphabet as defined in ts 23 . 038 [ 12 ] with bit 8 set to 0 . the alpha identifier shall be left justified . unused bytes shall be set to ' ff ' ; or - one of the ucs2 coded options as defined in annex b . < p / > annex b from ts 11 . 11 v8 . 13 . 0 : 1 ) if the first octet in the alpha string is ' 80 ' , then the remaining octets are 16 bit ucs2 characters . . . 2 ) if the first octet in the alpha string is ' 81 ' , then the second octet contains a value indicating the number of characters in the string , and the third octet contains an 8 bit number which defines bits 15 to 8 of a 16 bit base pointer , where bit 16 is set to zero and bits 7 to 1 are also set to zero . these sixteen bits constitute a base pointer to a " half page " in the ucs2 code space , to be used with some or all of the remaining octets in the string . the fourth and subsequent octets contain codings as follows : if bit 8 of the octet is set to zero , the remaining 7 bits of the octet contain a gsm default alphabet character , whereas if bit 8 of the octet is set to one , then the remaining seven bits are an offset value added to the 16 bit base pointer defined earlier . . . 3 ) if the first octet of the alpha string is set to ' 82 ' , then the second octet contains a value indicating the number of characters in the string , and the third and fourth octets contain a 16 bit number which defines the complete 16 bit base pointer to a " half page " in the ucs2 code space . . .
