我想把一个字符用十六进制来表示,比如字符串"ip",转换后我想要的结果是"0x690x70",现在我能把这个字符串转成如"6970"这样的格式,而前面的"0x"没有啊!小弟对数据结构不了解,还望各位高手不吝赐教教。
解决方案 »
- IE浏览器未处理tomcat响应的数据!
- 求助:关于在jdk源码中加入断点报错
- 求助:jre不是内部或外部命令,也不是可运行的程序或批处理文件!!!
- 工作中hibernate用表反射生成映射文件那注解怎么办????
- tomcat发布后html的object标签问题
- Spring 的 Controller接口在那个架包里?
- hibernate 连续两次 left join怎么写?
- 有必要报培训班吗
- 怎么pstmt.setInt(0,employeeID)老是编译不了
- 在win2003下的SQL Server for JDBC的不运行情况,急,在线等
- JAVA掉用存储过程得不到返回参数!(大侠请指教)
- 请假一个hibernate多对一的问题
//第一种方法
public static String stringToHex(String base)
{
StringBuffer buffer = new StringBuffer();
int intValue;
for (int x = 0; x < base.length(); x++)
{
int cursor = 0;
intValue = base.charAt(x);
String binaryChar = new String(Integer.toBinaryString(base.charAt(x)));
for (int i = 0; i < binaryChar.length(); i++)
{
if (binaryChar.charAt(i) == '1')
{
cursor += 1;
}
}
if ((cursor % 2) > 0)
{
intValue += 128;
}
buffer.append(Integer.toHexString(intValue));
}
return buffer.toString(); }
public static String stringToHex2(String intString,String encoding) throws UnsupportedEncodingException
{
byte[] bytes = intString.getBytes(encoding);
return bytesToHex(bytes);
}//第二种方法
public static String stringToHex2(String intString)
{
byte[] bytes = intString.getBytes();
return bytesToHex(bytes);
} public static String bytesToHex(byte[] bytes)
{
StringBuilder hexString = new StringBuilder(2 * bytes.length); for (int i = 0; i < bytes.length; i++)
{
String hexStr = String.format("%2X", bytes[i]);
hexString.append(hexStr);
}
return hexString.toString();
}
char c[] = new char[s.length()];
String hexString = "";
for(int i= 0;i<s.length();i++){
c[i] = (char)s.charAt(i);
hexString += "0X"+Integer.toHexString(c[i]+0);
}
System.out.println(hexString);
与 int a = Integer.parseInt("2f", 16);是一致的。而当你system.out.println(a)时,默认情况下 按照十进制来打印出一个字符串。打印16进制时Integer.toHexString(a);打印出来的也不会带“Ox”不知道你明白我的意思没有,一点个人的理解,LZ加油研究。