java 中 byte Ascii Hex int short 字串之間的互轉
阿新 • • 發佈:2019-02-13
之前寫過一個C++中ascii碼字串、十六進位制字串、byte字串之間的互轉 的文章 http://blog.csdn.net/jimbo_lee/article/details/10404295
先整一個java常用的byte字串、asciii碼字串、十六進位制字串的互相轉換, 留底以備後用
import android.graphics.Bitmap; import android.graphics.Matrix; import java.io.UnsupportedEncodingException; import java.util.Arrays; public class StrUtil { public static int hexCharToInt(char c) { if ((c >= '0') && (c <= '9')) return (c - '0'); if ((c >= 'A') && (c <= 'F')) return (c - 'A' + 10); if ((c >= 'a') && (c <= 'f')) return (c - 'a' + 10); throw new RuntimeException("invalid hex char '" + c + "'"); } public static String bytesToAscii(byte[] bytes, int offset, int dateLen) { if ((bytes == null) || (bytes.length == 0) || (offset < 0) || (dateLen <= 0)) { return null; } if ((offset >= bytes.length) || (bytes.length - offset < dateLen)) { return null; } String asciiStr = null; byte[] data = new byte[dateLen]; System.arraycopy(bytes, offset, data, 0, dateLen); try { asciiStr = new String(data, "ISO8859-1"); } catch (UnsupportedEncodingException e) { } return asciiStr; } public static String bytesToAscii(byte[] bytes, int dateLen) { return bytesToAscii(bytes, 0, dateLen); } public static String bytesToAscii(byte[] bytes) { return bytesToAscii(bytes, 0, bytes.length); } public static String bytesToHexString(byte[] bytes, int offset, int len) { if (bytes == null) return "null!"; StringBuilder ret = new StringBuilder(2 * len); for (int i = 0; i < len; ++i) { int b = 0xF & bytes[(offset + i)] >> 4; ret.append("0123456789abcdef".charAt(b)); b = 0xF & bytes[(offset + i)]; ret.append("0123456789abcdef".charAt(b)); } return ret.toString(); } public static String bytesToHexString(byte[] bytes, int len) { return ((bytes == null) ? "null!" : bytesToHexString(bytes, 0, len)); } public static String bytesToHexString(byte[] bytes) { return ((bytes == null) ? "null!" : bytesToHexString(bytes, bytes.length)); } public static byte[] hexStringToBytes(String s) { if (s == null) return null; int sz = s.length(); try { byte[] ret = new byte[sz / 2]; for (int i = 0; i < sz; i += 2) { ret[(i / 2)] = (byte) (hexCharToInt(s.charAt(i)) << 4 | hexCharToInt(s.charAt(i + 1))); } return ret; } catch (RuntimeException re) { } return null; } public static byte[] shortToBytesLe(short shortValue) { byte[] bytes = new byte[2]; for (int i = 0; i < bytes.length; ++i) { bytes[i] = (byte) (shortValue >> i * 8 & 0xFF); } return bytes; } public static byte[] shortToBytesBe(short shortValue) { byte[] bytes = new byte[2]; for (int i = 0; i < bytes.length; ++i) { bytes[(bytes.length - i - 1)] = (byte) (shortValue >> i * 8 & 0xFF); } return bytes; } public static byte[] intToBytesLe(int intValue) { byte[] bytes = new byte[4]; for (int i = 0; i < bytes.length; ++i) { bytes[i] = (byte) (intValue >> i * 8 & 0xFF); } return bytes; } public static byte[] intToBytesBe(int intValue) { byte[] bytes = new byte[4]; for (int i = 0; i < bytes.length; ++i) { bytes[(bytes.length - i - 1)] = (byte) (intValue >> i * 8 & 0xFF); } return bytes; } public static int bytesToIntLe(byte[] bytes) { if ((bytes == null) || (bytes.length > 4)) { throw new RuntimeException("invalid arg"); } int ret = 0; for (int i = 0; i < bytes.length; ++i) { ret += ((bytes[i] & 0xFF) << i * 8); } return ret; } public static int bytesToIntLe(byte[] data, int start, int end) { return bytesToIntLe(Arrays.copyOfRange(data, start, end)); } public static int bytesToIntBe(byte[] bytes) { if ((bytes == null) || (bytes.length > 4)) { throw new RuntimeException("invalid arg"); } int ret = 0; for (int i = 0; i < bytes.length; ++i) { ret += ((bytes[i] & 0xFF) << (bytes.length - i - 1) * 8); } return ret; } public static int bytesToIntBe(byte[] data, int start, int end) { return bytesToIntBe(Arrays.copyOfRange(data, start, end)); } public static int bytesToIntLe(byte b0, byte b1, byte b2, byte b3) { int ret = 0; ret = b0 & 0xFF; ret += ((b1 & 0xFF) << 8); ret += ((b2 & 0xFF) << 16); ret += ((b3 & 0xFF) << 24); return ret; } public static int bytesToIntBe(byte b0, byte b1, byte b2, byte b3) { int ret = 0; ret = (b0 & 0xFF) << 24; ret += ((b1 & 0xFF) << 16); ret += ((b2 & 0xFF) << 8); ret += (b3 & 0xFF); return ret; } public static void byteArraySetByte(byte[] bytesArray, byte setValue, int index) { bytesArray[index] = setValue; } public static void byteArraySetByte(byte[] bytesArray, int setValue, int index) { bytesArray[index] = (byte) (setValue & 0xFF); } public static void byteArraySetBytes(byte[] bytesArray, byte[] setValues, int index) { System.arraycopy(setValues, 0, bytesArray, index, setValues.length); } public static void byteArraySetWord(byte[] bytesArray, int setValue, int index) { bytesArray[index] = (byte) (setValue & 0xFF); bytesArray[(index + 1)] = (byte) (setValue >> 8 & 0xFF); } public static void byteArraySetWordBe(byte[] bytesArray, int setValue, int index) { bytesArray[index] = (byte) (setValue >> 8 & 0xFF); bytesArray[(index + 1)] = (byte) (setValue & 0xFF); } public static void byteArraySetInt(byte[] bytesArray, int setValue, int index) { bytesArray[index] = (byte) (setValue & 0xFF); bytesArray[(index + 1)] = (byte) (setValue >> 8 & 0xFF); bytesArray[(index + 2)] = (byte) (setValue >> 16 & 0xFF); bytesArray[(index + 3)] = (byte) (setValue >> 24 & 0xFF); } public static void byteArraySetIntBe(byte[] bytesArray, int setValue, int index) { bytesArray[index] = (byte) (setValue >> 24 & 0xFF); bytesArray[(index + 1)] = (byte) (setValue >> 16 & 0xFF); bytesArray[(index + 2)] = (byte) (setValue >> 8 & 0xFF); bytesArray[(index + 3)] = (byte) (setValue & 0xFF); } public static void delayms(int ms) { if (ms <= 0) return; try { Thread.sleep(ms); } catch (InterruptedException e) { } } public static Bitmap bitmapRotate(Bitmap b, int degrees) { if ((degrees != 0) && (b != null)) { Matrix m = new Matrix(); m.setRotate(degrees, b.getWidth() / 2.0F, b.getHeight() / 2.0F); try { Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true); if (b != b2) { b.recycle(); b = b2; } } catch (OutOfMemoryError ex) { } } return b; } public static boolean isAscii(char ch) { return (ch <= ''); } public static boolean isAscii(String text) { for (int i = 0; i < text.length(); ++i) { if (!(isAscii(text.charAt(i)))) { return false; } } return true; } public static boolean hasAsciiChar(String text) { for (int i = 0; i < text.length(); ++i) { if (isAscii(text.charAt(i))) { return true; } } return false; } }
import java.io.ByteArrayOutputStream; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.DESedeKeySpec; /** * 位元組、整數、長整數等資料型別之間的拆分和轉換。<br/> * 還包括BCD碼,十六進位制碼,網路位元組序等等相互轉換 * * @author * @version 1.0 */ public class TypeConvert { /** * 位元組陣列轉換成整型。(網路位元組序,高位元組在前) * * @param b 位元組陣列。 * @param offset 待轉換位元組開始的位置。 * @return 整數形式。 */ public final static int byte2int(byte[] b, int offset) { return (b[offset + 3] & 0xff) | ((b[offset + 2] & 0xff) << 8) | ((b[offset + 1] & 0xff) << 16) | ((b[offset] & 0xff) << 24); } /** * 位元組陣列轉換成整型。(網路位元組序,高位元組在前) * * @param b 位元組陣列。 * @return 整數形式。 */ public final static int byte2int(byte[] b) { return (b[3] & 0xff) | ((b[2] & 0xff) << 8) | ((b[1] & 0xff) << 16) | ((b[0] & 0xff) << 24); } /** * 低位元組在前,高位元組在後位元組流轉換為整型 * * @param buf byte[] * @param offset int * @return int */ public final static int Sbyte2int(byte[] buf, int offset) { // 讀取4個. return (buf[offset] & 0xff) | ((buf[offset + 1] << 8) & 0xff00) | (((buf[offset + 2] << 16) & 0xff0000)) | (((buf[offset + 3] << 24) & 0xff000000)); } /** * 低位元組在前,高位元組在後整型轉換為字串 * * @param n int * @param buf byte[] * @param offset int */ public final static void Sint2byte(int n, byte[] buf, int offset) { buf[offset + 3] = (byte) (n >> 24); buf[offset + 2] = (byte) (n >> 16); buf[offset + 1] = (byte) (n >> 8); buf[offset + 0] = (byte) n; } /** * 整型轉換成16進位制,,負數最後要在最高位加1就是8 * * @param nInt int * @return 16進制度字串。 */ public final static String int2hex(int nInt) { int n = nInt, tmp; // 不斷的移動,4個分一小段,,移位和與或操作。 char[] bb = new char[8]; for (int i = 0; i < 32; i += 4) { tmp = (int) ((n >> i) & 0x0000000f); tmp = getHex(tmp); bb[7 - i / 4] = (char) tmp; } return new String(bb); } private final static String HEX = "0123456789ABCDEF"; public final static int hex2int(String hex) { int ret = 0; for (int i = 0, len = hex.length(); i < len; i++) { ret = (ret << 8) + HEX.indexOf(hex.charAt(i)); } return ret; } // 預設要求是大寫十六進位制,否則出錯 public final static byte hex2Byte(char hex) { byte b = (byte) HEX.indexOf(hex); return b; } /** * 將位元組陣列內容按照二進位制碼打印出來 * * @param buf byte[] * @return String */ public final static String byte2binary(byte[] buf) { int len = buf.length; StringBuilder chars = new StringBuilder(); byte tmp; int inch = 0; int j; for (int i = 0; i < len; i++) { tmp = buf[i]; chars.append(' '); for (j = 7; j >= 0; j--) { inch = ((tmp >> j) & 0x01); if (inch == 0) { chars.append('0'); } else { chars.append('1'); } } } return chars.substring(1); } /** * BCD碼轉為10進位制串(阿拉伯資料)<br/> * * BCD與十進位制字串的轉換.<br/> * BCD(Binary Coded Decimal)是用二進位制編碼表示的十進位制數,<br/> * 十進位制數採用0~9十個數字,是人們最常用的。在計算機中,同一個數可以用兩種BCD格式來表示:<br/> * ①壓縮的BCD碼 ②非壓縮的BCD碼<br/> * 壓縮的BCD碼:<br/> * 壓縮的BCD碼用4位二進位制數表示一個十進位制數位,整個十進位制數用一串BCD碼來表示。<br/> * 例如,十進位制數59表示成壓縮的BCD碼為0101 1001,十進位制數1946表示成壓縮的BCD碼為0001 1001 0100 0110。<br/> * 非壓縮的BCD碼:<br/> * 非壓縮的BCD碼用8位二進位制數表示一個十進位制數位,其中低4位是BCD碼,高4位是0。<br/> * 例如,十進位制數78表示成壓縮的BCD碼為0111 1000。<br/> * 從鍵盤輸入資料時,計算機接收的是ASCII碼,要將ASCII碼錶示的數轉換成BCD碼是很簡單的,<br/> * 只要把ASCII碼的高4位清零即可。反之,如果要把BCD碼轉換成ASII碼,只要把BCD碼 "或|"00110000即可。 * * @param bytes BCD碼 * @return String 10進位制串 */ public static String bcd2Str(byte[] bytes) { if (bytes.length == 0) { return ""; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { int h = ((bytes[i] & 0xff) >> 4) + 48; sb.append((char) h); int l = (bytes[i] & 0x0f) + 48; sb.append((char) l); } return sb.toString(); } /** * 10進位制串轉為BCD碼<br/> * * @param asc 10進位制串 * @return byte[] BCD碼 */ public static byte[] str2Bcd(String data) { if (data.length() == 0) { return new byte[0]; } String str = data; // 奇數個數字需左補零 if (str.length() % 2 != 0) { str = "0" + str; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); char[] cs = str.toCharArray(); for (int i = 0; i < cs.length; i += 2) { int high = cs[i] - 48; int low = cs[i + 1] - 48; baos.write(high << 4 | low); } return baos.toByteArray(); } /** * 將位元組陣列內容按照十六進位制碼打印出來 * * @param buf byte[] * @param split String 分隔符 * @return String */ public final static String bytes2hex(byte[] buf, String split) { if ((split == null) || (split.length() == 0)) { return bytes2hex(buf); } if ((buf == null) || (buf.length == 0)) { return ""; } StringBuilder chars = new StringBuilder(); for (byte tmp : buf) { chars.append(split); int inch = ((tmp >> 4) & 0x0F); inch = getHex(inch); chars.append((char) inch); inch = (tmp & 0x0F); inch = getHex(inch); chars.append((char) inch); } return chars.substring(split.length()); } /** * 將位元組陣列內容按照十六進位制碼打印出來 * * @param buf byte[] * @param split String 分隔符 * @return String */ public final static String bytes2hex(byte[] buf) { if ((buf == null) || (buf.length == 0)) { return ""; } StringBuilder chars = new StringBuilder(); for (byte b : buf) { int inch = ((b >> 4) & 0x0F); inch = getHex(inch); chars.append((char) inch); inch = (b & 0x0F); inch = getHex(inch); chars.append((char) inch); } return chars.toString(); } private static int getHex(int inch) { if (inch >= 10) { return inch + 55; } else { return inch + 48; } } public static String byte2hex(byte b) { String hex = ""; int inch = ((b >> 4) & 0x0F); inch = getHex(inch); hex += (char) inch; inch = (b & 0x0F); inch = getHex(inch); hex += (char) inch; return hex; } /** * 將十六進位制碼字串轉換成位元組陣列內容 * * @param str String * @return String */ public final static byte[] hex2bytes(String str) { str = str.toUpperCase(); int len = str.length() / 2; byte[] buf = new byte[len]; int inch = 0; for (int i = 0; i < len; i++) { inch = str.charAt(i + i); if (inch >= 65) { buf[i] = (byte) (((inch - 55) << 4) & 0xF0); } else { buf[i] = (byte) (((inch - 48) << 4) & 0xF0); } inch = str.charAt(i + i + 1); if (inch >= 65) { buf[i] = (byte) (buf[i] | ((inch - 55) & 0x0F)); } else { buf[i] = (byte) (buf[i] | ((inch - 48) & 0x0F)); } } return buf; } /*** * 根據實際長度返回,不補任何東西. * * @param aa int * @return String 一個16進位制的字串. */ public final static String int2hexA(int aa) { char[] cc = new char[8]; int len = 0; int tmp = 0; for (int offset = 28; offset >= 0; offset -= 4) { tmp = ((aa >> offset) & 0x0f); if ((tmp == 0) && (len == 0)) { // 開頭是0則省略調. continue; } else tmp = getHex(tmp); // 遇到正常字元. // len++; cc[len++] = (char) tmp; } return new String(cc, 0, len); } public final static void main(String[] args) { /* System.out.println(TypeConvert.byte2binary("asdf".getBytes())); System.out.println(TypeConvert.bytes2hex("asdf".getBytes(), " ")); byte[] buf = TypeConvert.hex2bytes("f1234f"); System.out.println(TypeConvert.bytes2hex(buf, "")); System.out.println(TypeConvert.int2hex(255).substring(6));*/ byte[] tmpbytes=binaryTobytes("010001000111010000000000000000000000010100000000000000000000000"); System.out.println(bytes2hex(tmpbytes)); System.out.println(byte2binary(hex2bytes("A23A000002800000"))); } /** * 位元組陣列轉換成長整型。(網路位元組序,高位元組在前) * * @param b 位元組陣列。 * @return 長整數形式。 */ public final static long byte2long(byte[] b) { return ((long) b[7] & 0xff) | (((long) b[6] & 0xff) << 8) | (((long) b[5] & 0xff) << 16) | (((long) b[4] & 0xff) << 24) | (((long) b[3] & 0xff) << 32) | (((long) b[2] & 0xff) << 40) | (((long) b[1] & 0xff) << 48) | ((long) b[0] << 56); } /** * 位元組陣列轉換成長整型。(網路位元組序,高位元組在前) * * @param b byte[] 位元組陣列。 * @param offset int * @return long 長整數形式。 */ public final static long byte2long(byte[] b, int offset) { return ((long) b[offset + 7] & 0xff) | (((long) b[offset + 6] & 0xff) << 8) | (((long) b[offset + 5] & 0xff) << 16) | (((long) b[offset + 4] & 0xff) << 24) | (((long) b[offset + 3] & 0xff) << 32) | (((long) b[offset + 2] & 0xff) << 40) | (((long) b[offset + 1] & 0xff) << 48) | ((long) b[offset] << 56); } /** * 整型轉換成位元組。(網路位元組序,高位元組在前) * * @param n 整數。 * @return 長度為4的位元組陣列。 */ public final static byte[] int2byte(int n) { byte[] b = new byte[4]; b[0] = (byte) (n >> 24); b[1] = (byte) (n >> 16); b[2] = (byte) (n >> 8); b[3] = (byte) n; return b; } /** * 整型轉換成位元組。(網路位元組序,高位元組在前) * * @param n 整數。 * @param buf 存放轉換結果的位元組陣列。 * @param offset 存放位置的偏移地址。 */ public final static void int2byte(int n, byte[] buf, int offset) { buf[offset] = (byte) (n >> 24); buf[offset + 1] = (byte) (n >> 16); buf[offset + 2] = (byte) (n >> 8); buf[offset + 3] = (byte) n; } /** * 短整型轉換成位元組。(網路位元組序,高位元組在前) * * @param n 整數。 * @return 長度為4的位元組陣列。 */ public final static byte[] short2byte(int n) { byte[] b = new byte[2]; b[0] = (byte) (n >> 8); b[1] = (byte) n; return b; } /** * 短整型轉換成位元組。(網路位元組序,高位元組在前) * * @param n 整數。 * @param buf 存放轉換結果的位元組陣列。 * @param offset 存放位置的偏移地址。 */ public final static void short2byte(int n, byte[] buf, int offset) { buf[offset] = (byte) (n >> 8); buf[offset + 1] = (byte) n; } /** * 長整型轉換成位元組。(網路位元組序,高位元組在前) * * @param n 長整數。 * @return 長度為8的位元組陣列。 */ public final static byte[] long2byte(long n) { byte[] b = new byte[8]; // b[0]=(byte)(n>>57); // comment by edong 20011203 b[0] = (byte) (n >> 56); b[1] = (byte) (n >> 48); b[2] = (byte) (n >> 40); b[3] = (byte) (n >> 32); b[4] = (byte) (n >> 24); b[5] = (byte) (n >> 16); b[6] = (byte) (n >> 8); b[7] = (byte) n; return b; } /** * 長整型轉換成位元組。(網路位元組序,高位元組在前) * * @param n 長整數。 * @param buf 存放轉換結果的位元組陣列。 * @param offset 存放位置的偏移地址。 */ public final static void long2byte(long n, byte[] buf, int offset) { // buf[offset]=(byte)(n>>57); // comment by edong // 20011203 buf[offset] = (byte) (n >> 56); buf[offset + 1] = (byte) (n >> 48); buf[offset + 2] = (byte) (n >> 40); buf[offset + 3] = (byte) (n >> 32); buf[offset + 4] = (byte) (n >> 24); buf[offset + 5] = (byte) (n >> 16); buf[offset + 6] = (byte) (n >> 8); buf[offset + 7] = (byte) n; } // **************低位元組在前******************* /** * 長整型轉換成位元組。(網路位元組序,高位元組在前) * * @param n 長整數。 * @param buf 存放轉換結果的位元組陣列。 * @param offset 存放位置的偏移地址。 * @author zhangyong 2004-05-27 */ public final static void Slong2byte(long n, byte[] buf, int offset) { buf[offset + 7] = (byte) (n >> 56); buf[offset + 6] = (byte) (n >> 48); buf[offset + 5] = (byte) (n >> 40); buf[offset + 4] = (byte) (n >> 32); buf[offset + 3] = (byte) (n >> 24); buf[offset + 2] = (byte) (n >> 16); buf[offset + 1] = (byte) (n >> 8); buf[offset] = (byte) n; } /** * 整型轉換成位元組。(網路位元組序,低位元組在前) * * @param iSource int 整數。 * @param buf byte[] 存放轉換結果的位元組陣列。 * @param offset int 存放位置的偏移地址。 * @return byte[] */ static public byte[] inttobyte(int iSource, byte[] buf, int offset) { buf[offset] = (byte) iSource; buf[offset + 1] = (byte) (iSource >> 8); buf[offset + 2] = (byte) (iSource >> 16); buf[offset + 3] = (byte) (iSource >> 24); return (buf); } /** * 短整數轉換成位元組。(網路位元組序,低位元組在前) * * @param n 短整數。 * @param buf 存放轉換結果的位元組陣列。 * @param offset 存放位置的偏移地址。 * @author zhangyong 2004-05-27 */ public final static void shorttobyte(int n, byte[] buf, int offset) { buf[offset] = (byte) n; buf[offset + 1] = (byte) (n >> 8); } /** * 短整數轉換成位元組。(網路位元組序,低位元組在前) * * @param b byte[] 存放轉換結果的位元組陣列。 * @param offset int 存放位置的偏移地址。 * @return int */ public final static int bytetoshort(byte[] b, int offset) { return (b[offset] & 0xff) | ((b[offset + 1] & 0xff) << 8); } /** * 位元組陣列轉換成整型。(網路位元組序,低位元組在前) * * @param b 位元組陣列。 * @param offset 待轉換位元組開始的位置。 * @return 整數形式。 */ public final static int bytetoint(byte[] b, int offset) { return (b[offset] & 0xff) | ((b[offset + 1] & 0xff) << 8) | ((b[offset + 2] & 0xff) << 16) | ((b[offset + 3] & 0xff) << 24); } /** * 位元組陣列轉換成整型。(網路位元組序,低位元組在前) * * @param b 位元組陣列。 * @return 整數形式。 */ public final static int bytetoint(byte[] b) { return (b[0] & 0xff) | ((b[1] & 0xff) << 8) | ((b[2] & 0xff) << 16) | ((b[3] & 0xff) << 24); } /** * 位元組陣列轉換成長整型。(網路位元組序,低位元組在前) * * @param b 位元組陣列。 * @return 長整數形式。 */ public final static long bytetolong(byte[] b) { // System.out.println("typeconvert="+((int)b[7])); // System.out.println("typeconvert"+((int)b[6])); // System.out.println("typeconvert"+((int)b[5])); // System.out.println("typeconvert"+((int)b[4])); // System.out.println("typeconvert"+((int)b[3])); // System.out.println("typeconvert"+((int)b[2])); // System.out.println("typeconvert"+((int)b[1])); // System.out.println("typeconvert"+((int)b[0])); return ((long) b[0] & 0xff) | (((long) b[1] & 0xff) << 8) | (((long) b[2] & 0xff) << 16) | (((long) b[3] & 0xff) << 24) | (((long) b[4] & 0xff) << 32) | (((long) b[5] & 0xff) << 40) | (((long) b[6] & 0xff) << 48) | ((long) b[7] << 56); } /** * 位元組陣列轉換成長整型。(網路位元組序,高位元組在前) * * @param b byte[] 位元組陣列。 * @param offset int * @return long 長整數形式。 */ public final static long bytetolong(byte[] b, int offset) { return ((long) b[offset] & 0xff) | (((long) b[offset + 1] & 0xff) << 8) | (((long) b[offset + 2] & 0xff) << 16) | (((long) b[offset + 3] & 0xff) << 24) | (((long) b[offset + 4] & 0xff) << 32) | (((long) b[offset + 5] & 0xff) << 40) | (((long) b[offset + 6] & 0xff) << 48) | ((long) b[offset + 7] << 56); } /* *//** * 二進位制字串轉為位元組陣列 *//* public final static byte[] binaryTobytes(String binary){ if(binary==null){ return null; } while(binary.length()%8!=0){ binary=binary+"0"; } int byteLen=binary.length()/8; byte[] desBytes=new byte[byteLen]; String tmpstr=null; int tmpint=0; int offset=1; for(int i=0 ;i<byteLen;i++){ tmpstr=binary.substring(i*8,i*8+8); System.out.println("轉換之前的二進位制字串:"+tmpstr); offset=1; tmpint=0; for(int j=8;j>0;j--){ if(tmpstr.substring(j-1,j).equals("1")){ tmpint=tmpint+offset; } offset=offset*2; } System.out.println("轉換之後的二進位制字串:"+Integer.toBinaryString(tmpint)); desBytes[i]=(byte)tmpint; } return desBytes; }*/ /** * 二進位制字串轉為位元組陣列 */ public final static byte[] binaryTobytes(String binary){ if(binary==null){ return null; } while(binary.length()%8!=0){ binary=binary+"0"; } int byteLen=binary.length()/8; String s=null; byte b; ByteArrayOutputStream tmpOs=new ByteArrayOutputStream(); for (int i = 0; i < byteLen;i++) { s = binary.substring(i*8, i*8 + 8); if (s.substring(0, 1).equals("1")) { s = "0" + s.substring(1); b = Byte.valueOf(s, 2); b |= 128; } else { b = Byte.valueOf(s, 2); } tmpOs.write(b); } return tmpOs.toByteArray(); } /** * 將位元組陣列內容按照二進位制碼打印出來 * * @param buf byte[] * @return String */ public final static String bytes2binary(byte[] buf) { int len = buf.length; StringBuilder chars = new StringBuilder(); byte tmp; int inch = 0; int j; for (int i = 0; i < len; i++) { tmp = buf[i]; //chars.append(' '); for (j = 7; j >= 0; j--) { inch = ((tmp >> j) & 0x01); if (inch == 0) { chars.append('0'); } else { chars.append('1'); } } } return chars.substring(0); } public static byte[] des(String keys, String value) throws Exception{ if(keys.length() == 32) { String s = keys.substring(0,16); keys += s; } byte[] key = TypeConvert.hex2bytes(keys); byte[] val = TypeConvert.hex2bytes(value); if (key.length == 8) { DESKeySpec desKey; desKey = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey sKey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, sKey); return cipher.doFinal(val); } else { DESedeKeySpec desKey; desKey = new DESedeKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey sKey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, sKey); return cipher.doFinal(val); } } /** * 敏感資訊加* 處理 * @param oldStr * @return string */ public static String coverString(String oldStr){ if(oldStr == null || oldStr.length() <= 0){ return oldStr; }else{ int oldLen= oldStr.length(); int secrLen = oldLen/3 ; String coverStr = oldStr.substring(oldLen/3, secrLen + oldLen/3); StringBuffer str = new StringBuffer(""); for(int i=0;i<coverStr.length();i++){ str.append('*'); } return oldStr.replace(coverStr, str); } } /** * 敏感資訊加* 處理 * @param oldStr * @return string */ public static String coverAllString(String oldStr){ if(oldStr == null || oldStr.length() <= 0){ return oldStr; }else{ return oldStr.replaceAll(".", "*"); } } }