BitConvertor字节转换类JAVA版本

在做网络编程中,经常涉及到字节处理,C#中有个非常好用的工具类BitConvertor,可以从字节流中转换各种类型的数据,我觉得很好用,正好java项目中用到类似功能,就自己封装了一个,关于char类型各个语言的处理不太一样,所以这里会有些差异,至于int32,int64,这些整形数各个语言的表示基本都一样。

闲话就到这里,上代码:

package com.itsub.util;
/**
 * 位处理,字节处理
 * @author Master.Erik Date:2014年12月15日
 */
public class BitConverter {
     
    public static short ToInt16(byte[] bytes, int offset) {
        short result = (short) ((int)bytes[offset]&0xff);
        result |= ((int)bytes[offset+1]&0xff) << 8;
        return (short) (result & 0xffff);
    }
    public static int ToUInt16(byte[] bytes, int offset) {
        int result = (int)bytes[offset+1]&0xff;
        result |= ((int)bytes[offset]&0xff) << 8;
        return result & 0xffff;
    }
    public static int ToInt32(byte[] bytes, int offset) {
        int result = (int)bytes[offset]&0xff;
        result |= ((int)bytes[offset+1]&0xff) << 8;
        result |= ((int)bytes[offset+2]&0xff) << 16;
        result |= ((int)bytes[offset+3]&0xff) << 24;
        return result;
    }
    public static long ToUInt32(byte[] bytes, int offset) {
        long result = (int)bytes[offset]&0xff;
        result |= ((int)bytes[offset+1]&0xff) << 8;
        result |= ((int)bytes[offset+2]&0xff) << 16;
        result |= ((int)bytes[offset+3]&0xff) << 24;
        return result & 0xFFFFFFFFL;
    }
    public static long ToInt64(byte[] buffer,int offset) {
        long values = 0;
        for (int i = 0; i < 8; i++) {
            values <<= 8; values |= (buffer[offset+i] & 0xFF);
        }
        return values;
    }
    public static long ToUInt64(byte[] bytes, int offset) {
        long result = 0;
        for (int i = 0; i <= 56; i += 8) {
            result |= ((int)bytes[offset++]&0xff) << i;
        }
        return result;
    }
    public static float ToFloat(byte[] bs, int index) {                
        return Float.intBitsToFloat(ToInt32(bs, index));                  
    }
    public static double ToDouble(byte[] arr,int offset) {
        return Double.longBitsToDouble(ToUInt64(arr, offset));
    }
    public static boolean ToBoolean(byte[] bytes,int offset) {
        return (bytes[offset]==0x00)? false:true;
    }
 
    public static byte[] GetBytes(short value) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (value & 0xff);
        bytes[1] = (byte) ((value & 0xff00) >> 8);
        return bytes;
    }
    public static byte[] GetBytes(int value) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) ((value)&0xFF); //最低位
        bytes[1] = (byte) ((value >> 8)&0xFF);
        bytes[2] = (byte) ((value >> 16)&0xFF);
        bytes[3] = (byte) ((value >>> 24)); //最高位,无符号右移
        return bytes;
    }
    public static byte[] GetBytes(long values) {
        byte[] buffer = new byte[8];
        for (int i = 0; i < 8; i++) {
            int offset = 64 - (i + 1) * 8;
            buffer[i] = (byte)((values >> offset) & 0xff);
        }
        return buffer;
    }
    public static byte[] GetBytes(float value) {
        return GetBytes(Float.floatToIntBits(value));
    }
    public static byte[] GetBytes(double val) {
        long value = Double.doubleToLongBits(val);
        return GetBytes(value);
    }
    public static byte[] GetBytes(boolean value) {
        return new byte[]{(byte)(value? 1:0)};
    }
     
    public static byte IntToByte(int x) {
        return (byte) x;
    }
    public static int ByteToInt(byte b) {
        return b & 0xFF;
    }
    public static char ToChar(byte[] bs,int offset) {
        return (char) (((bs[offset] & 0xFF) << 8) | (bs[offset+1] & 0xFF));
    }
    public static byte[] GetBytes(char value) {
        byte[] b = new byte[2];
        b[0] = (byte) ((value & 0xFF00) >> 8);
        b[1] = (byte) (value & 0xFF);
        return b;
    }
 
    public static byte[] Concat(byte[]... bs) {
        int len = 0,idx=0;
        for(byte[] b:bs)len+=b.length;
        byte[] buffer = new byte[len];
        for(byte[] b:bs) {
            System.arraycopy(b,0, buffer, idx, b.length);
            idx+=b.length;
        }
        return buffer;
    }
    public static void main(String[] args) {
        long a = 123456;
        byte[] b1=GetBytes(a);
        long b= ToInt64(b1, 0);
        System.out.println(b);
    }
}

与之对应的C++版本已经丢了,代码找不到,如果需要就自己写吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程大玩家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值