java中如何将一个字符串以16进制方式显示出来
ABCDEFGHIJKLMN-->-->0X410X420X430X440X450X460X470X480X490X4A0X4B0X4C0X4D0X4E”...
ABCDEFGHIJKLMN--> --> 0X41 0X42 0X43 0X44 0X45 0X46 0X47
0X48 0X49 0X4A 0X4B 0X4C 0X4D 0X4E” 展开
0X48 0X49 0X4A 0X4B 0X4C 0X4D 0X4E” 展开
展开全部
package lzh.Util;
/**
*
* @author http://hi.baidu.com/gladfeel/blog/item/7d45b01e7be529164034176b.html*/
public class String2Hex
{
public static String toHexString1(byte[] b)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i)
{
buffer.append(toHexString1(b[i]));
}
return buffer.toString();
}
public static String toHexString1(byte b)
{
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1)
{
return "0" + s;
} else
{
return s;
}
}
public static String toHexString2(byte[] b)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i)
{
buffer.append(toHexString2(b[i]));
}
return buffer.toString();
}
public static String toHexString2(byte b)
{
char[] buffer = new char[2];
buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16);
buffer[1] = Character.forDigit(b & 0x0F, 16);
return new String(buffer);
}
public static void main(String[] args)
{
byte[] bs = "Hello,World".getBytes();
System.out.println(toHexString1(bs).toUpperCase());
System.out.println(toHexString2(bs).toUpperCase());
}
}
/**
*
* @author http://hi.baidu.com/gladfeel/blog/item/7d45b01e7be529164034176b.html*/
public class String2Hex
{
public static String toHexString1(byte[] b)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i)
{
buffer.append(toHexString1(b[i]));
}
return buffer.toString();
}
public static String toHexString1(byte b)
{
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1)
{
return "0" + s;
} else
{
return s;
}
}
public static String toHexString2(byte[] b)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i)
{
buffer.append(toHexString2(b[i]));
}
return buffer.toString();
}
public static String toHexString2(byte b)
{
char[] buffer = new char[2];
buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16);
buffer[1] = Character.forDigit(b & 0x0F, 16);
return new String(buffer);
}
public static void main(String[] args)
{
byte[] bs = "Hello,World".getBytes();
System.out.println(toHexString1(bs).toUpperCase());
System.out.println(toHexString2(bs).toUpperCase());
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把楼上朋友的改了点,嘿嘿。。。
public class String2Hex
{
/*
java中如何将一个字符串以16进制方式显示出来
悬赏分:10 - 离问题结束还有 14 天 23 小时
ABCDEFGHIJKLMN--> --> 0X41 0X42 0X43 0X44 0X45 0X46 0X47
0X48 0X49 0X4A 0X4B 0X4C 0X4D 0X4E”
*/
public static String toHexString1(byte[] b)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i)
{
buffer.append(toHexString1(b[i]));
}
return buffer.toString();
}
public static String toHexString1(byte b)
{
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1)
{
return "0" + s;
} else
{
return s;
}
}
public static String toHexString2(byte[] b)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i)
{
buffer.append("0x");
buffer.append(toHexString2(b[i]));
buffer.append(" ");
}
return buffer.toString();
}
public static String toHexString2(byte b)
{
char[] buffer = new char[2];
buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16);
buffer[1] = Character.forDigit(b & 0x0F, 16);
return new String(buffer);
}
public static void main(String[] args)
{
// byte[] bs = "Hello,World".getBytes();
byte[] bs = "ABCDEFGHIJKLMN".getBytes();
//System.out.println(toHexString1(bs).toUpperCase());
System.out.println(toHexString2(bs).toUpperCase());
}
}
public class String2Hex
{
/*
java中如何将一个字符串以16进制方式显示出来
悬赏分:10 - 离问题结束还有 14 天 23 小时
ABCDEFGHIJKLMN--> --> 0X41 0X42 0X43 0X44 0X45 0X46 0X47
0X48 0X49 0X4A 0X4B 0X4C 0X4D 0X4E”
*/
public static String toHexString1(byte[] b)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i)
{
buffer.append(toHexString1(b[i]));
}
return buffer.toString();
}
public static String toHexString1(byte b)
{
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1)
{
return "0" + s;
} else
{
return s;
}
}
public static String toHexString2(byte[] b)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i)
{
buffer.append("0x");
buffer.append(toHexString2(b[i]));
buffer.append(" ");
}
return buffer.toString();
}
public static String toHexString2(byte b)
{
char[] buffer = new char[2];
buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16);
buffer[1] = Character.forDigit(b & 0x0F, 16);
return new String(buffer);
}
public static void main(String[] args)
{
// byte[] bs = "Hello,World".getBytes();
byte[] bs = "ABCDEFGHIJKLMN".getBytes();
//System.out.println(toHexString1(bs).toUpperCase());
System.out.println(toHexString2(bs).toUpperCase());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询