Java基本类型与byte数组之间相互转换方法
作者:jingxian 发布时间:2023-11-16 22:49:23
标签:java,基本类型,数组
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)));
}
}
0
投稿
猜你喜欢
- 为什么要自定义缓存注解?Spring Cache本身提供@Cacheable、@CacheEvict、@CachePut等缓存注解,为什么还
- 最近 IDEA 2020最后一个版本发布了 ,已经内置了Lombok插件,SpringBoot 2.1.x之后的版本也在Starter中内置
- package list;import java.util.ArrayList;/** * Java约瑟夫问题: n个人(不同id
- 目录前言:1.委托的声明1.1.delegate1.1.1. 0-23个参数,可以有返回值也可以没有返回值1.1.2.委托的调用1.1.3.
- 本文实例讲述了C#多线程学习之操纵一个线程的方法。分享给大家供大家参考。具体实现方法如下:下面我们就动手来创建一个线程,使用Thread类创
- 本文介绍C#编程时,给定一个字符串,如何判断它是不是一个日期。本文将介绍两种方法,一个是判断字符串是否是时间,如果是就转换为一个时间变量,第
- File类简介package com.file;import java.io.File;import java.io.IOException
- 1. 树型结构1.1概念树是一种 非线性 的数据结构,它是由 n ( n>=0 )个有限结点组成一个具有层次关系的集合。 把它叫做树是
- 一、背景在上一篇文章中,我们使用Seata整合了SpringBoot,在这篇文章中我们使用Seata整合SpringCloud。同时有了上一
- 作者:精致码农出处:http://cnblogs.com/willick联系:liam.wang@live.com最近工作中遇到一个这样的需
- 本文实例为大家分享了Java实现简单ATM机功能的具体代码,供大家参考,具体内容如下项目介绍基于大家使用银行卡在ATM机取款操作,进行相对应
- (1)很多朋友在使用genymotion开发安卓应用程序的时候,会遇见完全正确的安装但是在运行的时候仍然找不到,genymotion上的设备
- 最近这款“跳一跳”很火,在段子里面看到有人才放了张画着坐标的纸在手机上,说根据
- 直接上代码新建DecimalInputTextWatcher类继承TextWatcher (代码可直接复制使用) import androi
- 公司在使用定时任务的时候,使用的是spring scheduled。代码如下:@EnableSchedulingpublic class T
- Unity中的PostProcessScene:深入解析与实用案例在Unity游戏开发中,我们经常需要对场景进行后处理,以实现更丰富的视觉效
- 本文实例讲述了Android编程判断SD卡是否存在及使用容量查询实现方法。分享给大家供大家参考,具体如下:1.判断SD卡是否存在 返回tru
- 哈希 Hash 算法介绍哈希算法也叫散列算法, 不过英文单词都是 Hash, 简单一句话概括, 就是可以把任意长度的输入信息通过算法变换成固
- 读语句: String str = ConfigurationManager.AppSettings["DemoKey"
- bind函数定义在头文件 functional 中。可以将 bind 函数看作一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用