byte[]与各种数据类型互相转换示例
public class TestCase {
/**
* short到字节数组的转换.
*/
public static byte[] shortToByte(short number) {
int temp = number;
byte[] b = new byte[2];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
temp = temp >> 8;// 向右移8位
}
return b;
}
/**
* 字节数组到short的转换.
*/
public static short byteToShort(byte[] b) {
short s = 0;
short s0 = (short) (b[0] & 0xff);// 最低位
short s1 = (short) (b[1] & 0xff);
s1 <<= 8;
s = (short) (s0 | s1);
return s;
}
/**
* int到字节数组的转换.
*/
public static byte[] intToByte(int number) {
int temp = number;
byte[] b = new byte[4];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
temp = temp >> 8;// 向右移8位
}
return b;
}
/**
* 字节数组到int的转换.
*/
public static int byteToInt(byte[] b) {
int s = 0;
int s0 = b[0] & 0xff;// 最低位
int s1 = b[1] & 0xff;
int s2 = b[2] & 0xff;
int s3 = b[3] & 0xff;
s3 <<= 24;
s2 <<= 16;
s1 <<= 8;
s = s0 | s1 | s2 | s3;
return s;
}
/**
* long类型转成byte数组
*/
public static byte[] longToByte(long number) {
long temp = number;
byte[] b = new byte[8];
for (int i = 0; i < b.length; i++) {
b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 temp = temp
// >> 8;// 向右移8位
}
return b;
}
/**
* 字节数组到long的转换.
*/
public static long byteToLong(byte[] b) {
long s = 0;
long s0 = b[0] & 0xff;// 最低位
long s1 = b[1] & 0xff;
long s2 = b[2] & 0xff;
long s3 = b[3] & 0xff;
long s4 = b[4] & 0xff;// 最低位
long s5 = b[5] & 0xff;
long s6 = b[6] & 0xff;
long s7 = b[7] & 0xff;
// s0不变
s1 <<= 8;
s2 <<= 16;
s3 <<= 24;
s4 <<= 8 * 4;
s5 <<= 8 * 5;
s6 <<= 8 * 6;
s7 <<= 8 * 7;
s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
return s;
}
/**
* double到字节数组的转换.
*/
public static byte[] doubleToByte(double num) {
byte[] b = new byte[8];
long l = Double.doubleToLongBits(num);
for (int i = 0; i < 8; i++) {
b[i] = new Long(l).byteValue();
l = l >> 8;
}
return b;
}
/**
* 字节数组到double的转换.
*/
public static double getDouble(byte[] b) {
long m;
m = b[0];
m &= 0xff;
m |= ((long) b[1] << 8);
m &= 0xffff;
m |= ((long) b[2] << 16);
m &= 0xffffff;
m |= ((long) b[3] << 24);
m &= 0xffffffffl;
m |= ((long) b[4] << 32);
m &= 0xffffffffffl;
m |= ((long) b[5] << 40);
m &= 0xffffffffffffl;
m |= ((long) b[6] << 48);
m &= 0xffffffffffffffl;
m |= ((long) b[7] << 56);
return Double.longBitsToDouble(m);
}
/**
* float到字节数组的转换.
*/
public static void floatToByte(float x) {
//先用 Float.floatToIntBits(f)转换成int
}
/**
* 字节数组到float的转换.
*/
public static float getFloat(byte[] b) {
// 4 bytes
int accum = 0;
for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) {
accum |= (b[shiftBy] & 0xff) << shiftBy * 8;
}
return Float.intBitsToFloat(accum);
}
/**
* char到字节数组的转换.
*/
public static byte[] charToByte(char c){
byte[] b = new byte[2];
b[0] = (byte) ((c & 0xFF00) >> 8);
b[1] = (byte) (c & 0xFF);
return b;
}
/**
* 字节数组到char的转换.
*/
public static char byteToChar(byte[] b){
char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
return c;
}
/**
* string到字节数组的转换.
*/
public static byte[] stringToByte(String str) throws UnsupportedEncodingException{
return str.getBytes("GBK");
}
/**
* 字节数组到String的转换.
*/
public static String bytesToString(byte[] str) {
String keyword = null;
try {
keyword = new String(str,"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return keyword;
}
/**
* object到字节数组的转换
*/
@Test
public void testObject2ByteArray() throws IOException,
ClassNotFoundException {
// Object obj = "";
Integer[] obj = { 1, 3, 4 };
// // object to bytearray
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(obj);
byte[] bytes = bo.toByteArray();
bo.close();
oo.close();
System.out.println(Arrays.toString(bytes));
Integer[] intArr = (Integer[]) testByteArray2Object(bytes);
System.out.println(Arrays.asList(intArr));
byte[] b2 = intToByte(123);
System.out.println(Arrays.toString(b2));
int a = byteToInt(b2);
System.out.println(a);
}
/**
* 字节数组到object的转换.
*/
private Object testByteArray2Object(byte[] bytes) throws IOException,
ClassNotFoundException {
// byte[] bytes = null;
Object obj;
// bytearray to object
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
obj = oi.readObject();
bi.close();
oi.close();
System.out.println(obj);
return obj;
}
}
byte[]与各种数据类型互相转换示例的更多相关文章
- java学习笔记(3)数据类型、源码、反码、补码、精度损失、基本数据类型互相转换
关于java中的数据类型: 1.数据类型的作用是什么? 程序当中有很多数据,每一个数据都是有相关类型的,不同数据类型的数据占用的空间大小不同. 数据类型的作用是指导java虚拟机(JVM)在运行程序的 ...
- java中数据类型的转换
数据类型的转换,分为自动转换和强制转换. 自动转换是程序执行过程中“悄然”进行的转换,不需要用户提前声明,一般是从位数低的类型向位数高的类型转换 强制转换必须在代码中声明,转换顺序不受限制 自动数据类 ...
- Java的基本数据类型与转换
1.1 Java为什么需要保留基本数据类型 http://www.importnew.com/11915.html 基本数据类型对大多数业务相关或网络应用程序没有太大的用处,这些应用一般是采用客户端/ ...
- java的数据类型的转换
一:java的数据类型转换除布尔类型boolean(不能转换)有两种:<一> 自动转换: <二> 强制转换 <一>.自动转换:就是将小的数据类型自动转换成大的数据类 ...
- Java学习笔记之:Java数据类型的转换
一.介绍 数据类型的转换,分为自动转换和强制转换.自动转换是程序在执行过程中“悄然”进行的转换,不需要用户提前声明,一般是从位数低的类型向位数高的类型转换:强制类型转换则必须在代码中声明,转换顺序不受 ...
- 语言基础:C#输入输出与数据类型及其转换
今天学习了C#的定义及特点,Visual Studio.Net的集成开发环境和C#语言基础. C#语言基础资料——输入输出与数据类型及其转换 函数的四要素:名称,输入,输出,加工 输出 Console ...
- java中的基本数据类型的转换
本文参考了如下两篇文章: https://my.oschina.net/joymufeng/blog/139952 http://www.cnblogs.com/lwbqqyumidi/p/37001 ...
- Java基础之数据类型和转换
一.常见的数据类型分类 1.java中基本数据类型分为三大类,即布尔类型,字符型,数值型.其中数值型又分为整型和浮点型.引用数据类型分为类,接口,数组,枚举,注解.具体如下: 注:一个字节 = 8位 ...
- Java数据类型及其转换&&经常用到的快捷键
数据类型 基本数据类型分类 (8种) byte .short. int. long. char. float. double .boolean 1个字节占8位 整数型byte 1字节 -128~1 ...
随机推荐
- Linux Anaconda安装步骤
首选下载wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh 可以去官网查看适合自己的文件https://www ...
- (2.5)Mysql之SQL基础——数据类型
(2.5)Mysql之SQL基础——数据类型 关键词:mysql数据类型 目录: 一.整数型 二.小数型(以下均不能使用无符号) 三.日期时间型 四.字符串型 一.整数型 额外参数示例: int [( ...
- 项目实战:Mahout构建图书推荐系统
前言 本文是Mahout实现推荐系统的又一案例,用Mahout构建图书推荐系统.与之前的两篇文章,思路上面类似,侧重点在于图书的属性如何利用.本文的数据在自于Amazon网站,由爬虫抓取获得. 目录 ...
- Python eval() 的使用:将字符串转换为列表,元祖,字典
eval() 函数用来执行一个字符串表达式,并返回表达式的值. 语法 以下是 eval() 方法的语法: eval(expression[, globals[, locals]]) 参数 expres ...
- appium ios 自动化测试
iOS自动化测试:Appium 从入门到实践https://www.jianshu.com/p/43f858180557appium自动化测试iOS Demohttps://www.jianshu.c ...
- Mock Server 之 moco-runner 使用指南一
文章出处http://ju.outofmemory.cn/entry/96866 用以下命令可以启动moco-runner 服务 java -jar moco-runner-<version&g ...
- Bootstrap 中的插件的学习2(导航)
实例 在导航栏内 下面的实例演示了在导航栏内的下拉菜单的用法: <!DOCTYPE HTML> <html> <head> <link href=" ...
- js中获取时间戳
function conver(){ var date = new Date(); var year = date.getFullYear() var month=date.getMonth()+1; ...
- 如何配置IIS服务器?
1, 先安装IIS 然后安装vs; 注: 顺序颠倒则执行cmd命令: 1,cd \ 2,cd Windows 3, cd Microsoft.NET 4, dir 5,cd Framework 6, ...
- 【Deep Learning】两层CNN的MATLAB实现
想自己动手写一个CNN很久了,论文和代码之间的差距有一个银河系那么大. 在实现两层的CNN之前,首先实现了UFLDL中与CNN有关的作业.然后参考它的代码搭建了一个一层的CNN.最后实现了一个两层的C ...