show you the code~!
/**
* 常见数字类型和byte数组的互相转换
* @author ERic
*
*/
public class BytesHelper {
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;
}
return b;
}
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;
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;
}
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;
}
return b;
}
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;
}
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;
}
return b;
}
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的转换实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
char short int long float double 转换成byte数组
ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...
int转换为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转...
字节转换什么事啊您是否曾经想过通过I2C,SPI,串行或其他协议或总线传输int , short , long , double或任何其他数字类型,但是您已将变量转换为字符串以能够按char进行传输。 该库使您可以将任何数值转换为字节...
自动类型转换,也称隐式类型转换,是指不需要书写代码,由系统自动完成的类型转换。由于实际开发中这样的类型转换很多,所以Java语言在设计时,没有为该操作设计语法,而是由JVM自动完成。 转换规则 从存储范围小的...
float、double等类型与byte数组相互转换,根据数据类型的位数不同,对应的数组大小也不同、例如int32位,4个字节,则需要大小为4的byte数组来进行转换,详情请看代码,里面有转换的实现和详细的注释
举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character
在switch(expr1)中,expr1只能是一个整数表达式或者枚举常量(更大字体),整数表达式可以是int基本类型或Integer包装类型,由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也...
一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double型之间的转换,整数和String类型之间的转换,以及处理、显示时间方面的问...byte、short、int、long;浮点型float、double。其
byte→short(char)→int→long→float→double 也就是说byte类型的变量可以自动转换为short类型,示例代码: byte b = 10; short sh = b; 在类型转换时可以跳跃。示例代码: byte b1 = 100; int n = b1; ...
--------------------------------------->⾼ byte,short,char-> int -> long -> float -> double 运算中,不同类型的数据先转化为同⼀类型,然后进⾏运算,转换规则如下: 基本数据类型的强制转换: 强制类型转换,...
第一章、基础一 、基本类型数字 Double Float Long Int Short Byte ( 常量可以使用下划线)转换 toByte toShort t
当需要把某个属性值或者文本块内容转换为int、short、long时,不需要先把byte数组转化成string,然后再转化成int、short、long。FastXml直接把byte数组转换成对应的类型。 如果xml文档中有些标签内容你并...
[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
键盘输入1个字符串,如果是整数数字串,如”4353”, 分别按八进制和十六进制输出,以及按byte,short,int,long,char输出;每个数占10位长.每行输出3个数; 如果是带小数数字串,如”243.58”,分别按short,int,long,float...
int HufBlock(BYTE dchufindex,BYTE achufindex); int DecodeElement(); BYTE ReadByte(); int WaterMarkExtractDecode(); void IQtIZzMCUComponent(short flag); void IZzMCUComponent(short flag); void ...
返回值的类型 往两者类型大的方向转(byte<short<int<float<long) //特殊情况(含char类型) char和一个byte/short/int 结果为 int char和一个float/double 结果为float/double 二 表达式2和表达式3都是常量: ...
引用类型和原始类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以哪种类型的数据结构存储,当引用类型和原始类型用作某个类的实例数据时所指定的缺省值。对象引用实例变量的缺省值为 null,而原始...
* byte short int long float double boolean char * 0 0 0 0 0.0 0.0 false 空格 * * 2.引用数据类型 * String。。。。。。等, 还可以是我们自己封装的类 * 默认值:都是null * @param args */