JAVAの16進数と文字列の変換

高洛峰
リリース: 2017-01-24 15:10:57
オリジナル
2129 人が閲覧しました

toHexString
public static String toHexString(int i) は、整数パラメーターの文字列表現を 16 進数の符号なし整数形式で返します。
パラメータが負の場合、符号なし整数値はパラメータに 232 を加えた値になります。それ以外の場合は、パラメータと等しくなります。値を先頭の 0 を除いた 16 進数 (base 16) ASCII 数値文字列に変換します。符号なし数値のサイズ値がゼロの場合は、ゼロ文字 '0' ('u0030') で表されます。それ以外の場合、符号なし数値のサイズ表現の最初の文字はゼロ文字になりません。次の文字を 16 進数として使用します:
0123456789abcdef
これらの文字の範囲は「u0030」から「u0039」および「u0061」から「u0066」です。大文字が必要な場合は、結果に対して String.toUpperCase() メソッドを呼び出すことができます:
Integer.toHexString(n).toUpperCase()
パラメータ:
i - 文字列に変換される整数。
戻り値:
16 進数 (基数 16) 引数の符号なし整数値の文字列表現。
//文字列を 16 進エンコードに変換します
public static String toHexString(String s)
{
String str="";
for (int i=0;i{
int ch = (int)s.charAt(i);
String s4 = Integer.toHexString(ch);
}
return str;
// 16 進数のエンコーディングを String に変換します
public static String toStringHex (文字列 s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i {
try
{
baKeyword[ i] = (バイト)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace(); }
}
try
{
s = new String(baKeyword, "utf-8");//UTF-16le:Not
}
catch (Exception e1)
{
e1.printStackTrace ()
}
return; s;
}
// 16 進エンコーディングを文字列に変換します
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2 ]
for(int i = 0; i < baKeyword.length; i++)
{
を試す
baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i *2+2),16); }
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
s = new String(baKeyword, "utf-8") //UTF-16le:Not
}
catch (例外 e1)
{
e1.printStackTrace();
}
return s;
public static void main(String[] args) {
System.out.println (encode("中文")); .out.println(decode(encode("中文")));
}
/*
* 16 進数値文字セット
*/
private static String hexString=" 0123456789ABCDEF";
/*
* 文字列を 16 進数にエンコードしますすべての文字 (中国語を含む) に適した数値
*/
public static String encode(String str)
{
//デフォルトのエンコーディングに従ってバイト配列を取得します
byte[] bytes=str.getBytes(); sb=new StringBuilder(bytes.length*2);
//バイト配列の各バイトを 2 桁の 16 進整数に分解します
for( int i=0;i{
sb. append(hexString.charAt((bytes[i]&0xf0)>>4));
sb.append(hexString.charAt((bytes [i]&0x0f)>>0)); .toString();
}
/*
* すべての文字 (中国語を含む) に適用可能な 16 進数を文字列にデコードします
* /
public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length) ()/2);
//すべての 2 桁の 16 進整数を 1 バイトに組み立てます
for(int i= 0;ibaos.write((hexString.indexOf(bytes) .charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1)) ));
return new String(baos.toByteArray());
2 番目のメソッド:
指定された文字列を出力します。バイト配列を 16 進形式でコンソールに送信します

package com.nantian.iclient.atm.sdb; 

public class Util { 
public Util() { 
} 

/** 
* 将指定byte数组以16进制的形式打印到控制台 
* @param hint String 
* @param b byte[] 
* @return void 
*/ 
public static void printHexString(String hint, byte[] b) { 
System.out.print(hint); 
for (int i = 0; i < b.length; i++) { 
String hex = Integer.toHexString(b[i] & 0xFF); 
if (hex.length() == 1) { 
hex = &#39;0&#39; + hex; 
} 
System.out.print(hex.toUpperCase() + " "); 
} 
System.out.println(""); 
} 

/** 
* 
* @param b byte[] 
* @return String 
*/ 
public static String Bytes2HexString(byte[] b) { 
String ret = ""; 
for (int i = 0; i < b.length; i++) { 
String hex = Integer.toHexString(b[i] & 0xFF); 
if (hex.length() == 1) { 
hex = &#39;0&#39; + hex; 
} 
ret += hex.toUpperCase(); 
} 
return ret; 
} 

/** 
* 将两个ASCII字符合成一个字节; 
* 如:"EF"--> 0xEF 
* @param src0 byte 
* @param src1 byte 
* @return byte 
*/ 
public static byte uniteBytes(byte src0, byte src1) { 
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue(); 
_b0 = (byte)(_b0 << 4); 
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue(); 
byte ret = (byte)(_b0 ^ _b1); 
return ret; 
} 

/** 
* 将指定字符串src,以每两个字符分割转换为16进制形式 
* 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9} 
* @param src String 
* @return byte[] 
*/ 
public static byte[] HexString2Bytes(String src){ 
byte[] ret = new byte[8]; 
byte[] tmp = src.getBytes(); 
for(int i=0; i<8; i++){ 
ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); 
} 
return ret; 
} 

}
ログイン後にコピー

その他の JAVA 16 進数と文字列変換に関する記事については、PHP 中国語 Web サイトに注目してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!