package com.core.common; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; public class RadixTools { public static void main(String[] args) { String val = toBinaryString((byte) 1); System.out.println(val); for (int i = val.length() - 1; i >= 0; i--) { char c = val.charAt(i); if (i == 7 && c == '1') { System.out.println("==="); } } } public static String toBinaryString(byte b) { return Long.toBinaryString((b & 0xFF) + 0x100L).substring(1); } public static String toBinaryString(byte[] bytes) { StringBuilder builder = new StringBuilder(); for (byte b : bytes) { builder.append(Long.toBinaryString((b & 0xFF) + 0x100L).substring(1)); } return builder.toString(); } public static byte[] hexStringToBytes(String hexString) { if (hexString == null || "".equals(hexString)) { return null; } String upper = hexString.toUpperCase(); int length = upper.length() / 2; char[] chars = upper.toCharArray(); byte[] result = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; result[i] = (byte) ((charToByte(chars[pos]) << 4) | charToByte(chars[pos + 1])); } return result; } public static String bytesToHexStr(byte[] bytes) { StringBuilder builder = new StringBuilder(bytes.length * 2); for (byte b : bytes) { builder.append(String.format("%02x", b & 0xFF)); } return builder.toString(); } private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } public static String bytesToStr(byte[] bytes) { return bytesToStr(bytes, Charset.forName("gbk")); } public static String bytesToStr(byte[] bytes, Charset charset) { return new String(bytes, charset).trim().toUpperCase(); } public static byte[] strToBytes(String str) throws UnsupportedEncodingException { return str.getBytes("gbk"); } public static byte[] longToBytes(long value) { long tmp = value; byte[] bytes = new byte[8]; for (int i = 0; i < bytes.length; i++) { bytes[i] = Long.valueOf(tmp & 0xFF).byteValue(); tmp = tmp >> 8; } return bytes; } public static long bytesToLong(byte[] bytes) { long result = 0L; long b0 = bytes[0] & 0xFFL; long b1 = (bytes[1] & 0xFFL) << 8; long b2 = (bytes[2] & 0xFFL) << 16; long b3 = (bytes[3] & 0xFFL) << 24; long b4 = (bytes[4] & 0xFFL) << 32; long b5 = (bytes[5] & 0xFFL) << 40; long b6 = (bytes[6] & 0xFFL) << 48; long b7 = (bytes[7] & 0xFFL) << 56; result = b0 | b1 | b2 | b3 | b4 | b5 | b6 | b7; return result; } public static byte[] intToBytes(int value) { int tmp = value; byte[] bytes = new byte[4]; for (int i = 0; i < bytes.length; i++) { bytes[i] = Integer.valueOf(tmp & 0xFF).byteValue(); tmp = tmp >> 8; } return bytes; } public static int bytesToInt(byte[] bytes) { int result; int b0 = bytes[0] & 0xFF; int b1 = (bytes[1] & 0xFF) << 8; int b2 = (bytes[2] & 0xFF) << 16; int b3 = (bytes[3] & 0xFF) << 24; result = b0 | b1 | b2 | b3; return result; } public static short byteToShort(byte[] bytes) { short result; short b0 = (short) (bytes[0] & 0xFF); short b1 = (short) ((bytes[1] & 0xFF) << 8); result = (short) (b0 | b1); return result; } public static byte[] shortToByte(short value) { int tmp = value; byte[] bytes = new byte[2]; for (int i = 0; i < bytes.length; i++) { bytes[i] = Integer.valueOf(tmp & 0xFF).byteValue(); tmp = tmp >> 8; } return bytes; } }