この記事では、バイナリ変換ツールクラスのJava実装について説明します。次のように、参照のために共有してください。
Import Java.nio.Charset.Charset;/*** Hexadecimal(Hexまたはsubscript 16として略された)は、16から1までの数学のキャリーシステムです。これは一般に、数値0〜9とFからF(ここで:A〜Fは10〜15)で表されます。 <br> *たとえば、10進数57はバイナリ111001で記述されており、16進数39は16進数で記述されています。 <br> * In order to distinguish between hexadecimal and decimal values, languages like java and c will prefix 0x in front of the hexadecimal number, for example, 0x20 is 32 in decimal, instead of 20 in decimal<br> * * Reference: https://my.oschina.net/xinxingegeya/blog/287476 * * @author Looly * */public class hexkit {/** *ヘキサデシマル文字の出力を作成するために使用される低ケース文字配列 */private static final char [] digits_lower = {'0' 1 '、' 2 '、' 3 '、' 4 '、' 5 '、' 6 '、' 7 '、' '' '' '' '' '' f '' 'f / ***ヘキサデシマル文字の出力を作成するために使用されるキャピタル文字配列//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 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; }/** *バイトアレイを160文字の配列に変換 * * @paramデータバイト[] * @param tolowercase <code> true </code> lowercase形式に転送、<code> false </code> false </code> false </code> false </return hex char [] */public char [] encodehex(buet encodehex(data、tolowercase?digits_lower:digits_upper); } / ** *バイト配列をHexadecimal stringに変換 * * @param data byte [] * @return hexstring * / public static string encodehexstr(byte [] data){return encodehexstr(data、true); }/** *バイトアレイを16進ストリングに変換 * * @paramデータバイト[] * @param tolowercase <code> true </code> lowercase形式に転送、<code> false </code> false </code> @return hexstring */public static string encodehexstr(buolean tolowercase escadecase escadehexstr(] ?digits_lower:digits_upper); } //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- decodehexstr(hexstr.tochararray()、charset); } / *** hex文字配列を文字列に変換** @param hexdata hex char []* @param charset encoding* @return string* / public static string decodehexstr(char [] hexdata、charset charset){return strkit.str(decodehex(hexdata)、charset); } / ** * hex文字配列をバイト配列に変換 * * @param hexdata hex char [] * @return byte [] * @throws runtimexception runtimexception source hex文字アレイが奇妙な長さである場合、ランタイム例外がスローされます。 if((len&0x01)!= 0){新しいruntimeexception( "奇数文字数。"); } byte [] out = new byte [len >> 1]; // 2つの文字が六角値を形成します。 for(int i = 0、j = 0; j <len; i ++){int f = dodigit(hexdata [j]、j)<< 4; J ++; f = f | dodigit(hexdata [j]、j); J ++; out [i] =(byte)(f&0xff); }戻る; } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- dodigits){new string(encodehex(data、dygits))を返す); } / ** *バイトアレイを160文字に変換します * * @param data byte [] * @param todigits char []出力を制御 * @return hex char [] * / private static char [] encodehex(byte [] data、char [] todigits){int l = data.length; char [] out = new char [l << 1]; // 2つの文字が六角値を形成します。 for(int i = 0、j = 0; i <l; i ++){out [j ++] = dodigits [(0xf0&data [i])>>> 4]; out [j ++] = dodigits [0x0f&data [i]]; }戻る; } / *** 16進数文字を整数に変換します** @param ch hexadecimal char* @param index文字配列における16進数文字の位置* @return integer* @throws runtimeexception chが合法的な六分子文字ではない場合、ランタイム例外を投げる* character.digit(ch、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)); } tmp.toString()を返します。 } / ** * hexstring * @param hexstring * @return * / public static string hex2binary(string hexstring){if(hexstring == null || hexstring.length()%2!= 0)return null; string 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); } bstringを返します。 } / ** *バイナリをhexに変換 * @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 byte [] hex2byte(string hexstr){if(hexstr.length()<1)return null; byte [] result = new byte [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); result [i] =(byte)(high * 16 + low); } return result; }}PS:このウェブサイトのオンライン変換および計算ツールをいくつか紹介します。私はそれがあなたに役立つと信じています:
オンライン任意の変換ツール:
http://tools.vevb.com/transcoding/hexconvert
オンライン標準電卓:
http://tools.vevb.com/jisuanqi/jsq
オンライン科学計算機:
http://tools.vevb.com/jisuanqi/jsqkeexue
Javaアルゴリズムの詳細については、このサイトに興味のある読者は、「Javaデータ構造とアルゴリズムのチュートリアル」、「Java操作DOMノードのヒントの要約」、「Javaファイルの要約およびディレクトリ操作のヒント」、「Java Cache操作のヒントの要約」というトピックを見ることができます。
この記事がみんなのJavaプログラミングに役立つことを願っています。