當前位置:首頁 » 編程語言 » 16進制的字元串java

16進制的字元串java

發布時間: 2025-07-16 07:39:54

1. java 怎麼解析\xE4\xB8\xAD 這類十六進制字元串

toHexString
public static String toHexString(int i)以十六進制的無符號整數形式返回一個整數參數的字元串表示形式。
如果參數為負,那麼無符號整數值為參數加上 232;否則等於該參數。將該值轉換為十六進制(基數 16)的無前導 0 的 ASCII 數字字元串。如果無符號數的大小值為零,則用一個零字元 '0' ('\u0030') 表示它;否則,無符號數大小的表示形式中的第一個字元將不是零字元。用以下字元作為十六進制數字:
0123456789abcdef
這些字元的范圍是從 '\u0030' 到 '\u0039' 和從 '\u0061' 到 '\u0066'。如果希望得到大寫字母,可以在結果上調用 String.toUpperCase() 方法:
Integer.toHexString(n).toUpperCase()
參數:
i - 要轉換成字元串的整數。
返回:
用十六進制(基數 16)參數表示的無符號整數值的字元串表示形式。
// 轉化字元串為十六進制編碼
public static String toHexString(String s)
{
String str="";
for (int i=0;i<s.length();i++)
{
int ch = (int)s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
// 轉化十六進制編碼為字元串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i < baKeyword.length; i++)
{
try
{
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 (Exception e1)
{
e1.printStackTrace();
}
return s;
}
// 轉化十六進制編碼為字元串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i < baKeyword.length; i++)
{
try
{
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 (Exception e1)
{
e1.printStackTrace();
}
return s;
}
public static void main(String[] args) {
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/*
* 16進制數字字元集
*/
private static String hexString="0123456789ABCDEF";
/*
* 將字元串編碼成16進制數字,適用於所有字元(包括中文)
*/
public static String encode(String str)
{
//根據默認編碼獲取位元組數組
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
//將位元組數組中每個位元組拆解成2位16進制整數
for(int i=0;i<bytes.length;i++)
{
sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
}
return sb.toString();
}
/*
* 將16進制數字解碼成字元串,適用於所有字元(包括中文)
*/
public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
//將每2位16進制整數組裝成一個位元組
for(int i=0;i<bytes.length();i+=2)
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
return new String(baos.toByteArray());
}
第二種方法:
將指定byte數組以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 = '0' + 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 = '0' + 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;
}

}

2. java 以字元串獲取的數組,怎麼轉成16位字元串

你可以使用以下步驟將字元串 msg 轉換為16進制字元串:

  1. 將字元串 msg 轉換為位元組數組,可以使用 Arrays.toString(msg.getBytes())

  2. 將位元組數組中每個位元組轉換為 16 進制字元串,可以使用 Integer.toHexString(byteValue)

  3. 將轉換後的字元串拼接起來得到最終的16進制字元串

    例如:

    byte[] bytes = msg.getBytes();

    StringBuilder hexString = new StringBuilder();

    for (byte b : bytes) {

    hexString.append(Integer.toHexString(b & 0xff));

    }

    String result = hexString.toString();

  4. 注意:轉換後的字元串可能會有一些前導0,如果需要去掉可以使用 string.replaceFirst("^0+(?!$)", "")

3. java中如何把十六進制字元串轉成四位十六機制

在Java中,將byte[]數組轉換為16進制字元串,或者將16進制字元串轉換為byte[]數組,是常見的操作。我們首先了解一下基本原理。

我們知道,一個byte在Java中用二進製表示佔用8位。而16進制的每個字元需要4位二進制位來表示,因為2^4=16。

因此,可以將每個byte轉換為兩個相應的16進制字元。具體來說,就是將byte的高4位和低4位分別轉換為相應的16進制字元H和L,然後將這兩個字元組合起來,得到byte轉換為16進制字元串的結果。這表明,用十六進製表示一個byte只需要2位。

反之,如果要將兩個16進制字元轉換回一個byte,同樣遵循上述原則。具體操作就是根據16進制字元對應的二進制位,進行組合,再轉換回byte。

為了更直觀地理解這一過程,我們可以編寫一個簡單的Java方法來實現這種轉換。

下面是一個例子,展示如何將byte數組轉換為16進制字元串:

java

public static String byteToHex(byte[] bytes) {

StringBuilder hexString = new StringBuilder();

for (byte b : bytes) {

String hex = Integer.toHexString(0xFF & b);

if (hex.length() == 1) {

hexString.append('0');

}

hexString.append(hex);

}

return hexString.toString();

}

這個方法接受一個byte數組作為參數,然後逐個處理每個byte,將其轉換為兩位的16進制字元串。如果有不足兩位的情況,則在前面補0。

同樣,我們也可以編寫一個方法,將16進制字元串轉換回byte數組:

java

public static byte[] hexToByte(String hex) {

int len = hex.length();

byte[] bytes = new byte[len / 2];

for (int i = 0; i < len; i += 2) {

bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)

+ Character.digit(hex.charAt(i + 1), 16));

}

return bytes;

}

這個方法接收一個16進制字元串作為參數,然後每兩個字元組成一個byte,最終返回byte數組。

通過上述方法,我們可以方便地在byte數組和16進制字元串之間進行轉換。

熱點內容
戰隊宣傳片拍攝腳本 發布:2025-07-16 18:42:23 瀏覽:462
疫情源碼 發布:2025-07-16 18:34:53 瀏覽:794
安卓開發平台怎麼樣 發布:2025-07-16 18:30:35 瀏覽:345
電話加密碼 發布:2025-07-16 18:29:12 瀏覽:66
河馬雲腳本 發布:2025-07-16 18:29:03 瀏覽:148
格物致知編程 發布:2025-07-16 18:07:54 瀏覽:950
戴爾伺服器系統設置如何設置 發布:2025-07-16 18:02:09 瀏覽:961
為什麼換安卓這么難 發布:2025-07-16 17:14:44 瀏覽:423
轉動密碼鎖怎麼開 發布:2025-07-16 17:14:37 瀏覽:613
伺服器和網關ip 發布:2025-07-16 17:09:35 瀏覽:932