Java byte数据类型转换

来源:互联网 发布:未来软件怎么样 编辑:程序博客网 时间:2024/05/24 23:12
转载地址:http://www.cnblogs.com/xyqCreator/archive/2012/06/13/2547657.html
public class DataTypeChangeHelper {/** * 将一个单字节的byte转换成32位的int *  * @param b *            byte * @return convert result */public static int unsignedByteToInt(byte b) {return (int) b & 0xFF;}/** * 将一个单字节的Byte转换成十六进制的数 *  * @param b *            byte * @return convert result */public static String byteToHex(byte b) {int i = b & 0xFF;return Integer.toHexString(i);}/** * 将一个4byte的数组转换成32位的int *  * @param buf *            bytes buffer * @param byte[]中开始转换的位置 * @return convert result */public static long unsigned4BytesToInt(byte[] buf, int pos) {int firstByte = 0;int secondByte = 0;int thirdByte = 0;int fourthByte = 0;int index = pos;firstByte = (0x000000FF & ((int) buf[index]));secondByte = (0x000000FF & ((int) buf[index + 1]));thirdByte = (0x000000FF & ((int) buf[index + 2]));fourthByte = (0x000000FF & ((int) buf[index + 3]));index = index + 4;return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;}/** * 将16位的short转换成byte数组 *  * @param s *            short * @return byte[] 长度为2 * */public static byte[] shortToByteArray(short s) {byte[] targets = new byte[2];for (int i = 0; i < 2; i++) {int offset = (targets.length - 1 - i) * 8;targets[i] = (byte) ((s >>> offset) & 0xff);}return targets;}/** * 将32位整数转换成长度为4的byte数组 *  * @param s *            int * @return byte[] * */public static byte[] intToByteArray(int s) {byte[] targets = new byte[2];for (int i = 0; i < 4; i++) {int offset = (targets.length - 1 - i) * 8;targets[i] = (byte) ((s >>> offset) & 0xff);}return targets;}/** * long to byte[] *  * @param s *            long * @return byte[] * */public static byte[] longToByteArray(long s) {byte[] targets = new byte[2];for (int i = 0; i < 8; i++) {int offset = (targets.length - 1 - i) * 8;targets[i] = (byte) ((s >>> offset) & 0xff);}return targets;}/** 32位int转byte[] */public static byte[] int2byte(int res) {byte[] targets = new byte[4];targets[0] = (byte) (res & 0xff);// 最低位targets[1] = (byte) ((res >> 8) & 0xff);// 次低位targets[2] = (byte) ((res >> 16) & 0xff);// 次高位targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。return targets;}/** * 将长度为2的byte数组转换为16位int *  * @param res *            byte[] * @return int * */public static int byte2int(byte[] res) {// res = InversionByte(res);// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或return targets;}}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 杏仁吃多了中毒怎么办 出轨怀孕了该怎么办呢 瑞安医保卡丢了怎么办 包裹一直在揽收怎么办 揽收超时的快件怎么办 快递被别人偷了怎么办 行驶证副本丢了怎么办 眼睛进了石灰粉怎么办 高铁网上没票了怎么办 限行尾号是字母怎么办 手指被刺扎肿了怎么办 手上进了小刺怎么办 招投标标书丢了怎么办 冒险岛2装备红了怎么办 宝宝屁股腌红了怎么办 宝宝肛门红痒怎么办啊 宝宝屁屁溃烂了怎么办 脸过敏起红疙瘩怎么办 一岁宝宝屁股红怎么办 屁眼肉凸出来了怎么办 陶笛声音变闷了怎么办 吃三七粉上火了怎么办 红枣核吞下去了怎么办 话梅核吞下去了怎么办 芒果和海鲜吃了怎么办 小孩咳嗽喉咙有痰怎么办 4岁宝宝喉咙有痰怎么办 20天新生儿有痰怎么办 孩子嗓子老是有痰怎么办 买的哈密瓜不甜怎么办 吉他琴颈变形了怎么办 hcg值长得慢怎么办 蚊子老在耳边叫怎么办 刚买来的鲜海参怎么办 天冷手指关节疼怎么办 未满一年驾龄上高速违章怎么办 榴莲太生剥开了怎么办 榴莲开了没熟怎么办 榴莲打开了没熟怎么办 榴莲开口了没熟怎么办 榴莲没熟打开了怎么办