📄 smspducodec.java
字号:
//// Copyright (c) 2001 Kvanttisofta oy. All rights reserved.////// SMS PDU coder/encoder class.// (aspa@users.sourceforge.net).//// $Id: SmsPduCodec.java,v 1.1.1.1 2001/04/18 04:19:00 aspa Exp $.//// (based on info from: http://www.sics.se/~lpe/help/sms/)//// TODO:// - more characters in GSM <==> ISO-8859-1 mapping.// - GSM 7-bit alphabet extension table character support.//////// NB1: with jikes you have to use the '-encoding 8859_1' option.//package fi.kvanttisofta.sms;public class SmsPduCodec { private static final char EMPTYCHAR = 0x100; private static final char EXTTABLEESCAPE = 0x1B; private static char[] gsmToIsoMap; // GSM ==> ISO88591 private static char[] gsmToIsoExtMap; private static char[] isoToGsmMap; // ISO88591 ==> GSM private static char[] isoToGsmExtMap; private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; // swaps characters of every two character substring of a string. public static String swapDigits(String str) { if( str == null ) return str; int strlen = str.length(); StringBuffer sb = new StringBuffer(strlen); for(int i=0; (i+1) < strlen; i=i+2) { sb.append(str.charAt(i+1)); sb.append(str.charAt(i)); } return new String(sb); } // returns a hex string representation of a 8 bit integer. public static String toHexString(int b) { char[] digits = new char[2]; b = b & 255; digits[0] = hexDigits[b / 0x10]; digits[1] = hexDigits[b % 0x10]; return new String(digits); } // returns an input string as a hex string. for debugging. public static String hexDump(String s) { StringBuffer dump = new StringBuffer(); for(int i = 0; i<s.length(); i++) dump.append(SmsPduCodec.toHexString(s.charAt(i))); return new String(dump); } // converts a string of 7 bit characters to a sequence of // of 8 bit bytes encoded as hex strings. public static String sevenBitEncode(String message) { if(message == null) return message; StringBuffer msg = new StringBuffer(message); StringBuffer encmsg = new StringBuffer(2*160); int bb = 0, bblen = 0, i; char o=0, c=0, tc; for(i=0; i < msg.length() || bblen>=8; i++) { if(i<msg.length()) { c = msg.charAt(i); tc = isoToGsmMap[c]; /* if(tc == EXTTABLEESCAPE) { //msg.insert(i+1, isoToGsmExtMap[c]); } */ c = tc; c &= ~(1 << 7); // clear (discard) eight bit. bb |= (c << bblen); // insert c to bb. bblen += 7; } while(bblen >= 8) { // we have a full octet. o = (char) (bb & 255); // take 8 bits. encmsg.append(SmsPduCodec.toHexString(o)); bb >>>= 8; bblen -= 8; } } // end: for(i=0; i<msglen || bblen>=8; i++) { if( (bblen > 0) ) encmsg.append(SmsPduCodec.toHexString(bb)); return encmsg.toString(); } public static String sevenBitDecode(String encmsg) { return sevenBitDecode(encmsg, encmsg.length()); } // converts a sequence of 8 bit bytes encoded as hex // strings to a string of 7 bit characters. public static String sevenBitDecode(String encmsg, int msglen) throws NumberFormatException { // encmsg: encoded string to decode. // msglen: the requested number of characters to decode. int i, o, r=0, rlen=0, olen=0, charcnt=0; // ints are 32 bit long. StringBuffer msg = new StringBuffer(160); int encmsglen = encmsg.length(); String ostr; boolean exttableescape = false; char c; // assumes even number of chars in octet string. for(i=0; ((i+1)<encmsglen) && (charcnt<msglen); i=i+2) { ostr = encmsg.substring(i, i+2); o = Integer.parseInt(ostr, 16); olen = 8; if(rlen >= 7) { // take a full char off remainder. c = (char) (r & 127); r >>>= 7; rlen -= 7; msg.append(c); charcnt++; } o <<= rlen; // push remainding bits from r to o. o |= r; olen += rlen; c = (char) (o & 127); // get first 7 bits from o. o >>>= 7; olen -= 7; r = o; // put remainding bits from o to r. rlen = olen; // handle character conversion. c = gsmToIsoMap[c]; if( c == EXTTABLEESCAPE ) { // ext table character handling. exttableescape = true; continue; } else if( exttableescape == true) { exttableescape = false; c = gsmToIsoExtMap[c]; } msg.append(c); charcnt++; } // end: for(i=0; ((i+1)<encmsglen) && (charcnt<msglen); i=i+2) { if( (rlen>0) && (charcnt<msglen) ) msg.append((char)r); return msg.toString(); } // class initialization. static { // construct GSM alphabet to/from ISO mappings. char[] gsmiso = { 0, '@', 1, '
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -