導航:首頁 > 編程語言 > javabyte輸出16進制

javabyte輸出16進制

發布時間:2025-09-08 02:47:01

『壹』 java如何發送16進制報文

byte bytes[]=new bytes[256];
//..................賦值

out.write(bytes); //out是輸出流OutputStream,或繼承於的

『貳』 java中如何將byte[]裡面的數據轉換成十六進制

方法如下:

/* *

* Convert byte[] to hex string.這里我們可以將byte轉換成int,然後利用Integer.toHexString(int)

*來轉換成16進制字元串。

* @param src byte[] data

* @return hex string

*/

public static String bytesToHexString(byte[] src){

StringBuilder stringBuilder = new StringBuilder("");

if (src == null || src.length <= 0) {

return null;

}

for (int i = 0; i < src.length; i++) {

int v = src[i] & 0xFF;

String hv = Integer.toHexString(v);

if (hv.length() < 2) {

stringBuilder.append(0);

}

stringBuilder.append(hv);

}

return stringBuilder.toString();

}

此方法能將byte[]轉化成16進制字元串,

『叄』 java怎麼將16進制文件字元串轉成普通字元串

將指定byte數組以進制的形式列印到控制台,代碼如下:
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;
}

}

『肆』 java 16進制byte數組 轉化成UTF-8格式字元串

byte bytes[] = {'1','2','3'};
String str = new String(bytes,"utf-8");

『伍』 java byte 怎麼表示16進制

* Convert byte[] to hex string.這里我們可以將byte轉換成int,然後利用Integer.toHexString(int)來轉換成16進制字元串。
* @param src byte[] data
* @return hex string
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
* Convert hex string to byte[]
* @param hexString the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* Convert char to byte
* @param c char
* @return byte
*/
private byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}

閱讀全文

與javabyte輸出16進制相關的資料

熱點內容
javaocr數字識別 瀏覽:893
類似u盤的文件夾 瀏覽:557
眾合在線app怎麼打不開 瀏覽:80
微信早上起床圖片大全 瀏覽:154
js事件重復綁定 瀏覽:473
上海哪裡學少兒編程課程好 瀏覽:530
外往粗車循環如何編程 瀏覽:828
lol聯網配置文件 瀏覽:603
ipad升級四位密碼 瀏覽:112
word2003天空 瀏覽:883
自學中醫網站有哪些 瀏覽:525
ps文件添加文字填充做舊 瀏覽:934
透明的文件夾圖標 瀏覽:913
vivo手機主題壁紙在哪個文件夾里 瀏覽:907
win10安裝系統的三個文件 瀏覽:365
mastercam多軸編程怎麼設置夾具 瀏覽:904
postgre刪除資料庫 瀏覽:501
laravel多文件上傳 瀏覽:960
vb秒錶代碼製作 瀏覽:413
蘋果4s其他文件 瀏覽:726

友情鏈接