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[]与各种数据类型互相转换示例的更多相关文章

  1. java学习笔记(3)数据类型、源码、反码、补码、精度损失、基本数据类型互相转换

    关于java中的数据类型: 1.数据类型的作用是什么? 程序当中有很多数据,每一个数据都是有相关类型的,不同数据类型的数据占用的空间大小不同. 数据类型的作用是指导java虚拟机(JVM)在运行程序的 ...

  2. java中数据类型的转换

    数据类型的转换,分为自动转换和强制转换. 自动转换是程序执行过程中“悄然”进行的转换,不需要用户提前声明,一般是从位数低的类型向位数高的类型转换 强制转换必须在代码中声明,转换顺序不受限制 自动数据类 ...

  3. Java的基本数据类型与转换

    1.1 Java为什么需要保留基本数据类型 http://www.importnew.com/11915.html 基本数据类型对大多数业务相关或网络应用程序没有太大的用处,这些应用一般是采用客户端/ ...

  4. java的数据类型的转换

    一:java的数据类型转换除布尔类型boolean(不能转换)有两种:<一> 自动转换: <二> 强制转换 <一>.自动转换:就是将小的数据类型自动转换成大的数据类 ...

  5. Java学习笔记之:Java数据类型的转换

    一.介绍 数据类型的转换,分为自动转换和强制转换.自动转换是程序在执行过程中“悄然”进行的转换,不需要用户提前声明,一般是从位数低的类型向位数高的类型转换:强制类型转换则必须在代码中声明,转换顺序不受 ...

  6. 语言基础:C#输入输出与数据类型及其转换

    今天学习了C#的定义及特点,Visual Studio.Net的集成开发环境和C#语言基础. C#语言基础资料——输入输出与数据类型及其转换 函数的四要素:名称,输入,输出,加工 输出 Console ...

  7. java中的基本数据类型的转换

    本文参考了如下两篇文章: https://my.oschina.net/joymufeng/blog/139952 http://www.cnblogs.com/lwbqqyumidi/p/37001 ...

  8. Java基础之数据类型和转换

    一.常见的数据类型分类 1.java中基本数据类型分为三大类,即布尔类型,字符型,数值型.其中数值型又分为整型和浮点型.引用数据类型分为类,接口,数组,枚举,注解.具体如下: 注:一个字节 = 8位 ...

  9. Java数据类型及其转换&&经常用到的快捷键

    数据类型 基本数据类型分类 (8种) byte .short. int. long. char. float. double .boolean 1个字节占8位   整数型byte 1字节 -128~1 ...

随机推荐

  1. (2.4)备份与还原--WAL与备份原理

    预写式日志(Write-Ahead Logging (WAL))  部分转自:http://www.cnblogs.com/wenBlog/p/4423497.html SQL Server中使用了W ...

  2. C#建WindowForm调用R可视化

    众所周知R软件功能非常强大,可以很好的进行各类统计,并能输出图形.下面介绍一种R语言和C#进行通信的方法,并将R绘图结果显示到WinForm UI界面上的方法,文中介绍的很详细,需要的朋友可以参考下. ...

  3. javaScript动画1 offsetWidth、offsetLeft

    offsetWidth和offsetHeight <!DOCTYPE html> <html lang="en"> <head> <met ...

  4. topcoder SRM712 Div1 LR

    题目: Problem Statement      We have a cyclic array A of length n. For each valid i, element i-1 the l ...

  5. 【android】activity、fragment传值例子

    1:Activity篇 1.1向Activity传值 关键点在于putExtra.如果传递类的话,记得类实现Serializable接口 Intent intent = new Intent(Firs ...

  6. tensorflowxun训练自己的数据集之从tfrecords读取数据

    当训练数据量较小时,采用直接读取文件的方式,当训练数据量非常大时,直接读取文件的方式太耗内存,这时应采用高效的读取方法,读取tfrecords文件,这其实是一种二进制文件.tensorflow为其内置 ...

  7. 网络虚拟化 SDN

    一.Linux Bridge :Linux中的网桥 假设宿主机有 1 块与外网连接的物理网卡 eth0,上面跑了 1 个虚机 VM1,现在有个问题是: 如何让 VM1 能够访问外网? 至少有两种方案 ...

  8. ANE报错fix:Could not generate timestamp: Connection reset.

    如果你打包ANE时候 报了:Could not generate timestamp: Connection reset. 那么很有可能你用了JDK 1.8. 解决方案一 退回到 JDK 1.7,重新 ...

  9. Centos编译安装 LAMP (apache-2.4.7 + mysql-5.5.35 + php 5.5.8)+ Redis

    转载地址:http://www.cnblogs.com/whoamme/p/3530056.html 软件源代码包存放位置:/usr/local/src 源码包编译安装位置:/usr/local/软件 ...

  10. Python3.x:获取代理ip以及使用

    Python3.x:获取代理ip以及使用 python爬虫浏览器伪装 #导入urllib.request模块 import urllib.request #设置请求头 headers=("U ...