java基本数据类型转换成byte[]数组
import java.io.UnsupportedEncodingException;
public class ConToByte {
/**
* double转换byte
* @param arr byte[]
* @param param double double类型的参数
* @param index int
*/
public static void putDouble(byte[] arr, double param, int index) {
int len = (index - 1) + 8;
int arrLen = arr.length;
boolean b = isOutOfArrLength(arrLen, len); //判断当前数组长度是否大于转换的数组长度
Long l = Double.doubleToLongBits(param);
if (b) {
for (int i = 7; i >=0; i--) {
arr[index + i] = l.byteValue();
l = l >> 8;
}
} else {
// 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素
l = l>>(8*index);
for(int j= arrLen-index-1;j>=0;j--){
arr[index+j] = l.byteValue();
l = l>>8;
}
}
}
/**
* float转换byte
*
* @param arr byte[]
* @param param float float类型的参数
* @param index int
*/
public static void putFloat(byte[] arr, float param, int index) {
int len = (index - 1) + 4;
int arrLen = arr.length;
boolean b = isOutOfArrLength(arrLen, len); //判断当前数组长度是否大于转换的数组长度
int l = Float.floatToIntBits(param);
if (b) {
for (int i = 3; i >=0; i--) {
arr[index + i] = new Integer(l).byteValue();
l = l >> 8;
}
} else {
// 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素
l = l>>(8*index);
for(int j=arrLen-index-1;j>=0;j--){
arr[index+j] = new Integer(l).byteValue();
l =l>>8;
}
}
}
/**
* 字符到字节转换
*
* @param arr byte[]
* @param ch char char类型的参数
* @param index int
* @return
*/
public static void putChar(byte[] arr, char ch, int index) {
int len = (index - 1) + 4;
boolean b = isOutOfArrLength(arr.length, len); //判断当前数组长度是否大于转换的数组长度
if (b) {
int temp = (int) ch;
for (int i = 1; i >=0; i--) {
arr[index + i] = new Integer(temp & 0xff).byteValue();
temp = temp >> 8;
}
}
}
/**
* 转换long型为byte数组
*
* @param arr byte[]
* @param param long
* @param index int
*/
public static void putLong(byte[] arr, long param, int index) {
int len = (index - 1) + 8;
int arrLen = arr.length;
boolean b = isOutOfArrLength(arrLen, len); //判断当前数组长度是否大于转换的数组长度
if (b) {
arr[index + 0] = (byte) ((param >> 56) & 0xff);
arr[index + 1] = (byte) ((param >> 48) & 0xff);
arr[index + 2] = (byte) ((param >> 40) & 0xff);
arr[index + 3] = (byte) ((param >> 32) & 0xff);
arr[index + 4] = (byte) ((param >> 24) & 0xff);
arr[index + 5] = (byte) ((param >> 16) & 0xff);
arr[index + 6] = (byte) ((param >> 8) & 0xff);
arr[index + 7] = (byte) (param & 0xff);
} else {
// 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素
param = param >> (8*index);
for(int i=arrLen-index-1;i>=0;i--){
arr[index+i] = (byte) (param & 0xff);
param = param >> 8;
}
}
}
/**
* int类型转换成byte数组
*
* @param arr byte[]
* @param param int int类型的参数
* @param index int
*/
public static void putInt(byte[] arr, int param, int index) {
int len = (index - 1) + 4;
boolean b = isOutOfArrLength(arr.length, len); //判断当前数组长度是否大于转换的数组长度
if (b) {
arr[index + 0] = (byte) ((param >> 24) & 0xff);
arr[index + 1] = (byte) ((param >> 16) & 0xff);
arr[index + 2] = (byte) ((param >> 8) & 0xff);
arr[index + 3] = (byte) (param & 0xff);
}
}
/**
* short类型转换成byte数组
*
* @param arr byte[]
* @param param short
* @param index int
*/
public static void putShort(byte[] arr, short param, int index) {
int len = (index - 1) + 2;
boolean b = isOutOfArrLength(arr.length, len); //判断当前数组长度是否大于转换的数组长度
if (b) {
arr[index + 0] = (byte) ((param >> 8) & 0xff);
arr[index + 1] = (byte) (param & 0xff);
}
}
/**
* 字符串转换成byte数组
*
* @param arr byte[]
* @param str String
* @param index int
* @throws java.io.UnsupportedEncodingException
*/
public static void putString(byte[] arr, String str, int index) {
try {
byte[] bb = str.getBytes("GBK");
int len = index + bb.length;
boolean b = isOutOfArrLength(arr.length, len);
if (b) {
for (int i = 0; i < bb.length; i++) {
arr[index + i] = bb[i];
}
} else {
// 如果当前数组长度小于转换的数组长度,就根据index截取转换的数组元素
for(int j=0;j<arr.length-index;j++){
arr[index+j] = bb[j];
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 判断数组下标是否越界
*
* @param arrLength
* 数组总长度
* @param index
* 数组偏移量
* @return
*/
public static boolean isOutOfArrLength(int arrLength, int index) {
boolean b;
if (arrLength > index) {
b = true;
} else {
b = false;
}
return b;
}
}
java基本数据类型转换成byte[]数组的更多相关文章
- 聊聊java基础,int值强制类型转换成byte
聊聊java基础,int值强制类型转换成byte 知识点:byte.short.char在表达式中会自动提升为int 之前做一个应用时,打印IP地址,因为是用4个byte存储的,所以打印的时候值范围是 ...
- JAVA将文件转换成byte数组(byte[])
/** * 将文件转换成byte数组 * @param filePath 文件File类 通过new File(文件路径) * @return byte数组 */ public static byte ...
- JAVA中文件与Byte数组相互转换的方法
JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...
- Map 转换成byte[] 数组
把Map转换成byte数组,使用 ByteArrayOutputStream和ObjectOutputStream Map<String,String> map = new HashMap ...
- 将文件转换成byte[]数组
代码 /// <summary> /// 将文件转换成byte[] 数组 /// </summary> /// <param name="fileUrl&quo ...
- Java中字符串和byte数组之间的相互转换
1.将字符转换成byte数组 String str = "罗长"; byte[] sb = str.getBytes(); 2.将byte数组转换成字符 byte[] b={(by ...
- PHP实现INT型,SHORT型,STRING转换成BYTE数组
实现PHP实现INT型,SHORT型,STRING转换成BYTE数组的转化: class Bytes { public static function integerToBytes($val) { $ ...
- 将文件File转换成byte数组
代码如下: /** * 将文件转换成byte数组 * @param filePath * @return */ public static byte[] File2byte(File tradeFil ...
- Java String类型转换成Date日期类型
插入数据库时,存入当前日期,需要格式转换 import java.text.SimpleDateFormat; formatter = new SimpleDateFormat( "yyyy ...
随机推荐
- Ubuntu实现双网卡双IP双待机
Ubuntu实现双网卡双IP双待机 待机是借用了手机中的说法,其实是电脑上有两个网卡,一个无线,一个有线的.要实现无线访问外网Google Baidu查资料,有线网卡直接连接开发板.在Ubuntu上配 ...
- HDU 2227 Find the nondecreasing subsequences (线段树)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- MySQL root密码重置报错:mysqladmin: connect to server at 'localhost' failed的解决方案!
-- ==================================================================== -- mysqladmin: connect to s ...
- 整合SSH三大框架用注解时报An AnnotationConfiguration instance is required to use
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's ...
- mysql简单使用增删改查
修改配置文件 在my.in配置文件 找到client 指的是mysql客户端 port3306 default -charachter-set=utf-8 default -charachter-se ...
- F - Free DIY Tour(动态规划,搜索也行)
这道题可用动态规划也可以用搜索,下面都写一下 Description Weiwei is a software engineer of ShiningSoft. He has just excelle ...
- django1.6读书笔记一
reporter是Article中的一个外键,我们可以有多篇文章指向同一个reporter,然后通过使用article_set.all()就可以返回其所有的headline了,也可以添加条件来筛选. ...
- Android学习笔记:如何高效显示图片,避免内存溢出 和 ImageView无法显示大尺寸的图片
因为手机的内存资源是有限的,每个app可使用的内存是受限的.而现在采用高分辨率拍的照片往往很大.如果加载时不注意方法,很有可能会引起java.lang.OutofMemoryError: bitmap ...
- jsp获取一个对象和list对象
DiscHd ks = DiscHdService.getDiscHdByID(code); model.addAttribute("ks", ks); 如果传的是对象,则jsp页 ...
- Docker 安装命令
curl -sSL https://get.daocloud.io/docker | sh