java中字节数组byte[]和字符(字符串)之间的转换
转自:http://blog.csdn.net/linlzk/article/details/6566124
Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成byte数组型,java在字符与数组转换也是非常方便的;
1、将字符转换成byte数组
String str = "罗长";
byte[] sb = str.getBytes();
2、将byte数组转换成字符
byte[] b={(byte)0xB8,(byte)0xDF,(byte)0xCB,(byte)0xD9};
String str= new String (b);
3、为了方便字符的加减操作,通常以16进制字符替代普通字符与byte数组进行相互转换
/**
* 16进制的字符串表示转成字节数组
*
* @param hexString
* 16进制格式的字符串
* @return 转换后的字节数组
**/
public static byte[] toByteArray(String hexString) {
if (StringUtils.isEmpty(hexString))
throw new IllegalArgumentException("this hexString must not be empty");
hexString = hexString.toLowerCase();
final byte[] byteArray = new byte[hexString.length() / 2];
int k = 0;
for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
byteArray[i] = (byte) (high << 4 | low);
k += 2;
}
return byteArray;
}
/**
* 字节数组转成16进制表示格式的字符串
*
* @param byteArray
* 需要转换的字节数组
* @return 16进制表示格式的字符串
**/
public static String toHexString(byte[] byteArray) {
if (byteArray == null || byteArray.length < 1)
throw new IllegalArgumentException("this byteArray must not be null or empty");
final StringBuilder hexString = new StringBuilder();
for (int i = 0; i < byteArray.length; i++) {
if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零
hexString.append("0");
hexString.append(Integer.toHexString(0xFF & byteArray[i]));
}
return hexString.toString().toLowerCase();
}
java中字节数组byte[]和字符(字符串)之间的转换的更多相关文章
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- C#中字节数组byte[]和字符串string类型的相互转换
C#中字节数组byte[]和字符串string类型的相互转换: string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBy ...
- java中数组、集合、字符串之间的转换,以及用加强for循环遍历
java中数组.集合.字符串之间的转换,以及用加强for循环遍历: @Test public void testDemo5() { ArrayList<String> list = new ...
- C#中字节数组(byte[])和字符串相互转换
转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...
- Java中的基本数据类型和基本数据类型之间的转换
在Java中有8中基本数据类型,分别为: 整型: byte.short.int.long 浮点型:float.double 布尔型:boolean 字符型:char. byte: 8位, 封装 ...
- js中js数组、对象与json之间的转换
在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键.例如:JSON字符串:var str1 = '{ &quo ...
- java字节数组和16进制之间的转换
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ pac ...
- java中基本数据类型、包装类及字符串之间的相互转换
基本数据类型:不支持面向对象的编程机制(没有属性和方法),即不支持面向对象,之所以提供8中基本数据类型,是为了方便常规数据的处理. 包装类:通过包装类可以将基本数据类型的值包装为引用数据类型的对象,使 ...
- (Java) byte[] 和 base64 字符串之间的转换
import org.apache.commons.codec.binary.Base64; public class UtilHelper { //base64字符串转byte[] public s ...
随机推荐
- HDU 5421 Victor and String
Victor and String Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on HDU. Orig ...
- zoj 2835 Magic Square(set)
Magic Square Time Limit: 2 Seconds Memory Limit: 65536 KB In recreational mathematics, a magic ...
- Problem 2125 简单的等式(FZU),,数学题。。。
Problem 2125 简单的等式 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 现在有一个等式如下:x^2+ ...
- BZOJ2059: [Usaco2010 Nov]Buying Feed 购买饲料
数轴上n<=500个站可以买东西,每个站位置Xi,库存Fi,价格Ci,运东西价格是当前运载重量的平方乘距离,求买K<=10000个东西到达点E的最小代价. f[i,j]--到第i站不买第i ...
- jascript的this
一,this基础 1. 虽然在jascript中一切都是对象,即函数也是一个对象,但在函数中的this并不是指函数本身. 2. 函数中的this指向不是在函数定义时确定的,而是在函数调用时确定的. 3 ...
- 混合APP开发框架资料汇总
Ionic(ionicframework)一款接近原生的Html5移动App开发框架 会html css js就可以开发app,Ionic基于angualrjs框架是一个专注于开发移动wap以及app ...
- loj515 贪心只能过样例(bitset)
题目: https://loj.ac/problem/515 分析: 所有可能和的最大值是1e6 如果dp的话,dp[i][j]表示前i个数能否凑出和为j的数 这样是O(n^5)的 考虑到[j]可以用 ...
- 【APUE】进程间通信之FIFO
FIFO也称为有名管道,它是一种文件类型,是半双工的.FIFO简单理解,就是它能把两个不相关的进程联系起来,FIFO就像一个公共通道,解决了不同进程之间的“代沟”.普通的无名管道只能让相关的进程进行沟 ...
- uva558 Wormholes SPFA 求是否存在负环
J - Wormholes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- POJ 3488 & HDU 1915 Arne Saknussemm(模拟)
题目链接: POJ:http://poj.org/problem? id=3488 HDU:pid=1915">http://acm.hdu.edu.cn/showproblem.ph ...