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 ...
随机推荐
- BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
1609: [Usaco2008 Feb]Eating Together麻烦的聚餐 Description 为了避免餐厅过分拥挤,FJ要求奶牛们分3批就餐.每天晚饭前,奶牛们都会在餐厅前排队入内,按F ...
- Poj 1002 487-3279(二叉搜索树)
题目链接:http://poj.org/problem?id=1002 思路分析:先对输入字符进行处理,转换为标准形式:插入标准形式的电话号码到查找树中,若有相同号码计数器增加1,再中序遍历查找树. ...
- iOS中的图像处理(二)——卷积运算
关于图像处理中的卷积运算,这里有两份简明扼要的介绍:文一,文二. 其中,可能的一种卷积运算代码如下: - (UIImage*)applyConvolution:(NSArray*)kernel { C ...
- SQL语句简单记录
SQL SERVER 新增列与默认值 alter table 表名 add 列明 bit default 0 not null 删除列(容易删除失败) alter table 表名 drop colu ...
- 数据库迁移(SQL SERVER导入数据到MySql)
地址:http://blog.csdn.net/jiaohougenyang/article/details/44937801 背景:项目最开始时使用的是SQL Server数据库,业务需求现要将数据 ...
- Two-phase Termination
本文参阅[http://ifeve.com/java-two-phase-termination/] Two-phase Termination模式简介 停止线程是一个目标简单而实现却不那么简单的任务 ...
- UVALive 6467 Strahler Order 拓扑排序
这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ ...
- jquey的 ajax请求的几种方式
在jquery中,提供了集中方法来进行ajax操作 一.$.get(url,[data],[callback]) 向服务器发起get操作. 说明:url为请求地址,data为请求数据的列表(json对 ...
- linux下find命令-atime,-ctime,-mtime真正含义
linux下的-atime,-ctime,-mtime含义我们经常会在论坛或者群里面被问到,在linux或者unix下如何查看某文件的创建日期?经常又会有人说用find命令加选项-ctime,其实这里 ...
- 3DShader之投影贴图(Projective Texturing)
相信大家都应该玩过CS或者CF吧,游戏里面有个喷图功能,就是按一个T键就能在墙上或者地板上喷出自己预先设定的图案. 而刚好这就是我们这个Shader所需实现的内容.由于没有潜伏者的贴图,我只有从这个图 ...