ByteUtil 工具类
ByteUtil 工具类
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.Charset; /**
* @author lishupeng
* @create 2017-05-03 下午 5:39
**/
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) {
byte[] a = {1,2};
System.out.println("111 "+getString(a));; // 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))); }
}
ByteUtil 工具类的更多相关文章
- SFTP工具类
1.SFTP搭建方法: 地址: http://www.jb51.net/article/101405.htm https://blog.csdn.net/helloloser/article/deta ...
- Android-ByteUtil工具类
Byte处理转换相关的工具类: public class ByteUtil { private ByteUtil(){} /** * 把byte[] 转成 Stirng * @param bytes ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- Guava库介绍之实用工具类
作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...
- Java程序员的日常—— Arrays工具类的使用
这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...
- .net使用正则表达式校验、匹配字符工具类
开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...
随机推荐
- P1346 电车(dijkstra)
P1346 电车 题目描述 在一个神奇的小镇上有着一个特别的电车网络,它由一些路口和轨道组成,每个路口都连接着若干个轨道,每个轨道都通向一个路口(不排除有的观光轨道转一圈后返回路口的可能).在每个路口 ...
- ArrayMap java.lang.ArrayIndexOutOfBoundsException
错误堆栈: java.lang.ArrayIndexOutOfBoundsException: length=0; index=1 at android.support.v4.util.SimpleA ...
- 用ServiceStack操作使用redis的问题
最近在学习Redis,查阅网上很多资料后使用SericeStack连接redis.在nuget中下载ServiceStack.Redis,主要使用到四个dll 但是运行之后会出现一堆奇怪问题:没有实现 ...
- Java - 问题集 - linux下,jar: command not found
linux下的找不到jar命令解决方法如下: 1. 确认jdk是否已安装 2. 检查jdk环境变量是否已设置,并且确认该设置已生效 3. 1,2两步均正常时,建立jar的软链接 # cd /usr/b ...
- mysql 导入CSV数据 [转]
转自: http://blog.chinaunix.net/uid-23284114-id-3196638.html MYSQL LOAD DATA INFILE命令可以把csv平面文件中的数据导 ...
- dispaly:-webkit-box 布局中的坑
dispaly:-webkit-box 具体用法 这里大家可以网上查, 这里说下里面的坑 里面的子对象设置-webkit-box-flex: 1 -webkit-box-flex: 2 时:一般两个子 ...
- Android当代码方法超过65536个时,在2.3机器上会不能安装,出现INSTALL_FAILED_DEXOPT错误
今天打包时,发现2.3机器,产生的APK在某些机器上不能安装(Installation error: INSTALL_FAILED_DEXOPT),针对这个问题的一个可能解释是:最新的ADT和SDK ...
- APPium-python实例(记录)
https://github.com/appium/sample-code/tree/master/sample-code/examples/python
- 国际电话区号SQL
CREATE TABLE `phone_prefix` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `country` varchar(30) N ...
- cocos2d-x 精灵
Sprite有两个父类:BatchableNode批量创建精灵(大量重复的比如子弹)和pyglet.sprite.Sprite. 精灵的创建