Bytes to float int String Double
来源:互联网 发布:一建机电知乎 编辑:程序博客网 时间:2024/04/30 20:40
package com.helo.datReader;import java.nio.charset.Charset;public class ByteUtil { public static byte[] getBytes(short data) { byte[] bytes = new byte[2]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data & 0xff00) >> 8); return bytes; } public static byte[] getBytes(char data) { byte[] bytes = new byte[2]; bytes[0] = (byte) (data); bytes[1] = (byte) (data >> 8); return bytes; } public static byte[] getBytes(int data) { byte[] bytes = new byte[4]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data & 0xff00) >> 8); bytes[2] = (byte) ((data & 0xff0000) >> 16); bytes[3] = (byte) ((data & 0xff000000) >> 24); return bytes; } public static byte[] getBytes(long data) { byte[] bytes = new byte[8]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data >> 8) & 0xff); bytes[2] = (byte) ((data >> 16) & 0xff); bytes[3] = (byte) ((data >> 24) & 0xff); bytes[4] = (byte) ((data >> 32) & 0xff); bytes[5] = (byte) ((data >> 40) & 0xff); bytes[6] = (byte) ((data >> 48) & 0xff); bytes[7] = (byte) ((data >> 56) & 0xff); return bytes; } public static byte[] getBytes(float data) { int intBits = Float.floatToIntBits(data); return getBytes(intBits); } public static byte[] getBytes(double data) { long intBits = Double.doubleToLongBits(data); return getBytes(intBits); } public static byte[] getBytes(String data, String charsetName) { Charset charset = Charset.forName(charsetName); return data.getBytes(charset); } public static byte[] getBytes(String data) { return getBytes(data, "GBK"); } public static short getShort(byte[] bytes) { return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8))); } public static char getChar(byte[] bytes) { return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8))); } public static int getInt(byte[] bytes) { return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24)); } public static long getLong(byte[] bytes) { return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24)) | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56)); } public static float getFloat(byte[] bytes) { return Float.intBitsToFloat(getInt(bytes)); } public static double getDouble(byte[] bytes) { long l = getLong(bytes); System.out.println(l); return Double.longBitsToDouble(l); } public static String getString(byte[] bytes, String charsetName) { return new String(bytes, Charset.forName(charsetName)); } public static String getString(byte[] bytes) { return getString(bytes, "GBK"); } public static void main(String[] args) { short s = 122; int i = 122; long l = 1222222; char c = 'a'; float f = 122.22f; double d = 122.22; String string = "我是好孩子"; System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(c); System.out.println(f); System.out.println(d); System.out.println(string); System.out.println("**************"); System.out.println(getShort(getBytes(s))); System.out.println(getInt(getBytes(i))); System.out.println(getLong(getBytes(l))); System.out.println(getChar(getBytes(c))); System.out.println(getFloat(getBytes(f))); System.out.println(getDouble(getBytes(d))); System.out.println(getString(getBytes(string))); } }
0 0
- Bytes to float int String Double
- string to int to double and int double to string
- String转换为数值double,float,int
- Java String转int、float、double
- [Scala--基础]--->类型转换(String to Double 、Long、Float和Int)
- float chang to bytes
- int float double BigDecimal
- c/c++ bytes与int,short,long,float,double相互转换
- String与int float double 类型之间的转换
- C# double float int string 与 byte数组 相互转化
- C# double float int string 与 byte数组 相互转化
- C# double float int string 与 byte数组 相互转化
- 实现int float double 转换成std::string
- C# double float int string 与 byte数组 相互转化
- CString string char * int double float 之间转化大全
- C++中将string类型转换为int, float, double类型
- [C++]string到double,float,int的转换
- C++中将string类型转换为int, float, double类型
- Objc_msgSend 方法
- CDH 修改集群IP
- args.Holder()关于v8 Javascript engine 的使用方法研究 (二
- mysql和oracle
- ngx_http_request_t结构体
- Bytes to float int String Double
- JAVA正则表达式:Pattern类与Matcher类详解
- c++编程中遇到的错误定位
- 需要补习的知识
- Centos7 下 spark1.6.1_hadoop2.6 分布式集群环境搭建
- SVN在Android Studio中的配置
- Eureka与ZooKeeper 的比较
- 1)unknown symbol问题
- AbstractQueuedSynchronizer 讲解 ---推荐