[分享]Java中Byte与基础类型之间的转换

========================================================

作者:qiujuer

博客:blog.csdn.net/qiujuer

网站:www.qiujuer.net

开源库:github.com/qiujuer/Genius-Android

开源库:github.com/qiujuer/Blink

转载请注明出处:http://blog.csdn.net/qiujuer/article/details/45152189

——学之开源,用于开源;初学者的心态,与君共勉!

========================================================

很久没有写博客了,有些生疏了;一直忙着做 Blink 框架去了。

在这里打个广告,Blink是一款Socket协议跨平台框架,现在提供有Java、Android、C#的版本;通过Blink能实现对Socket传输的异步封装,能直接发送或接受数据,现能直接发送 byte ,String,File 等类型,可在不改动框架情况下直接扩展所需类型。

Blink开源框架链接

因为在Socket的传输中都是 Bytes 的传输,所以涉及到 Java 基本类型(char、short、int、long)与byte[] 的转化,自己总结了一下,简单的写了一个类。

/**
 * Bit Converter
 */
public class BitConverter {

    /**
     * Convert char to byte[]
     *
     * @param x char
     * @return bytes
     */
    public static byte[] toBytes(char x) {
        return toBytes(x, new byte[2], 0);
    }

    /**
     * Convert char to byte[]
     *
     * @param x       char
     * @param bytes   Dest bytes
     * @param bytePos Dest pos
     * @return bytes
     */
    public static byte[] toBytes(char x, byte[] bytes, int bytePos) {
        bytes[bytePos++] = (byte) (x);
        bytes[bytePos] = (byte) (x >> 8);
        return bytes;
    }

    /**
     * Convert short to byte[]
     *
     * @param x Short
     * @return bytes
     */
    public static byte[] toBytes(short x) {
        return toBytes(x, new byte[2], 0);
    }

    /**
     * Convert short to byte[]
     *
     * @param x       Short
     * @param bytes   Dest bytes
     * @param bytePos Dest pos
     * @return bytes
     */
    public static byte[] toBytes(short x, byte[] bytes, int bytePos) {
        bytes[bytePos++] = (byte) (x);
        bytes[bytePos] = (byte) (x >> 8);
        return bytes;
    }

    /**
     * Convert int to byte[]
     *
     * @param x int
     * @return bytes
     */
    public static byte[] toBytes(int x) {
        return toBytes(x, new byte[4], 0);
    }

    /**
     * Convert int to byte[]
     *
     * @param x       int
     * @param bytes   Dest bytes
     * @param bytePos Dest pos
     * @return bytes
     */
    public static byte[] toBytes(int x, byte[] bytes, int bytePos) {
        bytes[bytePos++] = (byte) (x);
        bytes[bytePos++] = (byte) (x >> 8);
        bytes[bytePos++] = (byte) (x >> 16);
        bytes[bytePos] = (byte) (x >> 24);
        return bytes;
    }

    /**
     * Convert long to byte[]
     *
     * @param x long
     * @return bytes
     */
    public static byte[] toBytes(long x) {
        return toBytes(x, new byte[8], 0);
    }

    /**
     * Convert long to byte[]
     *
     * @param x       long
     * @param bytes   Dest bytes
     * @param bytePos Dest pos
     * @return bytes
     */
    public static byte[] toBytes(long x, byte[] bytes, int bytePos) {
        bytes[bytePos++] = (byte) (x);
        bytes[bytePos++] = (byte) (x >> 8);
        bytes[bytePos++] = (byte) (x >> 16);
        bytes[bytePos++] = (byte) (x >> 24);
        bytes[bytePos++] = (byte) (x >> 32);
        bytes[bytePos++] = (byte) (x >> 40);
        bytes[bytePos++] = (byte) (x >> 48);
        bytes[bytePos] = (byte) (x >> 56);
        return bytes;
    }

    /**
     * Convert byte[] to char
     *
     * @param bytes bytes
     * @return char
     */
    public static char toChar(byte[] bytes) {
        return toChar(bytes, 0);
    }

    /**
     * Convert byte[] to char
     *
     * @param bytes bytes
     * @param index byte start index
     * @return char
     */
    public static char toChar(byte[] bytes, int index) {
        return (char) ((bytes[index + 1] << 8) | (bytes[index] & 0xff));
    }

    /**
     * Convert byte[] to short
     *
     * @param bytes bytes
     * @return short
     */
    public static short toShort(byte[] bytes) {
        return toShort(bytes, 0);
    }

    /**
     * Convert byte[] to short
     *
     * @param bytes bytes
     * @param index byte start index
     * @return short
     */
    public static short toShort(byte[] bytes, int index) {
        return (short) ((bytes[index + 1] << 8) | (bytes[index] & 0xff));
    }

    /**
     * Convert byte[] to int
     *
     * @param bytes bytes
     * @return int
     */
    public static int toInt(byte[] bytes) {
        return toInt(bytes, 0);
    }

