⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _characterutil.java

📁 试用JAVA通过COMM口发送短信的代码.
💻 JAVA
字号:
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   _CharacterUtil.java

package org.cross.sms.msg;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class _CharacterUtil
{

    public _CharacterUtil()
    {
    }

    public static String intToHexString(int len)
    {
        String str1 = Integer.toHexString(len);
        if(str1.length() < 2)
            str1 = "0" + str1;
        return str1;
    }

    public static String getPduHexLength(String pdu, String encoding)
    {
        if(encoding.equals("gsm_default"))
        {
            int len = pdu.length() / 2;
            len *= 8;
            int l = len / 7;
            return intToHexString(l);
        } else
        {
            return intToHexString(pdu.length() / 2);
        }
    }

    public static String toBCDFormat(String s)
    {
        if(s.length() % 2 != 0)
            s = s + "F";
        String bcd = "";
        for(int i = 0; i < s.length(); i += 2)
            bcd = bcd + s.charAt(i + 1) + s.charAt(i);

        return bcd;
    }

    public static String hexToString(String hex, String charSet)
    {
        if(charSet.equalsIgnoreCase("gsm_unicode"))
            return hexToString_UniCode(hex);
        if(charSet.equalsIgnoreCase("gsm_default"))
            return hexToString_GSMDefault(hex);
        byte by[];
        by = new byte[hex.length() / 2];
        for(int i = 0; i < hex.length() / 2; i++)
        {
            int j = Integer.parseInt("" + hex.charAt(i * 2) + hex.charAt(i * 2 + 1), 16);
            by[i] = (byte)j;
        }

        try {
			return new String(by, charSet);
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
//        Exception ex;
//        ex;
//        ex.printStackTrace();
        return null;
    }

    public static String bytesToHexString(byte bys[])
    {
        int len = bys.length;
        String pdu = "";
        for(int i = 0; i < len; i++)
        {
            String str1 = Integer.toHexString(bys[i]);
            if(str1.length() != 2)
                str1 = "0" + str1;
            str1 = str1.substring(str1.length() - 2, str1.length());
            pdu = pdu + str1;
        }

        return pdu;
    }

    public static String stringToHexString(String str, String charSet)
    {
        if(charSet.equalsIgnoreCase("gsm_unicode"))
            return stringToHexString_UniCode(str);
        if(charSet.equalsIgnoreCase("gsm_default"))
            return stringToHexString_GSMDefault(str);
        byte by[];
		try {
			by = str.getBytes(charSet);
			return bytesToHexString(by);
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
        
//        Exception ex;
//        ex;
//        ex.printStackTrace();
        return null;
    }

    public static void main(String args[])
    {
        String pdu = "0500032C020162B219AD66BBE172B0986C46ABD96EB81CCC65DFBCD8723DDBB5668FC96C7A1E9D56EBF46CB98BCDCE87E372759ACD66B3CB65366A1E168FC965F619AD56AFD968F71B1E97CFE9F5FA1D9FD787C56316996D9EA337A9F47AEC46CBE372FB9C5EB7DFF179BD5D3C26B3D5EF313BBD1EA3E57571DC3E4FDBD977BC5DEF168FC965F579F34AA7D7";
        hexToString(pdu, "gsm_default");
        pdu = "0500032C020100B0986C46ABD96EB81C2C269BD16AB61B2E078BC966B49AED86CBC162B219AD66BBE172B0986C46ABD96EB81C2C269BD16AB61B2E078BC966B49AED86CBC162B219AD66BBE172B0986C46ABD96EB81C2C269BD16AB61B2E078BC966B49AED86CBC162B219AD66BBE172B0986C46ABD96EB81C2C269BD16AB61B2E078BC966B49AED86CBC162";
        hexToString(pdu, "gsm_default");
        pdu = stringToHexString("12345678", "gsm_default");
        System.out.println(pdu);
        System.out.println(hexToString(pdu, "gsm_default"));
    }

    public static String replaceSymbol(String text, String symbol, String value)
    {
        StringBuffer buffer;
        for(; text.indexOf(symbol) >= 0; text = buffer.toString())
        {
            buffer = new StringBuffer(text);
            buffer.replace(text.indexOf(symbol), text.indexOf(symbol) + symbol.length(), value);
        }

        return text;
    }

    private static byte[] hexStringToBytes(String pdu)
    {
        byte oldBytes[] = new byte[pdu.length() / 2];
        for(int i = 0; i < pdu.length() / 2; i++)
        {
            oldBytes[i] = (byte)(Integer.parseInt(pdu.substring(i * 2, i * 2 + 1), 16) * 16);
            oldBytes[i] += (byte)Integer.parseInt(pdu.substring(i * 2 + 1, i * 2 + 2), 16);
        }

        return oldBytes;
    }

    private static String hexToString_GSMDefault(String pdu)
    {
        byte oldBytes[] = hexStringToBytes(pdu);
        BitSet bitSet = new BitSet((pdu.length() / 2) * 8);
        int value1 = 0;
        for(int i = 0; i < pdu.length() / 2; i++)
        {
            for(int j = 0; j < 8; j++)
            {
                value1 = i * 8 + j;
                if((oldBytes[i] & 1 << j) != 0)
                    bitSet.set(value1);
            }

        }

        int value2 = ++value1 / 7;
        if(value2 == 0)
            value2++;
        byte newBytes[] = new byte[value2];
        for(int i = 0; i < value2; i++)
        {
            for(int j = 0; j < 7; j++)
                if(value1 + 1 > i * 7 + j && bitSet.get(i * 7 + j))
                    newBytes[i] |= (byte)(1 << j);

        }

        String text;
        if(newBytes[value2 - 1] == 0)
            text = new String(newBytes, 0, value2 - 1);
        else
            text = new String(newBytes);
        return text;
    }

    private static String hexToString_UniCode(String pdu)
    {
        int index = 0;
        String rtn = "";
        do
        {
            if(index >= pdu.length())
                break;
            try
            {
                int i = Integer.parseInt("" + pdu.charAt(index) + pdu.charAt(index + 1), 16);
                int j = Integer.parseInt("" + pdu.charAt(index + 2) + pdu.charAt(index + 3), 16);
                rtn = rtn + (char)(i * 256 + j);
                index += 4;
                continue;
            }
            catch(Exception ex) { }
            break;
        } while(true);
        return rtn;
    }

    private static String stringToHexString_UniCode(String str)
    {
        String str2 = "";
        for(int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            int high = c / 256;
            int low = c % 256;
            str2 = str2 + (Integer.toHexString(high).length() >= 2 ? Integer.toHexString(high) : "0" + Integer.toHexString(high));
            str2 = str2 + (Integer.toHexString(low).length() >= 2 ? Integer.toHexString(low) : "0" + Integer.toHexString(low));
        }

        return str2;
    }

    public static String offsetHexString(String pdu, int offsetNumber)
    {
        byte oldBytes[] = hexStringToBytes(pdu);
        BitSet bitSet = new BitSet(pdu.length() * 8 + offsetNumber);
        int value1 = 0 + offsetNumber;
        for(int i = 0; i < pdu.length() / 2; i++)
        {
            for(int j = 0; j < 8; j++)
            {
                value1 = i * 8 + j + offsetNumber;
                if((oldBytes[i] & 1 << j) != 0)
                    bitSet.set(value1);
            }

        }

        int value2 = ++value1 / 8;
        if(value1 % 8 != 0)
            value2++;
        if(value2 == 0)
            value2++;
        byte newBytes[] = new byte[value2];
        for(int i = 0; i < value2; i++)
        {
            for(int j = 0; j < 8; j++)
                if(value1 + 1 > i * 8 + j && bitSet.get(i * 8 + j))
                    newBytes[i] |= (byte)(1 << j);

        }

        for(pdu = bytesToHexString(newBytes); pdu.length() > 0 && pdu.substring(pdu.length() - 2).equals("00"); pdu = pdu.substring(0, pdu.length() - 2));
        return pdu;
    }

    private static String stringToHexString_GSMDefault(String text)
    {
        byte oldBytes[] = text.getBytes();
        BitSet bitSet = new BitSet(text.length() * 8);
        int value1 = 0;
        for(int i = 0; i < text.length(); i++)
        {
            for(int j = 0; j < 7; j++)
            {
                value1 = i * 7 + j;
                if((oldBytes[i] & 1 << j) != 0)
                    bitSet.set(value1);
            }

        }

        int value2;
        if((++value1 / 56) * 56 != value1)
            value2 = value1 / 8 + 1;
        else
            value2 = value1 / 8;
        if(value2 == 0)
            value2 = 1;
        byte newBytes[] = new byte[value2];
        for(int i = 0; i < value2; i++)
        {
            for(int j = 0; j < 8; j++)
                if(value1 + 1 > i * 8 + j && bitSet.get(i * 8 + j))
                    newBytes[i] |= (byte)(1 << j);

        }

        return bytesToHexString(newBytes);
    }

    public static boolean isContainsChineseChar(String text)
    {
        Pattern p = Pattern.compile("[\u4E00-\u9FA5]");
        Matcher m = p.matcher(text);
        return m.find();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -