请问JAVA如何将十六进制转换为字符串,急!谢谢 我也能用Integer.toHexString(ch)将字符串转为十六进制,请问反转的函数是什么?怎么转换,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 toHexStringpublic static String toHexString(int i)以十六进制的无符号整数形式返回一个整数参数的字符串表示形式。 如果参数为负,那么无符号整数值为参数加上 232;否则等于该参数。将该值转换为十六进制(基数 16)的无前导 0 的 ASCII 数字字符串。如果无符号数的大小值为零,则用一个零字符 '0' (’\u0030’) 表示它;否则,无符号数大小的表示形式中的第一个字符将不是零字符。用以下字符作为十六进制数字: 0123456789abcdef 这些字符的范围是从 '\u0030' 到 '\u0039' 和从 '\u0061' 到 '\u0066'。如果希望得到大写字母,可以在结果上调用 String.toUpperCase() 方法: Integer.toHexString(n).toUpperCase() 参数:i - 要转换成字符串的整数。 返回:用十六进制(基数 16)参数表示的无符号整数值的字符串表示形式。 谢谢!// 转化字符串为十六进制编码 public static String toHexString(String s) { String str=""; for (int i=0;i<s.length();i++) { int ch = (int)s.charAt(i); String s4 = Integer.toHexString(ch); str = str + s4; } return str; } 我现在需要的是把十六进制转换为字符串,请帮忙! 已经解决,放上来跟大家共享哈,谢谢大家的支持// 转化十六进制编码为字符串 public static String toStringHex(String s) { byte[] baKeyword = new byte[s.length()/2]; for(int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16)); } catch(Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "utf-8");//UTF-16le:Not } catch (Exception e1) { e1.printStackTrace(); } return s; } int temp = 1234567;String hexString = Integer.toHexString(temp);System.out.println( "hexString = " + hexString );String str = Integer.toString(Integer.parseInt(hexString, 16));System.out.println( "str = " + str ); 如果十六进制的串是中文字串,转换过来的中文字符串是乱码呀,请大虾帮忙看一下吧// 转化十六进制编码为字符串public static String toStringHex(String s){ byte[] baKeyword = new byte[s.length()/2]; for(int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16)); } catch(Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "utf-8");//UTF-16le:Not } catch (Exception e1) { e1.printStackTrace(); } return s;} wddlqd(跑,跑,跑,一直跑到天涯海角) 你好,你给我的是把整形数字转换,我需要的是连中文也能转换,我的那个怎么转换回来的中文就成乱码了,请指教 public static void main(String[] args) { System.out.println(encode("中文")); System.out.println(decode(encode("中文")));}/* * 16进制数字字符集 */private static String hexString="0123456789ABCDEF";/* * 将字符串编码成16进制数字,适用于所有字符(包括中文) */public static String encode(String str){ //根据默认编码获取字节数组 byte[] bytes=str.getBytes(); StringBuilder sb=new StringBuilder(bytes.length*2); //将字节数组中每个字节拆解成2位16进制整数 for(int i=0;i<bytes.length;i++) { sb.append(hexString.charAt((bytes[i]&0xf0)>>4)); sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); } return sb.toString();}/* * 将16进制数字解码成字符串,适用于所有字符(包括中文) */public static String decode(String bytes){ ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2); //将每2位16进制整数组装成一个字节 for(int i=0;i<bytes.length();i+=2) baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1)))); return new String(baos.toByteArray());} public class testcolor { private static byte uniteBytes(byte src0, byte src1) { byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue(); _b0 = (byte) (_b0 << 4); byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue(); byte ret = (byte) (_b0 ^ _b1); // System.out.println(ret<<8); return ret; } /** * @param src * @return */ /*private static byte[] HexString2Bytes(String src) { byte[] ret = new byte[6]; byte[] tmp = src.getBytes(); for (int i = 0; i < 3; i++) { ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); } return ret; } ;*/ public static byte[] HexString2Bytes(String src) { byte[] ret = new byte[6]; byte[] tmp = src.getBytes(); for(int i=0; i<6; ++i ){ ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); } return ret; } /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 String s="ff22ee"; Number ss; byte[] ret = new byte[6]; Integer i=new Integer(ret[0]); ret = HexString2Bytes(s) ; int c=((ret[2]<<8|ret[1]<<8|ret[0])|0xff000000); //Color cc=new Color(c); // System.out.println(cc.getRed()+":::"+cc.getBlue()+":::"+cc.getGreen()); }} 有个工具叫native2ascii.exe百度一下吧 int i = Integer.parseInt( str, 16 ); String str="中";char c=(char)str.charAt(0);System.out.println(c+"转换为16进制为:"+Integer.toString(c,16));System.out.println("16进制转换为字符串为:"+(char)Integer.valueOf(Integer.toString(c, 16),16).intValue()); String num = "FE";int parseInt = Integer.parseInt(num, 16);System.out.println(parseInt); public static String toHexString(int i) System.out.println((char)((int)'中')); fddfdfdfdfdfdffdfdfd up up up up up up up (已经求教很多人,均无答案)何如将<xliff:g>标签写入xml文件中 高手指点 java布局管理器问题 spring hibernate 事务不能回滚 谁会用prefuse?显示分类cluster,及cluster内item的关系 oracle 的时间怎么转为yyyyMMddHHmmss JSP页面刷新闪烁的问题 正则表达式的问题 求大神点拨一个网络通信程序的“客户端定位”的功能实现方案........... JDK1.3 中的java命令不好用? 怎样用Java.net.URL表示一个本地目录下的文件? c中的宏定义在java中怎样实现?
public static String toHexString(int i)以十六进制的无符号整数形式返回一个整数参数的字符串表示形式。
如果参数为负,那么无符号整数值为参数加上 232;否则等于该参数。将该值转换为十六进制(基数 16)的无前导 0 的 ASCII 数字字符串。如果无符号数的大小值为零,则用一个零字符 '0' (’\u0030’) 表示它;否则,无符号数大小的表示形式中的第一个字符将不是零字符。用以下字符作为十六进制数字: 0123456789abcdef
这些字符的范围是从 '\u0030' 到 '\u0039' 和从 '\u0061' 到 '\u0066'。如果希望得到大写字母,可以在结果上调用 String.toUpperCase() 方法:
Integer.toHexString(n).toUpperCase()
参数:
i - 要转换成字符串的整数。
返回:
用十六进制(基数 16)参数表示的无符号整数值的字符串表示形式。
// 转化字符串为十六进制编码
public static String toHexString(String s)
{
String str="";
for (int i=0;i<s.length();i++)
{
int ch = (int)s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
} 我现在需要的是把十六进制转换为字符串,请帮忙!
// 转化十六进制编码为字符串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i < baKeyword.length; i++)
{
try
{
baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
s = new String(baKeyword, "utf-8");//UTF-16le:Not
}
catch (Exception e1)
{
e1.printStackTrace();
}
return s;
}
String hexString = Integer.toHexString(temp);
System.out.println( "hexString = " + hexString );
String str = Integer.toString(Integer.parseInt(hexString, 16));
System.out.println( "str = " + str );
// 转化十六进制编码为字符串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i < baKeyword.length; i++)
{
try
{
baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
s = new String(baKeyword, "utf-8");//UTF-16le:Not
}
catch (Exception e1)
{
e1.printStackTrace();
}
return s;
}
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/*
* 16进制数字字符集
*/
private static String hexString="0123456789ABCDEF";
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(String str)
{
//根据默认编码获取字节数组
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
//将字节数组中每个字节拆解成2位16进制整数
for(int i=0;i<bytes.length;i++)
{
sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
}
return sb.toString();
}
/*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
//将每2位16进制整数组装成一个字节
for(int i=0;i<bytes.length();i+=2)
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
return new String(baos.toByteArray());
}
byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue();
byte ret = (byte) (_b0 ^ _b1);
// System.out.println(ret<<8);
return ret;
}
/**
* @param src
* @return
*/
/*private static byte[] HexString2Bytes(String src) {
byte[] ret = new byte[6];
byte[] tmp = src.getBytes();
for (int i = 0; i < 3; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
;*/ public static byte[] HexString2Bytes(String src)
{ byte[] ret = new byte[6];
byte[] tmp = src.getBytes(); for(int i=0; i<6; ++i ){ ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); } return ret; }
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
String s="ff22ee";
Number ss;
byte[] ret = new byte[6];
Integer i=new Integer(ret[0]);
ret = HexString2Bytes(s) ;
int c=((ret[2]<<8|ret[1]<<8|ret[0])|0xff000000);
//Color cc=new Color(c);
// System.out.println(cc.getRed()+":::"+cc.getBlue()+":::"+cc.getGreen());
}}
百度一下吧
char c=(char)str.charAt(0);
System.out.println(c+"转换为16进制为:"+Integer.toString(c,16));
System.out.println("16进制转换为字符串为:"+(char)Integer.valueOf(Integer.toString(c, 16),16).intValue());
int parseInt = Integer.parseInt(num, 16);
System.out.println(parseInt);