byte数组的转换工具类
public class FormatTransfer
{
public static byte[] toLH(int n)
{
byte[] b = new byte[4];
b[0] = (byte)(n & 0xFF);
b[1] = (byte)(n >> 8 & 0xFF);
b[2] = (byte)(n >> 16 & 0xFF);
b[3] = (byte)(n >> 24 & 0xFF);
return b;
}
public static byte[] toHH(int n)
{
byte[] b = new byte[4];
b[3] = (byte)(n & 0xFF);
b[2] = (byte)(n >> 8 & 0xFF);
b[1] = (byte)(n >> 16 & 0xFF);
b[0] = (byte)(n >> 24 & 0xFF);
return b;
}
public static byte[] toLH(short n)
{
byte[] b = new byte[2];
b[0] = (byte)(n & 0xFF);
b[1] = (byte)(n >> 8 & 0xFF);
return b;
}
public static byte[] toHH(short n)
{
byte[] b = new byte[2];
b[1] = (byte)(n & 0xFF);
b[0] = (byte)(n >> 8 & 0xFF);
return b;
}
public static byte[] toLH(float f)
{
return toLH(Float.floatToRawIntBits(f));
}
public static byte[] toHH(float f)
{
return toHH(Float.floatToRawIntBits(f));
}
public static byte[] stringToBytes(String s, int length)
{
while (s.getBytes().length < length) {
s = s + " ";
}
return s.getBytes();
}
public static String bytesToString(byte[] b)
{
StringBuffer result = new StringBuffer("");
int length = b.length;
for (int i = 0; i < length; i++) {
result.append((char)(b[i] & 0xFF));
}
return result.toString();
}
public static byte[] stringToBytes(String s)
{
return s.getBytes();
}
public static int hBytesToInt(byte[] b)
{
int s = 0;
for (int i = 0; i < 3; i++) {
if (b[i] >= 0)
s += b[i];
else {
s = s + 256 + b[i];
}
s *= 256;
}
if (b[3] >= 0)
s += b[3];
else {
s = s + 256 + b[3];
}
return s;
}
public static int lBytesToInt(byte[] b)
{
int s = 0;
for (int i = 0; i < 3; i++) {
if (b[(3 - i)] >= 0)
s += b[(3 - i)];
else {
s = s + 256 + b[(3 - i)];
}
s *= 256;
}
if (b[0] >= 0)
s += b[0];
else {
s = s + 256 + b[0];
}
return s;
}
public static short hBytesToShort(byte[] b)
{
int s = 0;
if (b[0] >= 0)
s += b[0];
else {
s = s + 256 + b[0];
}
s *= 256;
if (b[1] >= 0)
s += b[1];
else {
s = s + 256 + b[1];
}
short result = (short)s;
return result;
}
public static short lBytesToShort(byte[] b)
{
int s = 0;
if (b[1] >= 0)
s += b[1];
else {
s = s + 256 + b[1];
}
s *= 256;
if (b[0] >= 0)
s += b[0];
else {
s = s + 256 + b[0];
}
short result = (short)s;
return result;
}
public static float hBytesToFloat(byte[] b)
{
int i = 0;
Float F = new Float(0.0D);
i = (((b[0] & 0xFF) << 8 | b[1] & 0xFF) << 8 | b[2] & 0xFF) << 8 |
b[3] & 0xFF;
return Float.intBitsToFloat(i);
}
public static float lBytesToFloat(byte[] b)
{
int i = 0;
Float F = new Float(0.0D);
i = (((b[3] & 0xFF) << 8 | b[2] & 0xFF) << 8 | b[1] & 0xFF) << 8 |
b[0] & 0xFF;
return Float.intBitsToFloat(i);
}
public static byte[] bytesReverseOrder(byte[] b)
{
int length = b.length;
byte[] result = new byte[length];
for (int i = 0; i < length; i++) {
result[(length - i - 1)] = b[i];
}
return result;
}
public static void printBytes(byte[] bb)
{
int length = bb.length;
for (int i = 0; i < length; i++) {
System.out.print(bb + " ");
}
System.out.println("");
}
public static void logBytes(byte[] bb) {
int length = bb.length;
String out = "";
for (int i = 0; i < length; i++)
out = out + bb + " ";
}
public static int reverseInt(int i)
{
int result = hBytesToInt(toLH(i));
return result;
}
public static short reverseShort(short s)
{
short result = hBytesToShort(toLH(s));
return result;
}
public static float reverseFloat(float f)
{
float result = hBytesToFloat(toLH(f));
return result;
}
}
分享到:
相关推荐
byte数组操作的工具类,支持byte数组转int,int转byte数组,byte数组转short,short转byte数组。。。 下载的人多了,csdn会自动涨分,现在可以手动调了,我给降到最低了
C# string byte数组转换解析 C# string byte数组转换实现的过程是什么呢?C# string byte数组间的转换需要注意什么呢?C# string byte数组间转换所涉及的方法是什么呢?让我们来看看具体的内容: C# string byte数组...
C#中将byte数组转换为8bit灰度图像
实现CString和char数组的相互转换,实现CString和BYTE数组(16进制)的相互转换
主要解决开发人员遇到的常规问题,针对 C# 结构体 和 Byte 数组之间互相转换遇到的问题,代码中进行了简单封装,方便客户调用,下载可以直接使用,方便快捷。
发送数据时,只能发送byte数组,处理起来比较麻烦不说,如果是和c++等写的程序通信的话,很多的都是传送结构体,在VC6.0中可以很方便的把一个char[]数组转换为一个结构体,而在C#却不能直接把byte数组转换为结构体,...
今天小编就为大家分享一篇java中两个byte数组实现合并的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
C# string byte数组转换解析.pdfC# string byte数组转换解析.pdf
C# string byte数组转换解析.docxC# string byte数组转换解析.docx
Java-byte数组转换成16进制字符串
android byte字节数组转换十六进制字符串一个demo。里面有几种转换测试代码。我的博客地址:http://blog.csdn.net/qq_16064871。
2、把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示。 3、从图片byte数组得到对应图片的格式,生成一张图片保存到磁盘上。 这里的Image是System.Drawing.Image。 以下三个函数分别实现了上述三个...
public static uint ByteToInt(byte[] b) { uint u = 0; int startIndex = 0; if (b.Length == 8) u = (uint)(b[startIndex + 0] | b[startIndex + 1] | b[startIndex + 2] | b[startIndex + 3] | b[startIndex...
ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...
C# Byte数组 数值转花 Int32 Short Float 浮点数 自己写的Test 有问题可以联系我
java基本类型与byte数组互相转换.pdf
2、把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示。 3、从图片byte数组得到对应图片的格式,生成一张图片保存到磁盘上。 这里的Image是System.Drawing.Image。 //Get an image from file Image...
主要整理了Java中byte数组与其他基本类型的转换
1.字符串(String)与Byte[] 的互相转换. 2.Hex(String形式)与Byte[] 的互相转换.
char short int long float double 转换成byte数组