A. java如何把2進制字元串怎麼轉換成16進制字元串
package image;
public class Bean
{
public static void main ( String[] args )
{
String str = "10101011 10000100 01011011 01111100 00011100 01100010 00010111 10010010";
String[] strs = str.split ("\\s+");
String result = "";
for ( String string : strs )
{
String hex = Integer.toString (Integer.parseInt (string, 2), 16);
result += hex;
}
System.out.println (result);
}
}
B. java字元串轉換為十六進制數組
字元串作為函數change的參數inputStr
byte[] change(String inputStr) {
byte[] result = new byte[inputStr.length() / 2];
for (int i = 0; i < inputStr.length() / 2; ++i)
result[i] = (byte)(Integer.parseInt(inputStr.substring(i * 2, i * 2 +2), 16) & 0xff);
return result;
}
C. 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));
}
D. 怎麼把字元串轉化為十六進制字元串 java
思路:用一個初始化為0~9~a~f的字元串數組,也就是一個十六進制對應表,用這個對應表即可算出一個十六進制字元串的數值。
方法如下:
public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f; //位於運算
sb.append(chars[bit]); //進行字元串的拼接
}
return sb.toString();
}
調用方法如下:
String str = str2HexStr("asbd");
E. JAVA:string類型轉換int(16進制)
使用旦枯AT%IPSEND="XXXX"指令,但只模巧洞能作為字元發送,直接發送十寬巧六進制。
F. 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