/**
* 将传进来的十六进制表示的字符串转换成byte数组
*
* @param hexString
* @return 二进制表示的byte[]数组
*/
private static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase(Locale.getDefault());
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;
// 两个字符一个对应byte的高四位一个对应第四位
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* 将传进来的字符代表的数字转换成二进制数
*
* @param c
* 要转换的字符
* @return 以byte的数据类型返回字符代表的数字的二进制表示形式
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PostMethod get = new PostMethod(
"http://localhost:8080/servlet/getDoc");
String filePath = "d:/photo1.doc";
NameValuePair[] data = new NameValuePair[2];
NameValuePair nameValuePair1 = new NameValuePair("token", "4565465fgfdg");
NameValuePair nameValuePair2 = new NameValuePair("docId", "1101");
data[0] = nameValuePair1;
data[1] = nameValuePair2;
get.setRequestBody(data);
String result = "";
HttpClient client = new HttpClient();
try {
client.executeMethod(get);
byte[] responseBody = get.getResponseBody();
StringBuffer sb = new StringBuffer();
sb.append(get.getResponseBody());
result = new String(get.getResponseBody(), "utf-8");
String[] tt = result.split("\"");
File file = new File(filePath); // 用File类表示出源文件夹
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fo = new FileOutputStream(file);
// Blob bl = new BlobImpl(hexStringToBytes(tt[7]));
ByteArrayInputStream bi= new ByteArrayInputStream(hexStringToBytes(tt[7]));
InputStream in = bi;
int i;
byte[] buffer = new byte[20480];
while ((i = in.read(buffer)) != -1) {
fo.write(buffer, 0, i);
}
fo.close();
System.out.println("restConn-----------------------------------"+ tt[7]);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
分享到:
相关推荐
NULL 博文链接:https://hw1287789687.iteye.com/blog/1882644
此工具用于将16进制字符串转换成10进制byte数组
android byte字节数组转换十六进制字符串一个demo。里面有几种转换测试代码。我的博客地址:http://blog.csdn.net/qq_16064871。
16进制与字符串、字节数组之间的转换 16进制与字符串、字节数组之间的转换
最近由于工作上的需要,遇到进制转换的问题。涉及到的进制主要是 十进制,十六进制,二进制中间的转换。还包括了一个字节数组 与十六进制字符串的互相转换。估计遇到蓝牙通信的朋友,应该用的上。
/* java二进制,字节数组,字符,十六进制,BCD编码转换2007-06-07 00:17/** *//** * 把16进制字符串转换成字节数组 * @param hex * @return */
byte数组、十进制、十六进制间的两两转换
java工具类 : string转10进制sacii byte数组、 10进制sacii byte数组转16进制字符数组、 16进制字符数组转16进制byte数组、 string与BCD互转等方法,包含多个转换方法, 足够满足日常解包组包需求
字符串之间的转化通用,字节数组转化为十六进制字符串,16进制转化为byte数组,十六进制字符串转字符串,将一条十六进制字符串转换为ASCII
labview 读取的一维数组(整数)转换成16进制的字符串,方便大家进行数据处理。有问题可以留言咨询,互相学习
十六进制字符串转换为byte数组 char转换为byte数组 16进制转化为数字 ytes数组转16进制String byte数组转换为十六进制字符串 int转换为byte数组 byte数组转换为int 保留几位小数 null转String String转Byte String转...
java socket处理硬件传过来的16进制数据的工具类,包含大小端转换、byte[]数组转换为16进制的字符串、byte[]转ByteBuffer、16进制字符串转换为byte数组等
输入string 输出byte 16进制下的转换
二进制字符串转换成十六进制字符串,并返回byte数组 把16进制字符串转换成字节数组 把字节数组转换成16进制字符串 十六进制字符串转二进制字符串 二进制转十进制数 将数字(十进制)串转换为固定位数的二进制字符...
代码如下:/// <summary> /// 字符串转16进制字节数组 /// </summary> /// ”hexString”></param> /// <returns></returns> private static byte[] strToToHexByte(string hexString) { hexString = hexString....
15.将十六进制字符串转换为字节数组 16.将十六进制字符转换成一个整数 17.截取字节数组 18.int转byte数组 19.byte数组转int 20.字节数组逆序 21.异或校验/BCC异或校验 22.将16进制字符串低位字节补零 23.将十进制...
该代码可以实现单精度浮点数和16进制字符之间的转换,包含源代码
可以获取textEdit上的QString类型16进制数转换为ByteArray; 输入数据错误判断及提示; CRC16校验计算公式; 计算ByteArray的crc16结果; 结果转换为QString,并且每两位数据都需要增加空格展示; 输入清除功能; ...