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

📄 gb2pdu.java

📁 用JAVA实现串口GSM发信息
💻 JAVA
字号:
package com;

import java.io.*;

public class Gb2Pdu {

	// 处理短信中心号码
	public String adds(String adds) {
		if (adds == null)
			return null;
		adds = "86" + adds;
		if (adds.length() % 2 != 0)
			adds += 'F';
		String as = new String();
		for (int i = 0; i < adds.length() - 1; i += 2) {
			as += adds.charAt(i + 1);
			as += adds.charAt(i);
		}
		adds = "0891" + as;
		return adds;
	}

	// 处理目标手机号码号码
	public String phone(String phone) {
		if (phone == null)
			return null;
		phone = "86" + phone;
		if (phone.length() % 2 != 0)
			phone += 'F';
		String as = new String();
		for (int i = 0; i < phone.length() - 1; i += 2) {
			as += phone.charAt(i + 1);
			as += phone.charAt(i);
		}
		phone = as;
		phone = "11000D91" + phone + "000800";
		return phone;
	}

	// 将字符转化为Pdu格式
	public String charToUnicode(String str) {
		if (str == null)
			return null;
		String returnStr = "";
		try {
			char letter[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
			for (int i = 0; i < str.length(); i++) {
				int int0 = (int) str.charAt(i);// 将String转换成Int型
				if (true)// int0<0||int0>255 判断字符串是否为字母或数字
				{
					returnStr = returnStr;
					for (int m = 3; m >= 0; m--) {
						int int1 = ((int0 >>> (m * 4)) & 0x000f);
						if (int1 < 10)
							returnStr = returnStr + Integer.toString(int1);
						else
							returnStr = returnStr + letter[int1 - 0x0a];
					}
				} else {
					returnStr = returnStr + str.charAt(i);
				}
			}
		} catch (Exception e) {
			System.out.println("[Error] " + e.getMessage());
			return str;
		}
		return returnStr;
	}

	// 将Pdu格式转化为字符
	public String UnicodeToChar(String str)	 {
		StringBuffer sb = new StringBuffer();
		if (str == null)
			return null;

		int id = str.lastIndexOf('\"');// 查找字符串中 “ 号最后出现位置,信息内容从那开始
		if (id == -1)
			return str;
		else {
			while (str.charAt(id) < '0' || str.charAt(id) > 'F') {
				id++;
			}
			int ed = id;
			while (str.charAt(ed) >= '0' && str.charAt(ed) <= 'F') {
				ed++;
			}
			str = str.substring(id, ed);// 提取信息内容
			// System.out.println("转化前=" + str);

			for (int i = 0; i + 4 <= str.length(); i = i + 4) {
				try {
					int j = Integer.parseInt(str.substring(i, i + 4), 16);// 以16进制形式将字符串参数解析为有符号的整数
					sb.append((char) j);
				} catch (NumberFormatException e) {
					System.out.println("err:" + str + e.toString());
					return str;
				} catch (Exception es) {
					return str;
				}
			}
			String strs = new String(sb);
			return strs;
		}
	}

	public static void main(String[] agr) throws IOException {
		Gb2Pdu test = new Gb2Pdu();

		String adds = test.adds("13800769500");
		String phone = test.phone("15999771836");
		String msg = test.charToUnicode("JJ");
		int msgsize = msg.length() / 2;
		String size = Integer.toHexString(msgsize).toUpperCase();
		msg = size + msg;
		String pm = phone + msg;
		int pmsize = pm.length() / 2;
		String all = adds + pm;

		System.out.println("adds:\t" + adds);
		System.out.println("pmsize:\t" + pmsize);
		System.out.println("all:\t" + all);

	}
}

⌨️ 快捷键说明

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