📄 bytechange.java
字号:
package com.sxit.dl;import java.text.*;/** * <p>名称: ByteChange.java</p> * <p>功能: 提供byte[]与int,string之间的转换</p> * <p>日期: 2005/12/6</p> * <p>作者: TOMPAN</p> * not attributable */public class ByteChange { public ByteChange() { } //a.将长整数转化为byte[],高位在前。 public static byte[] longToBytes(long value){ byte[] b = new byte[4]; b[0] = (byte)(value >> 24); b[1] = (byte)(value >> 16); b[2] = (byte)(value >> 8); b[3] = (byte)value ; return b; } //b.将byte[]转化为int,在java中int占4*8位,不必使用long. public static int bytesToInt(byte[] b,int pos){ int b1,b2,b3,b4; b1 = (int)b[pos]; b2 = (int)b[pos+1]; b3 = (int)b[pos+2]; b4 = (int)b[pos+3]; if(b1<0){ b1 = b1 + 256; } if(b2<0){ b2 = b2 + 256; } if(b3<0){ b3 = b3 + 256; } if(b4<0){ b4 = b4 + 256; } return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4; } //c.将字符串转化为byte[] public static byte[] strToBytes(String s){ return s.getBytes(); } //d.将byte[]转化为String public static String bytesToStr(byte[] b,int pos,int len){ byte[] rb = new byte[len]; System.arraycopy(b,pos,rb,0,len); return new String(rb); } //e.将byte[](含中文)转化为String,以GBK编码,这个不需要逆向工程 public static String gbbytesToStr(byte[] b,int pos,int len){ String s = ""; byte[] rb = new byte[len]; System.arraycopy(b,pos,rb,0,len); try{ s = new String(rb,"GBK"); }catch(java.io.UnsupportedEncodingException e){ } return s; } //获取当前格式化时间 public static String getCurrentTime(){ java.util.Date currentTime = new java.util.Date(); SimpleDateFormat simpDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String currentTimeStr = simpDateFormat.format(currentTime); return currentTimeStr; } //UNICODE转换为String public static String UnicodeToStr(String UnicodeStr) throws Exception { UnicodeStr = UnicodeStr.toUpperCase(); int ii = 0; String str = ""; String sx = ""; while (ii + 4 <= UnicodeStr.length()) { sx = UnicodeStr.substring(ii, ii + 4); str = str + HexStrToChar4(sx); ii = ii + 4; }// str = new String(str.getBytes("ISO-8859-1"),"gb2312"); return str; } public static String AsciiToStr(String AsciiStr) throws Exception { int ii = 0; String str = ""; String sx = ""; while (ii + 2 <= AsciiStr.length()) { sx = AsciiStr.substring(ii, ii + 2); str = str + HexStrToChar2(sx); ii = ii + 2; } str = new String(str.getBytes("ISO-8859-1"),"gb2312"); return str; } private static char HexStrToChar2(String HexStr) { if (HexStr.length() != 2) return (char)0; int ii; ii = CharToInt(HexStr.charAt(0))*16 + CharToInt(HexStr.charAt(1)); return (char)ii; } public static char HexStrToChar4(String HexStr) { if (HexStr.length() != 4) return (char)0; int ii; ii = CharToInt(HexStr.charAt(0))*256*16 + CharToInt(HexStr.charAt(1))*256 + CharToInt(HexStr.charAt(2))*16 + CharToInt(HexStr.charAt(3)); return (char)ii; } public static int CharToInt(char c) { int re; re = (int)c; if ( (re <= 57) && (re >= 48) ) re = re - 48; else if ( (re >= 65) && (re <= 70) ) re = re - 55; else re = 0; return re; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -