private static int encodeText(CharSequence msg,int startpos,int count,StringBuilder sb,int initialSubmode){
  StringBuilder tmp=new StringBuilder(count);
  int submode=initialSubmode;
  int idx=0;
  while (true) {
    char ch=msg.charAt(startpos + idx);
switch (submode) {
case SUBMODE_ALPHA:
      if (isAlphaUpper(ch)) {
        if (ch == ' ') {
          tmp.append((char)26);
        }
 else {
          tmp.append((char)(ch - 65));
        }
      }
 else {
        if (isAlphaLower(ch)) {
          submode=SUBMODE_LOWER;
          tmp.append((char)27);
          continue;
        }
 else         if (isMixed(ch)) {
          submode=SUBMODE_MIXED;
          tmp.append((char)28);
          continue;
        }
 else {
          tmp.append((char)29);
          tmp.append((char)PUNCTUATION[ch]);
          break;
        }
      }
    break;
case SUBMODE_LOWER:
  if (isAlphaLower(ch)) {
    if (ch == ' ') {
      tmp.append((char)26);
    }
 else {
      tmp.append((char)(ch - 97));
    }
  }
 else {
    if (isAlphaUpper(ch)) {
      tmp.append((char)27);
      tmp.append((char)(ch - 65));
      break;
    }
 else     if (isMixed(ch)) {
      submode=SUBMODE_MIXED;
      tmp.append((char)28);
      continue;
    }
 else {
      tmp.append((char)29);
      tmp.append((char)PUNCTUATION[ch]);
      break;
    }
  }
break;
case SUBMODE_MIXED:
if (isMixed(ch)) {
tmp.append((char)MIXED[ch]);
}
 else {
if (isAlphaUpper(ch)) {
  submode=SUBMODE_ALPHA;
  tmp.append((char)28);
  continue;
}
 else if (isAlphaLower(ch)) {
  submode=SUBMODE_LOWER;
  tmp.append((char)27);
  continue;
}
 else {
  if (startpos + idx + 1 < count) {
    char next=msg.charAt(startpos + idx + 1);
    if (isPunctuation(next)) {
      submode=SUBMODE_PUNCTUATION;
      tmp.append((char)25);
      continue;
    }
  }
  tmp.append((char)29);
  tmp.append((char)PUNCTUATION[ch]);
}
}
break;
default :
if (isPunctuation(ch)) {
tmp.append((char)PUNCTUATION[ch]);
}
 else {
submode=SUBMODE_ALPHA;
tmp.append((char)29);
continue;
}
}
idx++;
if (idx >= count) {
break;
}
}
char h=0;
int len=tmp.length();
for (int i=0; i < len; i++) {
boolean odd=(i % 2) != 0;
if (odd) {
h=(char)((h * 30) + tmp.charAt(i));
sb.append(h);
}
 else {
h=tmp.charAt(i);
}
}
if ((len % 2) != 0) {
sb.append((char)((h * 30) + 29));
}
return submode;
}

private static int encodeText ( CharSequence msg , int startpos , int count , StringBuilder sb , int initialSubmode ) { StringBuilder tmp = new StringBuilder ( count ) ; int submode = initialSubmode ; int idx = 0 ; while ( true ) { char ch = msg . charAt ( startpos + idx ) ; switch ( submode ) { case SUBMODE_ALPHA : if ( isAlphaUpper ( ch ) ) { if ( ch == ' ' ) { tmp . append ( ( char ) 26 ) ; } else { tmp . append ( ( char ) ( ch - 65 ) ) ; } } else { if ( isAlphaLower ( ch ) ) { submode = SUBMODE_LOWER ; tmp . append ( ( char ) 27 ) ; continue ; } else if ( isMixed ( ch ) ) { submode = SUBMODE_MIXED ; tmp . append ( ( char ) 28 ) ; continue ; } else { tmp . append ( ( char ) 29 ) ; tmp . append ( ( char ) PUNCTUATION [ ch ] ) ; break ; } } break ; case SUBMODE_LOWER : if ( isAlphaLower ( ch ) ) { if ( ch == ' ' ) { tmp . append ( ( char ) 26 ) ; } else { tmp . append ( ( char ) ( ch - 97 ) ) ; } } else { if ( isAlphaUpper ( ch ) ) { tmp . append ( ( char ) 27 ) ; tmp . append ( ( char ) ( ch - 65 ) ) ; break ; } else if ( isMixed ( ch ) ) { submode = SUBMODE_MIXED ; tmp . append ( ( char ) 28 ) ; continue ; } else { tmp . append ( ( char ) 29 ) ; tmp . append ( ( char ) PUNCTUATION [ ch ] ) ; break ; } } break ; case SUBMODE_MIXED : if ( isMixed ( ch ) ) { tmp . append ( ( char ) MIXED [ ch ] ) ; } else { if ( isAlphaUpper ( ch ) ) { submode = SUBMODE_ALPHA ; tmp . append ( ( char ) 28 ) ; continue ; } else if ( isAlphaLower ( ch ) ) { submode = SUBMODE_LOWER ; tmp . append ( ( char ) 27 ) ; continue ; } else { if ( startpos + idx + 1 < count ) { char next = msg . charAt ( startpos + idx + 1 ) ; if ( isPunctuation ( next ) ) { submode = SUBMODE_PUNCTUATION ; tmp . append ( ( char ) 25 ) ; continue ; } } tmp . append ( ( char ) 29 ) ; tmp . append ( ( char ) PUNCTUATION [ ch ] ) ; } } break ; default : if ( isPunctuation ( ch ) ) { tmp . append ( ( char ) PUNCTUATION [ ch ] ) ; } else { submode = SUBMODE_ALPHA ; tmp . append ( ( char ) 29 ) ; continue ; } } idx ++ ; if ( idx >= count ) { break ; } } char h = 0 ; int len = tmp . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { boolean odd = ( i % 2 ) != 0 ; if ( odd ) { h = ( char ) ( ( h * 30 ) + tmp . charAt ( i ) ) ; sb . append ( h ) ; } else { h = tmp . charAt ( i ) ; } } if ( ( len % 2 ) != 0 ) { sb . append ( ( char ) ( ( h * 30 ) + 29 ) ) ; } return submode ; }
encode parts of the message using text compaction as described in iso / iec 15438 : 2001 ( e ) , chapter 4 . 4 . 2 .
