Java基本类型与byte数组之间相互转换方法
作者:jingxian 时间:2023-11-16 22:49:23
Java基本类型与byte数组之间相互转换,刚刚写的
package cn.teaey.utils;
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)));
}
}
标签:java,基本类型,数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android基于SwipeRefreshLayout实现类QQ的侧滑删除
2023-05-22 14:49:19
![](https://img.aspxhome.com/file/2023/2/138352_0s.gif)
Java date format时间格式化操作示例
2021-10-28 19:12:24
Android Camera开发实现可复用的相机组件
2023-04-08 20:34:56
通过实例学习Either 树和模式匹配
2023-05-21 02:02:41
![](https://img.aspxhome.com/file/2023/1/62131_0s.gif)
java版微信公众平台消息接口应用示例
2022-10-04 10:22:58
android利用ContentResolver访问者获取手机短信信息
2022-02-02 15:02:37
![](https://img.aspxhome.com/file/2023/1/116051_0s.jpg)
Android实现Path平滑的涂鸦效果实例
2023-12-11 23:07:45
![](https://img.aspxhome.com/file/2023/3/97863_0s.jpg)
List集合多个复杂字段判断去重的案例
2022-08-01 16:23:28
Android自定义软键盘的步骤记录
2023-04-08 07:05:50
![](https://img.aspxhome.com/file/2023/2/99792_0s.jpg)
Spring MVC 学习 之 - URL参数传递详解
2022-03-04 22:05:12
Java多线程ThreadPoolExecutor详解
2023-11-23 18:39:32
工作中禁止使用Executors快捷创建线程池原理详解
2021-11-24 20:55:48
![](https://img.aspxhome.com/file/2023/9/75439_0s.png)
Android自定义View之绘制圆形头像功能
2022-09-27 19:24:29
![](https://img.aspxhome.com/file/2023/5/92575_0s.png)
Java 8新的时间日期库的20个使用示例
2022-11-06 06:55:00
详解C#如何优雅地终止线程
2023-11-21 11:41:31
![](https://img.aspxhome.com/file/2023/0/108620_0s.png)
Java 实战项目锤炼之校园宿舍管理系统的实现流程
2023-10-15 07:00:58
![](https://img.aspxhome.com/file/2023/6/101956_0s.png)
c#中DataTable转List的2种方法示例
2022-05-10 20:51:09
C#基于时间轮调度实现延迟任务详解
2023-07-06 08:00:49
Android实现通讯录效果——获取手机号码和姓名
2021-10-25 11:53:59
![](https://img.aspxhome.com/file/2023/1/118961_0s.gif)
C#中的timer与线程使用
2023-08-21 00:13:11