Methods Summary |
---|
public static byte[] | decode(byte[] gsm7bytes)Converts a GSM 7-bit encoded byte array into a UCS-2 byte array.
/*
* Initialize a buffer with expected size twice that of
* the 7-bit encoded text
*/
ByteArrayOutputStream bos =
new ByteArrayOutputStream(gsm7bytes.length * 2);
for (int i = 0; i < gsm7bytes.length; i++) {
/*
* Check for escaped characters first.
*/
if (gsm7bytes[i] == 0x1b) {
/*
* Advance the pointer past the escape.
*/
i++;
for (int j = 0; j < escaped7BitChars.length; j++) {
if (gsm7bytes[i] == escaped7BitChars[j]) {
bos.write(escapedUCS2[j] >> 8);
bos.write(escapedUCS2[j] & 0xFF);
break;
}
}
} else {
for (int j = 0; j < chars7Bit.length; j++) {
if (gsm7bytes[i] == chars7Bit[j]) {
bos.write(charsUCS2[j] >> 8);
bos.write(charsUCS2[j] & 0xFF);
break;
}
}
}
}
return bos.toByteArray();
|
public static byte[] | encode(byte[] ucsbytes)Converts a UCS-2 character array into GSM 7-bit bytes.
/*
* Initialize a buffer with expected size twice that of
* the 7-bit encoded text.
*/
ByteArrayOutputStream bos = new ByteArrayOutputStream(ucsbytes.length);
/*
* Walk through the UCS 2 characters 2 bytes at a time.
* All characters must be in the direct or extended UCS
* character tables. If not we reject the entire conversion.
*/
for (int i = 0; i < ucsbytes.length; i += 2) {
int j;
for (j = 0; j < charsUCS2.length; j++) {
if (ucsbytes[i] == (charsUCS2[j] >> 8) &&
ucsbytes[i+1] == (charsUCS2[j] & 0xFF)) {
bos.write(chars7Bit[j]);
break;
}
}
/*
* If you get to the end of the basic character table,
* check the extra escaped sequence table, too.
*/
if (j == charsUCS2.length) {
int k;
for (k = 0; k < escapedUCS2.length; k++) {
if (ucsbytes[i] == (escapedUCS2[k] >> 8) &&
ucsbytes[i+1] == (escapedUCS2[k] & 0xFF)) {
bos.write(0x1b);
bos.write(escaped7BitChars[k]);
break;
}
}
/*
* If no match is found in either table,
* return null to indicate UCS 2 characters
* were found that are not included in the
* GSM 7 bit encoding.
*/
if (k == escapedUCS2.length) {
return null;
}
}
}
return bos.toByteArray();
|
public static byte[] | toByteArray(java.lang.String data)Converts a string to a UCS-2 byte array.
char[] c = data.toCharArray();
ByteArrayOutputStream bos =
new ByteArrayOutputStream(data.length());
for (int i = 0; i < c.length; i ++) {
bos.write(c[i] >> 8);
bos.write(c[i] & 0xFF);
}
return bos.toByteArray();
|
public static java.lang.String | toString(byte[] ucsbytes)Gets a String from the UCS-2 byte array.
char[] c = new char [ucsbytes.length/2];
/*
* Create a string from the raw UCS 2 bytes.
*/
for (int i = 0; i < ucsbytes.length; i += 2) {
c[i/2] = (char)((ucsbytes[i] << 8)
+ (ucsbytes[i+1] & 0xFF));
}
return new String(c);
|