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 ...
随机推荐
- Mybatis框架学习总结-优化Mybatis配置文件中的配置
连接数据库的配置单独放在一个properties文件中 之前,是直接将数据库的连接配置信息卸载了Mybatis的conf.xml文件中,如下: <?xml version="1.0&q ...
- sql server常用性能计数器
https://blog.csdn.net/kk185800961/article/details/52462913?utm_source=blogxgwz5 https://blog.csdn.ne ...
- UVA10026:Shoemaker's Problem(贪心)
题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/K 题目需求:鞋匠有n个任务,第i个任务要花费ti ...
- matplotlib绘制等高线图
参考自Matplotlib Python 画图教程 (莫烦Python)(12)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16 ...
- [笔记]Python中模块互相调用的例子
python中模块互相调用容易出错,经常是在本地路径下工作正常,切换到其他路径来调用,就各种模块找不到了. 解决方法是通过__file__定位当前文件的真实路径,再通过sys.path.append( ...
- idea新建的项目,文件夹右键不能新建class
一般情况下,新建的mave项目,通常没有XXX\src\main\java这个目录,如果手动创建,则又不能右键build与java相关的,强行建立的话,也不会被idea所识别,更不会被虚拟机编译执行. ...
- hdu6215 Brute Force Sorting
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...
- 647. Palindromic Substrings(马拉车算法)
问题 求一个字符串有多少个回文子串 Input: "abc" Output: 3 Input: "aaa" Output: 6 思路和代码(1)--朴素做法 用 ...
- linux上使用wget下载文件
首次安装的centos操作系统是没有安装wget的,所以首先需要先安装wget,然后才能使用wget下载文件. 1.第一步,保证centos能正常连网.使用命令 :yum -y install wg ...
- Decker hello world
Docker 允许在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序. 输出Hello world root@ranxf:/home/ranxf# docker run ...