이 기사에서는 이진 변환 도구 클래스의 Java 구현에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
import java.nio.charset.charset;/*** 16 진수 (16 진수 또는 첨자 16)는 16에서 1까지의 수학의 캐리 시스템입니다. 일반적으로 숫자 0에서 9로 표시되고 문자 a ~ f로 표시됩니다 (여기서 : a ~ f는 10 ~ 15). 예를 들어 <br> * 소수점 번호 57은 이진 111001로 작성되었으며 16 진 39는 16 진수로 작성됩니다. <br> * 16 진수와 소수점 값을 구별하기 위해 Java 및 C와 같은 언어는 16 진수 앞에서 0x를 접두사합니다. 예를 들어, 0x20은 10 진수에서 32입니다. 십진수 <br> * * 참조 : https://my.oschina.net/xinxingeya/blog/28766676 * * @author plublog/2876. Hexkit { / *** 16 진 문자의 출력을 생성하는 데 사용되는 소문자 배열* / private static final char [] digits_lower = { '0', '1', '2', '3', '4', '4', '5', '6', '8', '9', ''b ','c ','d ','e ','e '; / *** 16 진 문자의 출력을 생성하는 데 사용되는 자본 문자 배열*/ private static final char [] digits_upper = { '0', '1', '2', '3', '4', '5', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'e'; //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Convert a byte array to a hexadecimal character array* * @param data byte[] * @return hex char [] */ public static char [] encodeHex (byte [] data) {return encodeHex (data, true); } / *** 바이트 배열을 16 진 문자 배열로 변환* @param str string* @param charset encoding* @return hex char []* / public static char [] encodeHex (String Str, charset charset) {return encodeHex (strkit.getBytes (str, charset), true); }/** * 바이트 배열을 16 진 문자 배열로 변환 * * @param data byte [] * @param tolowercase <code> true </code> 소문자 형식으로 전송, <code> false </code> 대문자 형식으로 전송 */public static char [] */public static char [] byte [] data, bolean tolowase (bolean tolowcase) {return tolowase? DIGITS_LOWER : DIGITS_UPPER); } / ** * 바이트 배열을 16 진수 문자열로 변환 * @param data byte [] * @return hexstring * / public static string encodeHexstr (byte [] data) {return encodeHexstr (data, true); }/** * 바이트 배열 변환 hex 문자열 * * @param data byte [] * @param tolowercase <code> true </code> 소문자 형식으로 전송, <code> false </code> 대문자 형식으로 전송 * @return hexstring */public static string encodeHexStr (byte, byolean tolowercase) {encodehexStr (data, tolithouse)? digits_upper); } //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- DecodeHexstr (hexstr.tochararray (), charset); } / *** 육각 문자 배열을 문자열로 변환* @param hexdata hex char []* @param charset encoding* @return string* / public static string decodeHexstr (char [] hexdata, charset charset) {return strkit.str (decodeHex), charset); } / ** * hex 문자 배열을 바이트 배열로 변환 * @param hexdata hex char [] * @return byte [] * @throws rows runtimeexception 소스 헥스 문자 배열이 이상한 길이 인 경우 런타임 예외는 * / public static byte [] decodehex (char [] hexdata) {int len = hexdata.length입니다. if ((len & 0x01)! = 0) {새 runtimeexception을 던지십시오 ( "홀수의 문자"); } byte [] out = new Byte [len >> 1]; // 두 문자가 16 진수를 형성합니다. for (int i = 0, j = 0; j <len; i ++) {int f = todigit (hexdata [j], j) << 4; J ++; f = f | Todigit (HexData [J], J); J ++; out [i] = (byte) (f & 0xff); } 리턴 아웃; } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Todigits) {return new String (EncodeHex (데이터, 토 디지)); } / ** * 바이트 배열을 16 진 문자 배열로 변환 * @param data byte [] * @param todigits char [] 출력을 제어하기위한 char [] @return hex char [] * / private static char [] encodeHex (byte [] data, char [] todigits) {int l = data.length; char [] out = new char [l << 1]; // 두 문자가 16 진수를 형성합니다. for (int i = 0, j = 0; i <l; i ++) {out [j ++] = 유아 [(0xf0 & data [i]) >>> 4]; out [j ++] = 유아 [0x0f & data [i]]; } 리턴 아웃; } / *** hexadecimal 문자를 정수로 변환* @param ch hexadecimal char* @param index 캐릭터 배열에서 16 진 문자의 위치. @TheRrows runger* @Throws runtimeexception이 합법적 인 16 진 문자를 제외하고, 런타임 예외를 제외하고, 개인 정적 int toDigit (char {int in int) 16); if (digit == -1) {throw new runtimeexception ( "index" + index에서 "불법 16 진 문자" + ch + "); } 리턴 숫자; } //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- bstring.length () % 8! = 0) return null; StringBuffer tmp = new StringBuffer (); int itmp = 0; for (int i = 0; i <bstring.length (); i += 4) {itmp = 0; for (int j = 0; j <4; j ++) {itmp + = integer.parseint (bstring.substring (i + j, i + j + 1)) << (4 -j -1); } tmp.append (integer.tohexstring (itmp)); } return tmp.toString (); } / ** * hexstring * @param hexstring * @return * / public static string hex2binary (String hexstring) {if (hexstring == null || hexstring.length () % 2! = 0) return null; 문자열 bstring = "", tmp; for (int i = 0; i <hexstring.length (); i ++) {tmp = "0000" + integer.tobinarystring (integer.parseint (hexstring.substring (i, i + 1), 16); bstring += tmp.substring (tmp.length () -4); } return bstring; } / ** * 바이너리 변환 바이너리 변환 * @param buf * @return * / public static string binary2Hex (byte buf []) {StringBuffer sb = new StringBuffer (); for (int i = 0; i <buf.length; i ++) {String hex = integer.tohexstring (buf [i] & 0xff); if (hex.length () == 1) {hex = '0' + hex; } sb.append (hex.toupperCase ()); } return sb.toString (); } / ** * hexstr를 바이너리로 변환 * @param hexstr * @return * / public static bd 바이트 [] 결과 = 새로운 바이트 [hexstr.length () / 2]; for (int i = 0; i <hexstr.length () / 2; i ++) {int high = integer.parseint (hexstr.substring (i * 2, i * 2+1), 16); int low = integer.parseint (hexstr.substring (i * 2 + 1, i * 2 + 2), 16); 결과 [i] = (바이트) (높이 * 16 + 낮음); } 반환 결과; }}추신 :이 웹 사이트를위한 몇 가지 온라인 변환 및 계산 도구는 다음과 같습니다. 나는 그것이 당신에게 도움이 될 것이라고 믿습니다.
온라인 임의의 전환 도구 :
http://tools.vevb.com/transcoding/hexconvert
온라인 표준 계산기 :
http://tools.vevb.com/jisuanqi/jsq
온라인 과학 계산기 :
http://tools.vevb.com/jisuanqi/jsqkeexue
Java 알고리즘에 대한 자세한 내용은이 사이트에 관심이있는 독자들이 주제를 볼 수 있습니다. "Java 데이터 구조 및 알고리즘 자습서", "Java Operation Dom Node Tips 요약", "Java 파일 및 디렉토리 작동 팁 요약"및 "Java Cache Operation Tips의 요약"을 볼 수 있습니다.
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.