?? conversions.java
字號:
/* * File: Conversions.java * * Misc. data conversion routines. * * version 1.02 v1a * Copyright 1998, 1999 by Hush Communications Corporation, BWI */package hushcode;import java.math.BigInteger;final class Conversions{ static String bytesToHexString(byte[] b) { String outputString = ""; /* Create a byte array with an extra byte with MSB 0 * so BigInteger will return a positive number */ byte[] bb = new byte[b.length+1]; bb[0] = 127; System.arraycopy(b,0,bb,1,b.length); /* Create a BigInteger from the byte array, convert to a string * and chop off the first two characters, which represent * the previously added byte */ outputString = new BigInteger(bb).toString(16).substring(2); return outputString; } /** * This method accepts a hex string and returns a byte array. * The string must represent an integer number of bytes. */ static byte[] hexStringToBytes(String hex) { byte[] bigIntBytes = new BigInteger(hex,16).toByteArray(); if (bigIntBytes.length > hex.length()/2) { byte[] outbytes = new byte[hex.length()/2]; System.arraycopy(bigIntBytes, bigIntBytes.length-outbytes.length, outbytes, 0, outbytes.length); return outbytes; } else if (bigIntBytes.length < hex.length()/2) { byte[] outbytes = new byte[hex.length()/2]; System.arraycopy(bigIntBytes, 0, outbytes, outbytes.length - bigIntBytes.length, bigIntBytes.length); return outbytes; } else return bigIntBytes; } /* this function accepts 8 bytes and returns a long integer */ static long bytesToLong(byte[] bytes) { boolean negative = false; long returnLong = 0; for (int n=0; n<8; n++) { returnLong = returnLong << 8; long aByte = bytes[n]; if (aByte<0) aByte = aByte + 256; returnLong = returnLong | aByte; } return returnLong; } static byte[] longToBytes(long l) { long ll = l; long mask = 255; byte[] returnBytes = new byte[8]; long temp = 0; for (int n=7; n>=0; n--) { temp = ll & mask; if (temp > 127) temp = temp - 256; returnBytes[n] = (byte)ll; ll = ll >>> 8; } return returnBytes; } /* this function accepts 8 bytes and returns a long integer */ static int bytesToInt(byte[] bytes) { boolean negative = false; int returnInt = 0; for (int n=0; n<4; n++) { returnInt = returnInt << 8; int aByte = bytes[n]; if (aByte<0) aByte = aByte + 256; returnInt = returnInt | aByte; } return returnInt; } static byte[] intToBytes(int i) { int ii = i; int mask = 255; byte[] returnBytes = new byte[4]; int temp = 0; for (int n=3; n>=0; n--) { temp = ii & mask; if (temp > 127) temp = temp - 256; returnBytes[n] = (byte)ii; ii = ii >>> 8; } return returnBytes; }} // end Conversions
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -