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 工具类的更多相关文章

  1. SFTP工具类

    1.SFTP搭建方法: 地址: http://www.jb51.net/article/101405.htm https://blog.csdn.net/helloloser/article/deta ...

  2. Android-ByteUtil工具类

    Byte处理转换相关的工具类: public class ByteUtil { private ByteUtil(){} /** * 把byte[] 转成 Stirng * @param bytes ...

  3. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  4. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  5. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  6. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  7. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  8. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  9. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

随机推荐

  1. responsive grid

    http://csswizardry.com/csswizardry-grids/ http://unsemantic.com/demo-responsive http://getbootstrap. ...

  2. 使用Entity Framework出错

          在使用的过程中,写了一个例子,结果就报错说      The context cannot be used while the model is being created.      在 ...

  3. 教你用Bootstrap开发漂亮的前端界面

    Bootstrap介绍: Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目. Bootstrap的特点: 一.预处理脚本:虽然可以直 ...

  4. Android 序列化比对

    本文转自:https://www.zybuluo.com/linux1s1s/note/91046 注:部分内容有更改 在Android中使用序列化,无非两种途经: Parcelable 和 Seri ...

  5. EFT4 生成实体类

    创建T4模本拷贝以下代码 <#@ template language="C#" debug="false" hostspecific="true ...

  6. 用es6写一个分数库

    es6发布后nodejs开始更新.最近写一些库发现新特性还是很好用的,于是回来写一个分数库练手. 对于es6本身 ... => 以及 array.includes 很简洁.class依然不是很顺 ...

  7. 给虚拟机发送键盘按键key

    使用举例:virsh send-key 11 KEY_LEFTCTRL KEY_LEFTALT KEY_DELETE作用:发送"ctrl+alt+del"给虚拟机,linux虚拟机 ...

  8. Web负载均衡技术

    Web负载均衡(Load Balancing),简单地说就是给我们的服务器集群分配“工作任务”,而采用恰当的分配方式,对于保护处于后端的Web服务器来说,非常重要. 负载均衡的策略有很多,我们从简单的 ...

  9. annoy安装

    yum install gcc-c++ #linux下需安装c++编译器 sudo pip install annoy

  10. java设计模式之装饰器模式以及在java中作用

    在JAVA I/O类库里有很多不同的功能组合情况,这些不同的功能组合都是使用装饰器模式实现的,下面以FilterInputStream为例介绍装饰器模式的使用  FilterInputStream和F ...