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

📄 codetransfor.java

📁 代码转换工具类
💻 JAVA
字号:
package inspur.tax.wsbs.htcl.bsfileutil;

public class CodeTransfor {
	private final static byte[] hex = "0123456789ABCDEF".getBytes();

	/**
	 * BCD转换为16进制字符串
	 * 
	 * @param buf
	 * @param pos
	 * @param len
	 * @return
	 */
	public static String BCD2Str(byte buf[], int pos, int len) {
		StringBuffer hex = new StringBuffer();
		while (len-- > 0) {
			byte ch = buf[pos++];
			int d = (ch >> 4) & 0xf;
			hex.append((char) (d >= 10 ? 'a' - 10 + d : '0' + d));
			d = ch & 0xf;
			hex.append((char) (d >= 10 ? 'a' - 10 + d : '0' + d));
		}
		return hex.toString();
	}

	/**
	 * 将String转换为BCD编码
	 * 
	 * @param str
	 * @return
	 */
	public static byte[] Str2BCD(String str) {
		String s = str.toUpperCase();
		int ii = str.length();
		byte[] b = new byte[ii / 2];
		for (int i = 0, j = 0; i < ii; i++, j++) {
			int i1, i2;
			if (s.charAt(i) - 'A' >= 0)
				i1 = s.charAt(i) - 'A' + 10;
			else
				i1 = s.charAt(i) - '0';
			if (s.charAt(i + 1) - 'A' >= 0)
				i2 = s.charAt(i + 1) - 'A' + 10;
			else
				i2 = s.charAt(i + 1) - '0';

			b[j] = (byte) (i1 * 0x10 + i2);
			i = i + 1;
		}
		return b;
	}

	/**
	 * hex转换为int
	 * 
	 * @param b
	 * @param offset
	 * @param len
	 * @return
	 */
	public static int hex2Int(byte[] b, int offset, int len) {
		int result = 0;

		if (b == null) {
			return result;
		}
		for (int i = offset; i < (offset + len); i++) {
			result += (b[i] & 0xff) << (((offset + len) - 1 - i) * 8);
		}

		return result;
	}

	/**
	 * 将int转换为指定长度的String,如果长度不足,则在前面补ch
	 * 
	 * @param i
	 * @param ch
	 * @param length
	 * @return
	 */
	public static String int2Str(int i, String ch, int length) {
		String str = Integer.toString(i, 10);
		for (int j = str.length(); j < length; j++)
			str = ch + str;
		return str;
	}

	/**
	 * 将long转换为指定长度的String,如果长度不足,则在前面补ch
	 * 
	 * @param l
	 * @param ch
	 * @param length
	 * @return
	 */
	public static String long2Str(long l, String ch, int length) {
		String str = Long.toString(l, 10);
		for (int j = str.length(); j < length; j++)
			str = ch + str;
		return str;
	}

	/**
	 * 将hex转换为long型
	 * 
	 * @param b
	 * @param offset
	 * @param len
	 * @return
	 */
	public static long hex2Long(byte[] b, int offset, int len) {
		long result = 0;

		if (b == null) {
			return result;
		}
		for (int i = offset; i < (offset + len); i++) {
			result += (b[i] & 0xff) << (((offset + len) - 1 - i) * 8);
		}

		return result;
	}

	/**
	 * 将ASCII码转换为中文
	 * 
	 * @param strAscii
	 * @return
	 */
	public static String ascii2chinese(String strAscii) {
		String str = "";
		try {
			byte[] baKeyword = new byte[strAscii.length() / 2];
			for (int i = 0; i < baKeyword.length; i++) {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(strAscii
						.substring(i * 2, i * 2 + 2), 16));
			}
			str = new String(baKeyword, "GBK");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 将字节数组转换为十六进制字符串
	 * 
	 * @param b
	 * @param offset
	 * @param len
	 * @return
	 */
	public static String bytes2HexString(byte[] b, int offset, int len) {
		byte[] buff = new byte[2 * len];
		for (int i = 0; i < len; i++) {
			buff[2 * i] = hex[(b[offset + i] >> 4) & 0x0f];
			buff[2 * i + 1] = hex[b[offset + i] & 0x0f];
		}
		return new String(buff);
	}

	/**
	 * 将整形(十六进制值)转化成字节byte[]
	 * 
	 * @param hex
	 * @param size
	 * @return
	 */
	public static byte[] hex2Byte(int hex, int size) {
		byte[] bb = new byte[size];
		for (int i = 0; i < size; i++) {
			bb[i] = (byte) (((long) hex >> (i * 8)) & 0xFF);
		}
		return bb;
	}

	/**
	 * 将字符串str的左边添加指定的字符串ch以达到指定的长度len
	 * 
	 * @param str
	 * @param ch
	 * @param len
	 * @return
	 */
	public static String lPad(String str, String ch, int len) {
		String strData = str;
		while (strData.length() < len) {
			strData = ch + strData;
		}
		return strData;
	}

	/**
	 * 将字符串str的右边添加指定的字符串ch以达到指定的长度len
	 * 
	 * @param str
	 * @param ch
	 * @param len
	 * @return
	 */
	public static String rPad(String str, String ch, int len) {
		String strData = str;
		while (strData.length() < len) {
			strData = strData + ch;
		}
		return strData;
	}

	/**
	 * 将String转换为byte[]数组,转换后byte[]数组的长度与String的长度相等
	 * 
	 * @param str
	 * @return
	 */
	public static byte[] Str2Byte(String str) {
		byte[] b = new byte[str.length()];
		for (int i = 0; i < str.length(); i++) {
			b[i] = (byte) str.charAt(i);
		}
		return b;
	}

	/**
	 * 将字节数组反序排列
	 * 
	 * @param b
	 * @return
	 */
	public static byte[] reserveBytes(byte[] b) {
		byte[] ret = new byte[b.length];
		for (int i = 0; i < b.length; i++) {
			ret[i] = b[b.length - i - 1];
		}
		return ret;
	}
}

⌨️ 快捷键说明

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