📄 pduutil.java
字号:
import java.math.BigInteger;
public class PduUtil {
public static String Byte2Hex(int bt)
{
if(bt>255) bt=bt & 0xFF;
String hex=Integer.toHexString(bt);
if(hex.length()<2) hex="0"+hex;
return hex.toUpperCase();
}
public static String Int2Hex(int c)
{
c=c & 0xFFFF;
String hex=Integer.toHexString(c);
hex="000"+hex;
return hex.substring(hex.length()-4).toUpperCase();
}
public static boolean IsEnglish(String msg)
{ long code=0;
for(int i=0;i<msg.length();i++)
{
code=(long)msg.charAt(i);
if(code>127) return false;
}
return true;
}
public static String Msg2Hex(String msg,boolean IsEnglish) throws Exception
{
String ss=new String();
String code=null;
for(int i=0;i<msg.length();i++)
{
if(IsEnglish) code=Byte2Hex((int)msg.charAt(i)); else code=PduUtil.Int2Hex((int)msg.charAt(i));
ss=ss+code;
}
return ss;
}
public static String Msg2GsmHex(String msg) throws Exception
{//gsm 7bit编码的字符串
String hex=new String();
String temp=null;
int msglen=msg.length();
String S=new String();
for(int i=0;i<msglen; i++)
{
temp=Integer.toBinaryString((int)msg.charAt(i));
temp="0000000"+temp;
temp=temp.substring(temp.length()-7);
S=temp+S;
}
BigInteger BI=new BigInteger(S,2);
temp=BI.toString(16).toUpperCase();
if(temp.length()%2==1) temp="0"+temp;
int len=temp.length();
for(int i=0;i<len;i=i+2)
{
hex=hex+temp.charAt(len-1-i-1);
hex=hex+temp.charAt(len-1-i);
}
return hex;
}
public static String Tel2DA(String da) throws Exception
{
String s;
String DA;
String DAhead;
int dalen;
int ta;
if(da.startsWith("+"))
{
s=da.substring(1);
ta=0x91;
}else
{
s=da;
ta=0x81;
}
//06 81 214365
dalen=s.length();
DA=SwapByte(s);
DAhead=Byte2Hex(dalen)+
Byte2Hex(ta)+DA;
return DAhead;
}
public static int ReadByte(String hex,int pos)throws Exception
{
return Integer.parseInt((hex.substring(pos,pos+2)),16);
}
public static int ReadWord(String hex,int pos) throws Exception
{//字节翻转
//int B0=ReadByte(hex,pos);
//int B1=ReadByte(hex,pos+2);
//return B0+B1*256;
return Integer.parseInt(hex.substring(pos, pos+4), 16);
}
public static String Hex2UnicodeMsg(String hex)throws Exception
{
int len=hex.length()/4;
int k;
char ch;
char[] chs=new char[len];
for(int i=0;i<len;i++)
{
k=Integer.parseInt(hex.substring(i*4,i*4+4), 16);
ch=(char)k;
chs[i]=ch;
}
String msg=new String(chs);
return msg;
}
public static String Hex2AnsiMsg(String hex) throws Exception
{
int len=hex.length()/2;
int k;
char ch;
char[] chs=new char[len];
for(int i=0;i<len;i++)
{
k=Integer.parseInt(hex.substring(i*2,i*2+2), 16);
ch=(char)k;
chs[i]=ch;
}
String msg=new String(chs);
return msg;
}
public static String Hex2ASC(String hex) throws Exception
{//读到以空结尾的PDU字符串;返回该串
int len=hex.length()/2;
int k;
char ch;
char[] chs=new char[len];
for(int i=0;i<len;i++)
{
k=Integer.parseInt(hex.substring(i*2,i*2+2), 16);
if(k==0)break;
ch=(char)k;
chs[i]=ch;
}
String msg=new String(chs);
return msg.trim();
}
public static String Hex2gsmMsg(String gsmhex) throws Exception
{ //使用BigInteger移位处理
String gsmb=new String();
int len=gsmhex.length();
//转换字节顺序
for(int i=0;i<len;i=i+2)
{
gsmb=gsmb+gsmhex.charAt(len-1-i-1);
gsmb=gsmb+gsmhex.charAt(len-1-i);
}
BigInteger bb=new BigInteger(gsmb,16);
int k=bb.bitLength();
BigInteger c;
BigInteger ff=BigInteger.valueOf(0x7f);
String msg=new String();
while(k>6)
{
c=bb.and(ff);
char cc=(char)c.intValue();
msg=msg+cc;
bb=bb.shiftRight(7);
k=bb.bitLength();
}
return msg;
}
public static String Hex2GsmMsg(String hex) throws Exception
{
int len=hex.length()/2;
int arrayLen=Math.round(len/7+0.5f);
String[] hexArray=new String[arrayLen];
for(int i=0;i<arrayLen;i++)
{ if(14*i+14>hex.length())
{
hexArray[i]=hex.substring(14*i);
hexArray[i]=hexArray[i]+"00000000000000";
hexArray[i]=hexArray[i].substring(0,14);
}else hexArray[i]=hex.substring(14*i, 14*i+14);
}
char[] chs=new char[8];
int[] chis=new int[7];
int chi;
String ss=new String();
for(int i=0;i<arrayLen;i++)
{
for (int k=0;k<7;k++) chis[k]=Integer.parseInt(hexArray[i].substring(2*k,2*k+2),16);
chs[0]=(char)( chis[0]&0x7F);
chi=chis[1]*256+chis[0];
chs[1]=(char)( (chi>>7)&0x7F);
chi=chis[2]*256+chis[1];
chs[2]=(char)( (chi>>6)&0x7F);
chi=chis[3]*256+chis[2];
chs[3]=(char)( (chi>>5)&0x7F);
chi=chis[4]*256+chis[3];
chs[4]=(char)( (chi>>4)&0x7F);
chi=chis[5]*256+chis[4];
chs[5]=(char)( (chi>>3)&0x7F);
chi=chis[6]*256+chis[5];
chs[6]=(char)( (chi>>2)&0x7F);
chi=chis[6];
chs[7]=(char)( (chi>>1)&0x7F);
String chStr=new String(chs);
chStr=chStr.trim();
ss=ss+chStr;
}
return ss;
}
public static String SwapByte(String hex)throws Exception
{
//683110801505F0
if(hex.length()%2==1)
hex=hex.concat("F");
char[] chs=hex.toCharArray();
char ch;
for(int i=0;i<chs.length;i=i+2)
{
ch=chs[i];
chs[i]=chs[i+1];
chs[i+1]=ch;
}
String S=new String(chs);
return S;
}
public static String ReadAddr( String hex)throws Exception
{
//91683110801505F0
int ta=Integer.parseInt(hex.substring(0,2),16);
String s=SwapByte(hex.substring(2));
s=s.replaceAll("F","");
if(ta==0x91) return "+"+s; else return s;
//A1 National 91 international 81 国内
}
public static String getTime(String SCTS)
{
int yy=Integer.parseInt(SCTS.substring(0,2));
if(yy<80) yy=2000+yy;else yy=1900+yy;
String mm=SCTS.substring(2,4);
String dd=SCTS.substring(4,6);
String hh=SCTS.substring(6,8);
String MM=SCTS.substring(8,10);
String SS=SCTS.substring(10,12);
String zz=SCTS.substring(12);
return yy+"-"+mm+"-"+dd+" "+hh+":"+MM+":"+SS+" +"+zz;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -