⑴ java中 怎麼把 ascii碼轉換為 十六進制
我剛好寫了
publicstaticStringASCII2HEX(StringASCII2HEX_STRING){
switch(ASCII2HEX_STRING){
case"NUL":
return"00";
case"SOH":
return"01";
case"STX":
return"02";
case"ETX":
return"03";
case"EOT":
return"04";
case"ENQ":
return"05";
case"ACK":
return"06";
case"BEL":
return"07";
case"BS":
return"08";
case"HT":
return"09";
case"LF":
return"0A";
case"VT":
return"0B";
case"FF":
return"0C";
case"CR":
return"0D";
case"SO":
return"0E";
case"SI":
return"0F";
case"DLE":
return"10";
case"DC1":
return"11";
case"DC2":
return"12";
case"DC3":
return"13";
case"DC4":
return"14";
case"NAK":
return"15";
case"SYN":
return"16";
case"ETB":
return"17";
case"CAN":
return"18";
case"EM":
return"19";
case"SUB":
return"1A";
case"ESC":
return"1B";
case"FS":
return"1C";
case"GS":
return"1D";
case"RS":
return"1E";
case"US":
return"1F";
case"SP":
return"20";
case"!":
return"21";
case"\":
return"22";
case"#":
return"23";
case"$":
return"24";
case"%":
return"25";
case"&":
return"26";
case"'":
return"27";
case"(":
return"28";
case")":
return"29";
case"*":
return"2A";
case"+":
return"2B";
case",":
return"2C";
case"_":
return"2D";
case".":
return"2E";
case"/":
return"2F";
case"0":
return"30";
case"1":
return"31";
case"2":
return"32";
case"3":
return"33";
case"4":
return"34";
case"5":
return"35";
case"6":
return"36";
case"7":
return"37";
case"8":
return"38";
case"9":
return"39";
case":":
return"3A";
case";":
return"3B";
case"<":
return"3C";
case"=":
return"3D";
case">":
return"3E";
case"?":
return"3F";
case"@":
return"40";
case"A":
return"41";
case"B":
return"42";
case"C":
return"43";
case"D":
return"44";
case"E":
return"45";
case"F":
return"46";
case"G":
return"47";
case"H":
return"48";
case"I":
return"49";
case"J":
return"4A";
case"K":
return"4B";
case"L":
return"4C";
case"M":
return"4D";
case"N":
return"4E";
case"O":
return"4F";
case"P":
return"50";
case"Q":
return"51";
case"R":
return"52";
case"S":
return"53";
case"T":
return"54";
case"U":
return"55";
case"V":
return"56";
case"W":
return"57";
case"X":
return"58";
case"Y":
return"59";
case"Z":
return"5A";
case"[":
return"5B";
case""":
return"5C";
case"]":
return"5D";
case"↑":
return"5E";
case"←":
return"5F";
case"`":
return"60";
case"a":
return"61";
case"b":
return"62";
case"c":
return"63";
case"d":
return"64";
case"e":
return"65";
case"f":
return"66";
case"g":
return"67";
case"h":
return"68";
case"i":
return"69";
case"j":
return"6A";
case"k":
return"6B";
case"l":
return"6C";
case"m":
return"6D";
case"n":
return"6E";
case"o":
return"6F";
case"p":
return"70";
case"q":
return"71";
case"r":
return"72";
case"s":
return"73";
case"t":
return"74";
case"u":
return"75";
case"v":
return"76";
case"w":
return"77";
case"x":
return"78";
case"y":
return"79";
case"z":
return"7A";
case"{":
return"7B";
case"|":
return"7C";
case"}":
return"7D";
case"~":
return"7E";
case"DEL":
return"7F";
default:
return"";
}
}
⑵ 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位字元串
你可以使用以下步驟將字元串 msg 轉換為16進制字元串:
將字元串 msg 轉換為位元組數組,可以使用 Arrays.toString(msg.getBytes())
將位元組數組中每個位元組轉換為 16 進制字元串,可以使用 Integer.toHexString(byteValue)
將轉換後的字元串拼接起來得到最正磨終的纖謹16進制字元串
例如:
byte[] bytes = msg.getBytes();
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
hexString.append(Integer.toHexString(b & 0xff));
}
String result = hexString.toString();
注意:轉換後的字元串可能會有一些前導0,如果需要去掉可以使用 string.replaceFirst("毀清基^0+(?!$)", "")
⑷ java byte 怎麼表示16進制
Java中,我們可以將byte數組轉換為16進制字元串。這里的方法是將byte轉換成int,然後使用Integer.toHexString(int)將其轉換成16進制字元串。
以下是一個示例方法: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數組作為輸入參數,如果輸入的數組為空或長度小於等於0,那麼返回null。否則,它會遍歷數組中的每個元素,將其轉換為16進制字元串,並將其追加到StringBuilder中。
另外,我們還需要一個方法來將16進制字元串轉換回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; }
這個方法接收一個16進制字元串作為輸入參數,然後將其轉換回byte數組。首先,它將字元串轉換為大寫形式,計算長度並轉換為字元數組。接下來,它遍歷字元數組,將字元轉換為對應的byte值,並將其追加到byte數組中。
最後,我們需要一個輔助方法來將字元轉換為對應的byte值:private byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); }
這個方法接收一個字元作為輸入參數,然後返回該字元在"0123456789ABCDEF"字元串中的索引值。這個索引值將被轉換為byte值,以便將其添加到byte數組中。
⑸ java字元串轉16進制
1、寫轉換函數是必須的,目前Jdk中無現有類。
2、在不同的字元編碼當中,漢字的對應的數值或者說編碼值不一樣,像GBK、Unicode肯定是不一樣的。以Java默認的Unicode為例說明問題。
3、 一,得到字元串,
二,遍歷每個字元,用char來接收,實為int值,或直接用int也可以。
三,Integer.toHexString(每個字元對應的int值);
四,前綴加個「0x」就可以了。
4、如果是其它類型的話,找到對應的字元和數值對應表就可以了。
不知可否解決問題了。
⑹ JAVA字元串轉16進制ascii碼
String s = "abcd";
byte[] b = s.getBytes();
int[] in = new int[b.length];
for (int i = 0; i < in.length; i++) {
in[i] = b[i]&0xff;
}
for (int j = 0; j < in.length; j++) {
System.out.println(Integer.toString(in[j], 0x10));
}
⑺ java中如何將字元串轉16位輸出、、。例如「aa」,"0000 0000 0000 0000"按這樣的方式輸出
先要以正確的編碼把字元串轉為位元組串,在把位元組串轉為16進制編碼
public class Test {
public static void main(String[] args) {
try{
System.out.println(toHex("hello world","GBK"));
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
static public String toHex(String text,String enc) throws UnsupportedEncodingException{
byte B[]=text.getBytes(enc);
StringBuilder buf=new StringBuilder();
for(byte b:B){
buf.append(Integer.toHexString(b&0xff));
}
return buf.toString();
}
}
==========
68656c6c6f20776f726c64