十六进制 转 字符数组 问题 发钱1~!~ 小弟 从 679 十进制 转成 02A7 十六进制 。。想问的是 怎么 把 02A7 这个数字 转成 字节数组!~!~大虾帮忙!~!~发钱!~! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 大括号理的就是一个byte类型的数组,里面是3个byte类型那个的数 public static String stringToHexString(String strPart) { String hexString = ""; for (int i = 0; i < strPart.length(); i++) { int ch = (int) strPart.charAt(i); String strHex = Integer.toHexString(ch); hexString = hexString + strHex; } return hexString; } 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()); } 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); 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; }/** * Convert byte[] to hex string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。 * @param src byte[] data * @return hex string */ public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * Convert hex string to byte[] * @param hexString the hex string * @return byte[] */ public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } /** * Convert char to byte * @param c char * @return byte */ private byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } 就是最后得到一个 rump[] (byte数组就可以了 ) 你最后想要的字节数组是什么样的?int tmp = 0x02A7;byte[] b = String.valueOf(tmp).getBytes();最后b中的数据为[54, 55, 57] 这么简单, 不知道是不是你要的. Integer ten = 123; String str = Integer.toString(ten, 16); byte[] bytes = str.getBytes(); 不好意思 不是这个 我最后要得到是 2个数字 会不会是ACSII 这样转可以吗 大虾 为什么 我打印的是 这个 [B@1c78e57如果我想打印的是 54,55,57应该怎么做 import java.io.*;public class Binary{ public static void main(String[] args) throws IOException{ int i = 0x02A7; byte[] b = getBytes(i); for(int k=0;k<b.length;k++){ System.out.print(" "+b[k]); } } public static byte[] getBytes(int i) throws IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(i); dos.close(); return baos.toByteArray(); }}楼主看看这个是不是你想要的结果? for循环那个数组for(byte bb : b){ System.out.println(bb); } 我发现join在循环体使用中会出现意不到的结果,不知道是java的bug,还是我用法的问题.求高手指导 java使用jadoc JOptionPane的初级问题 swing中回车处理 关于JDK1.5中线程组suspend()和stop()方法的疑问 为什么 double x = 1/26; x=0????? 有谁做过计算机发传真的程序吗,java编程 关于hashCode()的问题,急! 关于delay()的问题? Java小问题 问个android preference的问题 数组 合并的 问题(大虾 帮助) !~!~
public static String stringToHexString(String strPart) {
String hexString = "";
for (int i = 0; i < strPart.length(); i++) {
int ch = (int) strPart.charAt(i);
String strHex = Integer.toHexString(ch);
hexString = hexString + strHex;
}
return hexString;
}
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());
}
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);
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;
}
/**
* Convert byte[] to hex string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。
* @param src byte[] data
* @return hex string
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
* Convert hex string to byte[]
* @param hexString the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* Convert char to byte
* @param c char
* @return byte
*/
private byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
就是最后得到一个 rump[] (byte数组就可以了 )
byte[] b = String.valueOf(tmp).getBytes();
最后b中的数据为[54, 55, 57]
String str = Integer.toString(ten, 16);
byte[] bytes = str.getBytes();
不好意思 不是这个
我最后要得到是 2个数字 会不会是ACSII 这样转可以吗
大虾 为什么 我打印的是 这个
[B@1c78e57如果我想打印的是 54,55,57应该怎么做
import java.io.*;public class Binary{
public static void main(String[] args) throws IOException{
int i = 0x02A7;
byte[] b = getBytes(i);
for(int k=0;k<b.length;k++){
System.out.print(" "+b[k]);
}
} public static byte[] getBytes(int i) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(i);
dos.close();
return baos.toByteArray();
}
}
楼主看看这个是不是你想要的结果?
System.out.println(bb);
}