java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法
更新時(shí)間:2016年10月08日 18:23:21 投稿:jingxian
下面小編就為大家?guī)?lái)一篇java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
實(shí)例如下:
public class DataTypeChangeHelper { /** * 將一個(gè)單字節(jié)的byte轉(zhuǎn)換成32位的int * * @param b * byte * @return convert result */ public static int unsignedByteToInt(byte b) { return (int) b & 0xFF; } /** * 將一個(gè)單字節(jié)的Byte轉(zhuǎn)換成十六進(jìn)制的數(shù) * * @param b * byte * @return convert result */ public static String byteToHex(byte b) { int i = b & 0xFF; return Integer.toHexString(i); } /** * 將一個(gè)4byte的數(shù)組轉(zhuǎn)換成32位的int * * @param buf * bytes buffer * @param byte[]中開(kāi)始轉(zhuǎn)換的位置 * @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轉(zhuǎn)換成byte數(shù)組 * * @param s * short * @return byte[] 長(zhǎng)度為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位整數(shù)轉(zhuǎn)換成長(zhǎng)度為4的byte數(shù)組 * * @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轉(zhuǎn)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);// 最高位,無(wú)符號(hào)右移。 return targets; } /** * 將長(zhǎng)度為2的byte數(shù)組轉(zhuǎn)換為16位int * * @param res * byte[] * @return int * */ public static int byte2int(byte[] res) { // res = InversionByte(res); // 一個(gè)byte數(shù)據(jù)左移24位變成0x??000000,再右移8位變成0x00??0000 int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或 return targets; } }
以上就是小編為大家?guī)?lái)的java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法全部?jī)?nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
利用keytools為tomcat 7配置ssl雙向認(rèn)證的方法
雙向認(rèn)證和單向認(rèn)證原理基本差不多,只是除了客戶端需要認(rèn)證服務(wù)端以外,增加了服務(wù)端對(duì)客戶端的認(rèn)證,下面這篇文章主要介紹了利用keytools為tomcat 7配置ssl雙向認(rèn)證的方法,需要的朋友可以借鑒,下面來(lái)一起看看吧。2017-02-02Jenkins源代碼管理SVN實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Jenkins源代碼管理SVN實(shí)現(xiàn)步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09掌握模塊化開(kāi)發(fā)Spring Boot子模塊使用技巧
這篇文章主要為大家介紹了掌握模塊化開(kāi)發(fā)Spring Boot子模塊使用技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06springboot配置ssl后啟動(dòng)一直是端口被占用的解決
這篇文章主要介紹了springboot配置ssl后啟動(dòng)一直是端口被占用的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08