com.hankcs.hanlp.utility
类 ByteUtil

java.lang.Object
  继承者 com.hankcs.hanlp.utility.ByteUtil

public class ByteUtil
extends Object

对数字和字节进行转换。
基础知识:
假设数据存储是以大端模式存储的:
byte: 字节类型 占8位二进制 00000000
char: 字符类型 占2个字节 16位二进制 byte[0] byte[1]
int : 整数类型 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3]
long: 长整数类型 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4] byte[5] byte[6] byte[7]
float: 浮点数(小数) 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3]
double: 双精度浮点数(小数) 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4] byte[5] byte[6] byte[7]


构造方法摘要
ByteUtil()
           
 
方法摘要
static char bytesHighFirstToChar(byte[] bytes, int start)
          字节数组转char,高位在前,适用于读取writeChar的数据
static double bytesHighFirstToDouble(byte[] bytes, int start)
          读取double,高位在前
static float bytesHighFirstToFloat(byte[] bytes, int start)
          读取float,高位在前
static int bytesHighFirstToInt(byte[] bytes, int start)
          字节数组和整型的转换,高位在前,适用于读取writeInt的数据
static long bytesHighFirstToLong(byte[] b)
           
static char bytesToChar(byte[] b)
          将一个2位字节数组转换为char字符。
static double bytesToDouble(byte[] b)
          将一个8位字节数组转换为双精度浮点数。
static float bytesToFloat(byte[] b)
          将一个4位字节数组转换为浮点数。
static int bytesToInt(byte[] b)
          将一个4位字节数组转换为4整数。
static int bytesToInt(byte[] bytes, int start)
          字节数组和整型的转换
static long bytesToLong(byte[] b)
          将一个8位字节数组转换为长整数。
static byte[] charToBytes(char c)
          将一个char字符转换位字节数组(2个字节),b[0]存储高位字符,大端
static char[] convertIntToTwoChar(int n)
           
static int convertTwoCharToInt(char high, char low)
           
static byte[] doubleToBytes(double d)
          将一个双精度浮点数转换位字节数组(8个字节),b[0]存储高位字符,大端
static byte[] floatToBytes(float f)
          将一个浮点数转换为字节数组(4个字节),b[0]存储高位字符,大端
static byte[] intToBytes(int i)
          将一个整数转换位字节数组(4个字节),b[0]存储高位字符,大端
static byte[] longToBytes(long l)
          将一个长整数转换位字节数组(8个字节),b[0]存储高位字符,大端
static void writeUnsignedInt(DataOutputStream out, int uint)
          无符号整型输出
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

ByteUtil

public ByteUtil()
方法详细信息

bytesToChar

public static char bytesToChar(byte[] b)
将一个2位字节数组转换为char字符。
注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。

参数:
b - 字节数组
返回:
char字符

bytesToDouble

public static double bytesToDouble(byte[] b)
将一个8位字节数组转换为双精度浮点数。
注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。

参数:
b - 字节数组
返回:
双精度浮点数

bytesHighFirstToDouble

public static double bytesHighFirstToDouble(byte[] bytes,
                                            int start)
读取double,高位在前

参数:
bytes -
start -
返回:

bytesToFloat

public static float bytesToFloat(byte[] b)
将一个4位字节数组转换为浮点数。
注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。

参数:
b - 字节数组
返回:
浮点数

bytesToInt

public static int bytesToInt(byte[] b)
将一个4位字节数组转换为4整数。
注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。

参数:
b - 字节数组
返回:
整数

bytesToLong

public static long bytesToLong(byte[] b)
将一个8位字节数组转换为长整数。
注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。

参数:
b - 字节数组
返回:
长整数

bytesHighFirstToLong

public static long bytesHighFirstToLong(byte[] b)

charToBytes

public static byte[] charToBytes(char c)
将一个char字符转换位字节数组(2个字节),b[0]存储高位字符,大端

参数:
c - 字符(java char 2个字节)
返回:
代表字符的字节数组

doubleToBytes

public static byte[] doubleToBytes(double d)
将一个双精度浮点数转换位字节数组(8个字节),b[0]存储高位字符,大端

参数:
d - 双精度浮点数
返回:
代表双精度浮点数的字节数组

floatToBytes

public static byte[] floatToBytes(float f)
将一个浮点数转换为字节数组(4个字节),b[0]存储高位字符,大端

参数:
f - 浮点数
返回:
代表浮点数的字节数组

intToBytes

public static byte[] intToBytes(int i)
将一个整数转换位字节数组(4个字节),b[0]存储高位字符,大端

参数:
i - 整数
返回:
代表整数的字节数组

longToBytes

public static byte[] longToBytes(long l)
将一个长整数转换位字节数组(8个字节),b[0]存储高位字符,大端

参数:
l - 长整数
返回:
代表长整数的字节数组

bytesToInt

public static int bytesToInt(byte[] bytes,
                             int start)
字节数组和整型的转换

参数:
bytes - 字节数组
返回:
整型

bytesHighFirstToInt

public static int bytesHighFirstToInt(byte[] bytes,
                                      int start)
字节数组和整型的转换,高位在前,适用于读取writeInt的数据

参数:
bytes - 字节数组
返回:
整型

bytesHighFirstToChar

public static char bytesHighFirstToChar(byte[] bytes,
                                        int start)
字节数组转char,高位在前,适用于读取writeChar的数据

参数:
bytes -
start -
返回:

bytesHighFirstToFloat

public static float bytesHighFirstToFloat(byte[] bytes,
                                          int start)
读取float,高位在前

参数:
bytes -
start -
返回:

writeUnsignedInt

public static void writeUnsignedInt(DataOutputStream out,
                                    int uint)
                             throws IOException
无符号整型输出

参数:
out -
uint -
抛出:
IOException

convertTwoCharToInt

public static int convertTwoCharToInt(char high,
                                      char low)

convertIntToTwoChar

public static char[] convertIntToTwoChar(int n)


Copyright © 2014–2015 鐮佸啘鍦�/a>. All rights reserved.