    /**
     * Convert byte[] to int
     *
     * @param bytes bytes
     * @param index bytes start index
     * @return int
     */
    public static int toInt(byte[] bytes, int index) {
        return (((bytes[index + 3]) << 24) |
                ((bytes[index + 2] & 0xff) << 16) |
                ((bytes[index + 1] & 0xff) << 8) |
                ((bytes[index] & 0xff)));
    }

    /**
     * Convert byte[] to long
     *
     * @param bytes bytes
     * @return long
     */
    public static long toLong(byte[] bytes) {
        return toLong(bytes, 0);
    }

    /**
     * Convert byte[] to long
     *
     * @param bytes bytes
     * @param index bytes start index
     * @return long
     */
    public static long toLong(byte[] bytes, int index) {
        return ((((long) bytes[index + 7]) << 56) |
                (((long) bytes[index + 6] & 0xff) << 48) |
                (((long) bytes[index + 5] & 0xff) << 40) |
                (((long) bytes[index + 4] & 0xff) << 32) |
                (((long) bytes[index + 3] & 0xff) << 24) |
                (((long) bytes[index + 2] & 0xff) << 16) |
                (((long) bytes[index + 1] & 0xff) << 8) |
                (((long) bytes[index] & 0xff)));
    }
}

需要注意的是:无论是从基本类型转换为 byte[] 还是 byte[] 转换为基本类型, 都是采用的低位在前高位在后的形式;这个与C#的默认模式是一致的,如果你的需求是高位在前的情况则需要自己更改一下顺序了。

========================================================

作者:qiujuer

博客:blog.csdn.net/qiujuer

网站:www.qiujuer.net

开源库:github.com/qiujuer/Genius-Android

开源库:github.com/qiujuer/Blink

转载请注明出处:http://blog.csdn.net/qiujuer/article/details/45152189

——学之开源,用于开源;初学者的心态,与君共勉!

========================================================

时间: 2024-08-19 07:06:17

[分享]Java中Byte与基础类型之间的转换的相关文章

Java中的日期各种类型之间的相互转换

1.字符串格式转日期格式 1 //定义时间格式 2 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 3 String dateStr = "2018-08-23 15:39"; 4 Date date = sdf.parse(dateStr); 注意:在字符串转日期格式时,传的参数内容必须多余时间模板内容,比如模板精确到分,传参必须也是到分或者到秒,否则会报以下异常. java.tex

关于Java中byte,short,char,int 之间相互赋值的问题

首先明确这几种数据类的取值范围: byte:  -128~127 short: -2^15~2^15-1 char: 0~65536 int: -2^31~2^31-1 请看以下代码: byte b = 100; short s = b; //正确,因为byte的取值范围在short取值范围之内. char c = b; //错误,因为byte的取值范围不完全在char的取值范围内. c = s;  //错误,因为short的取值范围不完全在char的取值范围内. int x = b; //正确

java中string与date格式之间的转换

1.string格式转化为Date对象: //把string转化为dateDateFormat fmt =new SimpleDateFormat("yyyy-MM-dd"); Date date = fmt.parse(szBeginTime); test.setStartTime(date); 注意:引入的是:java.text.DateFormat 2.Date格式转化为String对象: SimpleDateFormat sdf = new SimpleDateFormat(&

【java】java 中 byte[]、File、InputStream 互相转换

========================================================================= 使用过程中,一定要注意close()掉各个读写流!!!!! ========================================================================= 1.将File.FileInputStream 转换为byte数组: [new File(参数) 参数可以写绝对路径,也可以如下,写一个文件名,

java 中 byte[]、File、InputStream 互相转换

1.将File.FileInputStream 转换为byte数组: File file = new File("test.txt"); InputStream input = new FileInputStream(file); byte[] byt = new byte[input.available()]; input.read(byt); 2.将byte数组转换为InputStream: byte[] byt = new byte[1024]; InputStream inpu

java中的list,set,数组之间的转换

使用该工具类import org.apache.commons.collections.CollectionUtils; 在Apache Jakarta Commons Collections中 String[] strArray = { "aaa", "bbb", "ccc", "bbb" };        List<String> strList = new ArrayList<String>()

java中Integer 与 String 类型的 相互 转换

Integer 转 String 第一种方法: Integer  i =4; String  s =" "; String num = i+s; 第二种方法: String num =String.valueOf(i); String 转 Integer String ss=""; Integer num =Integer.parseInt(ss); Integer num =Integer.valueOf(ss).intValue();

【转】java中byte数组与int类型的转换(两种方式)----不错

原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送.者接收的数据都是 byte数组,但是int类型是4个byte组成的,如何把一个整形int转换成byte数组,同时如何把一个长度为4的byte数组转换为int类型.下面有两种方式. 第一种方法: public static byte[] int2byte(int

java中byte数组与int类型的转换(两种方式)

java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送.者接收的数据都是 byte数组,但是int类型是4个byte组成的,如何把一个整形int转换成byte数组,同时如何把一个长度为4的byte数组转换为int类型.下面有两种方式. 方法一 /** * int到byte[] * @param i * @return */ public static byte[] intToByteArray(int i) { byte[] resu