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 ...
随机推荐
- 把以100000+4位随机码的登录账号(比如1000001234),赋予制单页面的权限,怎么写sql啊
insert into sys_user_role (user_id,role_id,office_id) select id,'000101100000000004UP',company_id f ...
- Flask--修改默认的static文件夹的方法
修改的flask默认的static文件夹只需要在创建Flask实例的时候,把static_folder和static_url_path参数设置为空字符串即可. app = Flask(__name__ ...
- ZOJ3860-Find the Spy
Find the Spy Time Limit: 2 Seconds Memory Limit: 65536 KB Whoooa! There is a spy in Marjar Univ ...
- PTA 02-线性结构2 一元多项式的乘法与加法运算 (20分)
原题地址 https://pta.patest.cn/pta/test/15/exam/4/question/710 5-2 一元多项式的乘法与加法运算 (20分) 设计函数分别求两个一元多项式的 ...
- CSU 1307 最短路+二分
题目大意: 帮忙找到一条a到b的最短路,前提是要保证路上经过的站点的最大距离尽可能短 这道题居然要用到二分...完全没去想过,现在想想求最大距离的最小值确实是... 这里不断二分出值代入spfa()或 ...
- Codeforces 892 D.Gluttony
D. Gluttony time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 【多校训练2】HDU 6047 Maximum Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=6047 [题意] 给定两个长度为n的序列a和b,现在要通过一定的规则找到可行的a_n+1.....a_2n,求su ...
- SQL Server 创建唯一约束sql语句
SQL Server 创建唯一约束sql语句 语句示例: 在创建表是时同时创建, 创建id,name,sex三个字段的唯一索引 create table t1( id int primary ...
- 《effective C++》:条款07——为多态基类声明virtual析构函数
在继承中,基类的析构函数需要定义为虚析构函数数否则: (1)当派生类对象经由一个base类指针删除时,而这个base类的析构函数不是虚函数时,其结果是未定义的. (2)这样做会导致derived类部分 ...
- vs2010 静态使用 opencv 2.46 库
下载opencv2.46的库,假设解压到OpenCV246,设置如下: 在工程的c++的include目录下添加:OpenCV246\opencv\build\include 在工程的c++的lib目 ...