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 ...
随机推荐
- 【操作系统】:Main features of the X86-64
The combination of the new hardware supplied by Intel and AMD ,and the new versions of GCC targeting ...
- JAVA GUI学习 - 窗体背景图片设置方法:重写paintComponent(Graphics g)方法
public class BackgroundImage extends JFrame { public BackgroundImage() { this.setTitle("窗体背景图片设 ...
- java学习之反射
package com.gh.ref; public class Person { private String name; private int age; private char sex; pr ...
- ligh@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.3
ligh@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.3
- Swap file ".Podfile.swp" already exists!
解决Swap file ".ceshi.c.swp" already exists!问题 关于swp文件:使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一 ...
- javascript实现模仿迅雷电影评分
效果图: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- CentOS6.5 下在Nginx中添加SSL证书以支持HTTPS协议访问
参考文献: 1. NginxV1.8.0安装与配置 2. CentOS下在Nginx中添加SSL证书以支持HTTPS协议访问 3. nginx配置ssl证书的方法 4.nginx强制使用https访问 ...
- POJ 1279 Art Gallery 半平面交求多边形核
第一道半平面交,只会写N^2. 将每条边化作一个不等式,ax+by+c>0,所以要固定顺序,方便求解. 半平面交其实就是对一系列的不等式组进行求解可行解. 如果某点在直线右侧,说明那个点在区域内 ...
- 分享一个Redis帮助类
最近在项目中使用了redis来存储已经下载过的URL,项目中用的是ServiceStack来操作Redis,一开始ServiceStack的版本用的是最新的,后来发现ServiceStack已经商业化 ...
- springMVC中得到request对象,session对象
RequestAttributes ra = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((S ...