您的位置:首页 > 编程语言 > Java开发

关于java中byte

2015-12-17 11:04 459 查看
简单介绍:

a,1字节==8比特,在计算机中,一个字节,是用8个‘1’和‘0’比特表示的;

b,byte的数值范围是从-128~127,即“1000 0000” ~ “0111 1111”;

c,正数的比特值(二进制表示)可以用2的多少次幂算出来,但是,负数的不行;

d,负数的二进制表示,是通过,负数的绝对值的二进制取反加一获得,即-10—>10—>0000 1010—>1111 0101 + 0000 0001—>1111 0110(即-10在计算机中存储的形式就是1111 0110);

方法介绍:

以下是一个类,介绍byte数组与java类型直接的转换。

package com.xg.bytest;

import java.nio.charset.Charset;

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");
}

/*
* 将byte[]转化十六进制的字符串
* 使用b[i]&0xFF的原因
* 1,byte的大小为8bits而int的大小为32bits
* 2,如果换成b[i]&0xFFFF,则会造成误差,具体是怎样的误差,还不清楚。
* 0xff默认是整形,但ff只有8位,所以前面的24位全为0,
* 一个byte跟0xff(相当于0x000000ff)相与会先将那个byte转化成整形运算,
* 这样,结果中的高的24个比特就总会被清0,于是结果总是我们想要的。
* 如果是0xffff,则只有16位被清零
*/
public static String bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
// System.out.println(i + "--" + hex);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}

public static void main(String[] args) {
short s = 123;
int i = 123;
long l = 123456789;

char c = 'a';

float f = 123.456f;
double d = 122.22;

System.out.println(bytes2HexString(getBytes("abcdsstring")));
String string = "第一次测试byte";
System.out.println(getString(getBytes(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)));

byte b = -10;
System.out.println("toBinaryString--->" + Integer.toBinaryString(b));
System.out.println("toBinaryString--->" + Integer.toBinaryString(0xffff));
System.out.println("toBinaryString--->" + Integer.toBinaryString(b & 0xff));
System.out.println("toOctalString--->" + Integer.toOctalString(b));
// System.out.println(Integer.parseInt("11110110"));
// System.out.println(Byte.valueOf("11110110", 2));
// System.out.println(Integer.valueOf("11110110", 2));

}
}整型与byte类型之间转换的一些方法介绍
// 十进制转成十六进制:
Integer.toHexString(int i)
// 十进制转成八进制
Integer.toOctalString(int i)
// 十进制转成二进制
Integer.toBinaryString(int i)
// 十六进制转成十进制
Integer.valueOf("FFFF",16).toString()
// 八进制转成十进制
Integer.valueOf("876",8).toString()
// 二进制转十进制
Integer.valueOf("0101",2).toString()

// 有什么方法可以直接将2,8,16进制直接转换为10进制的吗?
java.lang.Integer类
parseInt(String s, int radix)
// 使用第二个参数指定的基数,将字符串参数解析为有符号的整数。
examples from jdk:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648还有些需要琢磨的,现在想不起来,等以后遇到再记录吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: