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