Java的类库支持完全不如C#,比如时间类,比如数据类型转换类等等,难道是我自己没找到吗?

下面是字节转换类,byte[]与short, int, long, float, double, String相互转换;网络字节序htons, htonl等实现;byte[]转十六进制字符串、二进制字符串实现。

希望对朋友有用,如果有更好的方法,请提醒我。

public class ByteUtil {
/**
* 转换short为byte
*
* @param b
* @param s
* 需要转换的short
* @param index
*/
public static void putShort(byte b[], short s, int index) {
b[index + 1] = (byte) (s >> 8);
b[index + 0] = (byte) (s >> 0);
} /**
* 通过byte数组取到short
*
* @param b
* @param index
* 第几位开始取
* @return
*/
public static short getShort(byte[] b, int index) {
return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff));
} /**
* 转换int为byte数组
*
* @param bb
* @param x
* @param index
*/
public static void putInt(byte[] bb, int x, int index) {
bb[index + 3] = (byte) (x >> 24);
bb[index + 2] = (byte) (x >> 16);
bb[index + 1] = (byte) (x >> 8);
bb[index + 0] = (byte) (x >> 0);
} /**
* 通过byte数组取到int
*
* @param bb
* @param index
* 第几位开始
* @return
*/
public static int getInt(byte[] bb, int index) {
return (int) ((((bb[index + 3] & 0xff) << 24)
| ((bb[index + 2] & 0xff) << 16)
| ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0)));
} /**
* 转换long型为byte数组
*
* @param bb
* @param x
* @param index
*/
public static void putLong(byte[] bb, long x, int index) {
bb[index + 7] = (byte) (x >> 56);
bb[index + 6] = (byte) (x >> 48);
bb[index + 5] = (byte) (x >> 40);
bb[index + 4] = (byte) (x >> 32);
bb[index + 3] = (byte) (x >> 24);
bb[index + 2] = (byte) (x >> 16);
bb[index + 1] = (byte) (x >> 8);
bb[index + 0] = (byte) (x >> 0);
} /**
* 通过byte数组取到long
*
* @param bb
* @param index
* @return
*/
public static long getLong(byte[] bb, int index) {
return ((((long) bb[index + 7] & 0xff) << 56)
| (((long) bb[index + 6] & 0xff) << 48)
| (((long) bb[index + 5] & 0xff) << 40)
| (((long) bb[index + 4] & 0xff) << 32)
| (((long) bb[index + 3] & 0xff) << 24)
| (((long) bb[index + 2] & 0xff) << 16)
| (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));
} /**
* 字符到字节转换
*
* @param ch
* @return
*/
public static void putChar(byte[] bb, char ch, int index) {
int temp = (int) ch;
// byte[] b = new byte[2];
for (int i = 0; i < 2; i ++ ) {
bb[index + i] = new Integer(temp & 0xff).byteValue(); // 将最高位保存在最低位
temp = temp >> 8; // 向右移8位
}
} /**
* 字节到字符转换
*
* @param b
* @return
*/
public static char getChar(byte[] b, int index) {
int s = 0;
if (b[index + 1] > 0)
s += b[index + 1];
else
s += 256 + b[index + 0];
s *= 256;
if (b[index + 0] > 0)
s += b[index + 1];
else
s += 256 + b[index + 0];
char ch = (char) s;
return ch;
} /**
* float转换byte
*
* @param bb
* @param x
* @param index
*/
public static void putFloat(byte[] bb, float x, int index) {
// byte[] b = new byte[4];
int l = Float.floatToIntBits(x);
for (int i = 0; i < 4; i++) {
bb[index + i] = new Integer(l).byteValue();
l = l >> 8;
}
} /**
* 通过byte数组取得float
*
* @param bb
* @param index
* @return
*/
public static float getFloat(byte[] b, int index) {
int l;
l = b[index + 0];
l &= 0xff;
l |= ((long) b[index + 1] << 8);
l &= 0xffff;
l |= ((long) b[index + 2] << 16);
l &= 0xffffff;
l |= ((long) b[index + 3] << 24);
return Float.intBitsToFloat(l);
} /**
* double转换byte
*
* @param bb
* @param x
* @param index
*/
public static void putDouble(byte[] bb, double x, int index) {
// byte[] b = new byte[8];
long l = Double.doubleToLongBits(x);
for (int i = 0; i < 8; i++) {
bb[index + i] = new Long(l).byteValue();
l = l >> 8;
}
} /**
* 通过byte数组取得double
*
* @param bb
* @param index
* @return
*/
public static double getDouble(byte[] b, int index) {
long l;
l = b[0];
l &= 0xff;
l |= ((long) b[1] << 8);
l &= 0xffff;
l |= ((long) b[2] << 16);
l &= 0xffffff;
l |= ((long) b[3] << 24);
l &= 0xffffffffl;
l |= ((long) b[4] << 32);
l &= 0xffffffffffl;
l |= ((long) b[5] << 40);
l &= 0xffffffffffffl;
l |= ((long) b[6] << 48);
l &= 0xffffffffffffffl;
l |= ((long) b[7] << 56);
return Double.longBitsToDouble(l);
} public static void putHexString(byte[] bb, String s, int index){
for (int i = 0; i < s.length(); i+=2) {
byte c= (byte)( charToByte(s.charAt(i))<<4 | charToByte(s.charAt(i+1)));
bb[index+(i>>1)] = c;
}
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
} public static String getHexString(byte[] b, int index, int count){
StringBuilder stringBuilder = new StringBuilder("");
if (b == null || index < 0 || b.length < index + count ) {
return null;
}
for (int i = index; i < count + index; i++) {
int v = b[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
} public static String getBinaryString(int d,int length){
StringBuilder stringBuilder = new StringBuilder("");
String hv = Integer.toBinaryString(d);
for (int j = 0; j < length - hv.length(); j++) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
return stringBuilder.toString();
} public static String getBinaryReverseString(int d,int length){
StringBuilder stringBuilder = new StringBuilder("");
String hv = Integer.toBinaryString(d);
for (int j = 0; j < length - hv.length(); j++) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
stringBuilder.reverse(); return stringBuilder.toString();
} //网络字节逆序
public static byte[] ReversEndian(byte b[],int count, boolean big)
{
byte by;
byte data[] = new byte[count];
for(int i=0;i<count;i++)
{
data[i] = b[i];
}
if(big==false)
{
for(int i=0;i<count;i++)
{
by = b[i];
data[count-i-1] = by;
}
}
return data;
}
public static short htons(short s){
short rslt = 0;
byte [] bs1 = new byte[2];
ByteUtil.putShort(bs1, s, 0);
byte[] bs2 = ReversEndian(bs1, 2, false);
rslt = ByteUtil.getShort(bs2, 0);
return rslt;
}
public static int htonl(int d){
int rslt = 0;
byte [] bs1 = new byte[4];
ByteUtil.putInt(bs1, d, 0);
byte[] bs2 = ReversEndian(bs1, 4, false);
rslt = ByteUtil.getInt(bs2, 0);
return rslt;
}
}

Java字节转换类实现的更多相关文章

  1. Java时间转换类实现

    Java时间类型非常的差,首先版本问题,本人使用java在Android平台开发.很多Data类的方法已经不提倡使用,一个时间,居然要使用Calendar.DateFormat等类共同编码,非常麻烦. ...

  2. Java 编程的动态性,第 5 部分: 动态转换类--转载

    在第 4 部分“ 用 Javassist 进行类转换”中,您学习了如何使用 Javassist 框架来转换编译器生成的 Java 类文件,同时写回修改过的类文件.这种类文件转换步骤对于做出持久变更是很 ...

  3. Java IO 转换流 字节转字符流

    Java IO 转换流 字节转字符流 @author ixenos 字节流 输入字节流:---------| InputStream 所有输入字节流的基类. 抽象类.------------| Fil ...

  4. 深入理解Java虚拟机(类文件结构+类加载机制+字节码执行引擎)

    目录 1.类文件结构 1.1 Class类文件结构 1.2 魔数与Class文件的版本 1.3 常量池 1.4 访问标志 1.5 类索引.父索引与接口索引集合 1.6 字段表集合 1.7 方法集合 1 ...

  5. 日期转换类 DateConverter.java

    package com.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.D ...

  6. Java中Pattern类的quote方法将任何字符串(包括正则表达式)都转换成字符串常量,不具有任何匹配功能

    Java中Pattern类的quote方法将任何字符串(包括正则表达式)都转换成字符串常量,不具有任何匹配功能. 下面是个例子: import org.junit.Test; import java. ...

  7. 我的Android进阶之旅------>Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换)

    Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换) 有时候要做出如下所示的展示文件大小的效果时候,需要对文件大小进行转换,然后再进行相关的代码逻辑编写. 下面是一个Java ...

  8. 【java】查看Java字节码文件内容的方法+使用javap找不到类 解决方法

    研究synchronized底层实现,涉及到查看java字节码的需要 前提是,你的PC已经成功安装了JDK并别配置了环境变量. ==========查看方法========= 一.javap查看简约字 ...

  9. 用MyEclipse将java文件转换成UML类图

    用MyEclipse将java文件转换成UML类图 参考: 用MyEclipse将java文件转换成UML类图 - 君临天下的博客 - CSDN博客  http://blog.csdn.net/dan ...

