//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;
}
//byte数组转成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;
}
/**
* 注释:int到字节数组的转换!
*
* @param number
* @return
*/
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的转换!
*
* @param b
* @return
*/
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;
}
/**
* 注释:short到字节数组的转换!
*
* @param s
* @return
*/
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的转换!
*
* @param b
* @return
*/
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;
}
分享到:
相关推荐
下面小编就为大家带来一篇java byte数组与int,long,short,byte的转换实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...
char short int long float double 转换成byte数组
byte数组转换为int 保留几位小数 null转String String转Byte String转Boolean String转Int String转Short String转Double Int转String Double转Long Double转Int Long转Double Long转Int String转Long Long转String
举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character
由于实际开发中这样的类型转换很多,所以Java语言在设计时,没有为该操作设计语法,而是由JVM自动完成。 转换规则 从存储范围小的类型到存储范围大的类型。 具体规则为: byte→short(char)→int→long→float→...
在编程过程中经常用到各种数据类型之间的相互转换,此代码中实现了short、int、long。float、double等类型与byte数组相互转换,根据数据类型的位数不同,对应的数组大小也不同、例如int32位,4个字节,则需要大小为4...
字节转换什么事啊您是否曾经想过通过I2C,SPI,串行或其他协议或总线传输int , short , long , double或任何其他数字类型,但是您已将变量转换为字符串以能够按char进行传输。 该库使您可以将任何数值转换为字节...
一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double型之间的转换,整数和String类型之间的转换,以及处理、显示时间方面的问下面笔者就开发中的一些体会介绍给大家。 我们知道,...
当需要把某个属性值或者文本块内容转换为int、short、long时,不需要先把byte数组转化成string,然后再转化成int、short、long。FastXml直接把byte数组转换成对应的类型。 如果xml文档中有些标签内容你并...
(1)基本类型 byte short int long float double char boolean (2)引用类型 类 数组 接口 枚举 注解 变量:内存中的一块存储空间, 存储的就是常量。 特点:一个变量只能存储一个数据,不能存储多个。 需求:...
在java中的整数类型有四种,分别是byte short in long,本文重点给大家介绍java中的整数类型(short int long),由于byte只是一个字节0或1,在此就不多说了,对java中的整数类型感兴趣的朋友一起学习吧
在switch(expr1)中,expr1只能是一个整数表达式或者枚举常量(更大字体),整数表达式可以是int基本类型或Integer包装类型,由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也...
第一种对应的是java中的整形 int long short byte 而第二种对应java中的 浮点型 float double oracle中的的date 对应java中的 java.util.date java.sql.date 是都可以的 但通常应用java.util.date 因为它的作用范畴...
[signed] short [int] 2Byte unsigned short [int] 2 Byte long [int] 4 Byte unsigned long [int] 4 Byte Visual C++ 6.0: [signed] int 4Byte unsigned int 4Byte [signed] short [in
不同类型数据间的优先关系如下: 低--------------------------------------------->⾼ byte,short,char-> int -> long -> float -> double 运算中,不同类型的数据先转化为同⼀类型,然后进⾏运算,转换规则如下: ...
这些方法是通用类型和内置类型,例如int,boolean,long,short,byte,double,float。 标量类型的数量小而有限。 每个标量类型都有Iterable,Iterator,Collection和List接口的实现。 该库还为仅具有Iterable接口...
Java 有8种基本数据类型: byte int short long double float Boolean char byte int short long 都属于整数类型. Double float 属于浮点类型. Boolean 为布尔类型 Char 为字符型 String 不是基本数据类型.它定义的...
利用java的反射解析Properties文件转成对象 /** * 解析properties文件为对象 * @param * @param propPath * @param cls * @return * @throws InstantiationException * @throws ...
键盘输入1个字符串,如果是整数数字串,如”4353”, 分别按八进制和十六进制输出,以及按byte,short,int,long,char输出;每个数占10位长.每行输出3个数; 如果是带小数数字串,如”243.58”,分别按short,int,long,float...