随机推荐

  1. caffe之(四)全连接层

    在caffe中,网络的结构由prototxt文件中给出,由一些列的Layer(层)组成,常用的层如:数据加载层.卷积操作层.pooling层.非线性变换层.内积运算层.归一化层.损失计算层等:本篇主要 ...

  2. 实现strlen,strcpy,strcat,strcmp同功能的函数stringLength,stringCopy,stringCatch,stringCompare

    #import <Foundation/Foundation.h> /* 求字符串长度 */ int stringLength(char arr[]); /* 复制字符串 将arr1 复制 ...

  3. Hansight

    http://www.hansight.com/scenarios.html#account

  4. 练习PYTHON之EVENTLET

    以下是重点,要会运用: eventlet是一个用来处理和网络相关的python库函数,而且可以通过协程来实现并发,在eventlet里,把“协程”叫做 greenthread(绿色线程).所谓并发,就 ...

  5. [Gauss]POJ3185 The Water Bowls

    题意:反正就是要给的一串01的变成全0 能影响自己和左右 最少需要几步 01方程组 异或解 ][]; // 增广矩阵 ]; // 解 ]; // 标记是否为自由未知量 int n; void debu ...

  6. USB (Universal Serial Bus)

    USB歷史簡介 USB規格演變 標準 USB 2.0 介面 實體層 訊號傳輸 傳輸速率 網路層 USB 通訊模型 Endpoints 傳輸型態 USB 資料連結 Transaction Frame P ...

  7. 程序不稳定是因为C++基础不扎实

    最近开发的程序,逻辑上都实现了,但是感觉运行不稳定,程序时不时崩溃(不是逻辑运行不正确),至少找出2个错误: 情况1:char* szRemoteReal = new char[MAX_LENGTH] ...

  8. iOS,Android网络抓包教程之tcpdump

    现在的移动端应用几乎都会通过网络请求来和服务器交互,通过抓包来诊断和网络相关的bug是程序员的重要技能之一.抓包的手段有很多:针对http和https可以使用Charles设置代理来做,对于更广泛的协 ...

  9. WordPress Contact Form 7插件任意文件上传漏洞

    漏洞名称: WordPress Contact Form 7插件任意文件上传漏洞 CNNVD编号: CNNVD-201311-415 发布时间: 2013-11-28 更新时间: 2013-11-28 ...

  10. arch Linux not found device 错误解决

    使用Archlinux LiveCD mount /dev.sda1 /mnt (有boot分区的挂boot) Running mkinitcpio -p linux Running grub-mkc ...