編輯:關於 Android 程式設計
做過底層通信、音頻處理或者文件解析的開發者,大概都遇到過這類需求:把整數按字節拆開、或者把一段字節流還原成數值。麻煩的地方在於大小端(Big Endian / Little Endian)必須和對端約定一致,否則收到的數字會莫名其妙地變樣。
下面這個工具類把常見的單值轉換、數組批量轉換都封裝好了,並且會自動判斷當前 CPU 的字節序,平時直接調用無參版本即可。
import java.nio.ByteOrder;
public class BytesTransUtils {
private static BytesTransUtils instance = null;
private BytesTransUtils() {
}
public static BytesTransUtils getInstance() {
if (instance == null) {
instance = new BytesTransUtils();
}
return instance;
}
/** 判斷當前 CPU 是否為大端序 */
public boolean testCPU() {
return ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
}
/* ---------------- 數值轉字節 ---------------- */
public byte[] getBytes(short s, boolean bBigEnding) {
byte[] buf = new byte[2];
if (bBigEnding) {
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x00ff);
s >>= 8;
}
} else {
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x00ff);
s >>= 8;
}
}
return buf;
}
public byte[] getBytes(int s, boolean bBigEnding) {
byte[] buf = new byte[4];
if (bBigEnding) {
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
} else {
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
}
return buf;
}
public byte[] getBytes(long s, boolean bBigEnding) {
byte[] buf = new byte[8];
if (bBigEnding) {
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;
}
} else {
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;
}
}
return buf;
}
/* ---------------- 字節轉數值 ---------------- */
public short getShort(byte[] buf, boolean bBigEnding) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 2) {
throw new IllegalArgumentException("byte array size > 2 !");
}
short r = 0;
if (bBigEnding) {
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
} else {
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
}
return r;
}
public int getInt(byte[] buf, boolean bBigEnding) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 4) {
throw new IllegalArgumentException("byte array size > 4 !");
}
int r = 0;
if (bBigEnding) {
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
} else {
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
}
return r;
}
public long getLong(byte[] buf, boolean bBigEnding) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 8) {
throw new IllegalArgumentException("byte array size > 8 !");
}
long r = 0;
if (bBigEnding) {
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
} else {
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
}
return r;
}
/* -------- 對轉換做一層簡化封裝:自動使用本機字節序 -------- */
public byte[] getBytes(int i) {
return getBytes(i, testCPU());
}
public byte[] getBytes(short s) {
return getBytes(s, testCPU());
}
public byte[] getBytes(long l) {
return getBytes(l, testCPU());
}
public int getInt(byte[] buf) {
return getInt(buf, testCPU());
}
public short getShort(byte[] buf) {
return getShort(buf, testCPU());
}
public long getLong(byte[] buf) {
return getLong(buf, testCPU());
}
/* ---------------- 數組批量轉換 ---------------- */
public short[] Bytes2Shorts(byte[] buf) {
byte bLength = 2;
short[] s = new short[buf.length / bLength];
for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = new byte[bLength];
for (int jLoop = 0; jLoop < bLength; jLoop++) {
temp[jLoop] = buf[iLoop * bLength + jLoop];
}
s[iLoop] = getShort(temp);
}
return s;
}
public byte[] Shorts2Bytes(short[] s) {
byte bLength = 2;
byte[] buf = new byte[s.length * bLength];
for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = getBytes(s[iLoop]);
for (int jLoop = 0; jLoop < bLength; jLoop++) {
buf[iLoop * bLength + jLoop] = temp[jLoop];
}
}
return buf;
}
public int[] Bytes2Ints(byte[] buf) {
byte bLength = 4;
int[] s = new int[buf.length / bLength];
for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = new byte[bLength];
for (int jLoop = 0; jLoop < bLength; jLoop++) {
temp[jLoop] = buf[iLoop * bLength + jLoop];
}
s[iLoop] = getInt(temp);
}
return s;
}
public byte[] Ints2Bytes(int[] s) {
byte bLength = 4;
byte[] buf = new byte[s.length * bLength];
for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = getBytes(s[iLoop]);
for (int jLoop = 0; jLoop < bLength; jLoop++) {
buf[iLoop * bLength + jLoop] = temp[jLoop];
}
}
return buf;
}
public long[] Bytes2Longs(byte[] buf) {
byte bLength = 8;
long[] s = new long[buf.length / bLength];
for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = new byte[bLength];
for (int jLoop = 0; jLoop < bLength; jLoop++) {
temp[jLoop] = buf[iLoop * bLength + jLoop];
}
s[iLoop] = getLong(temp);
}
return s;
}
public byte[] Longs2Bytes(long[] s) {
byte bLength = 8;
byte[] buf = new byte[s.length * bLength];
for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = getBytes(s[iLoop]);
for (int jLoop = 0; jLoop < bLength; jLoop++) {
buf[iLoop * bLength + jLoop] = temp[jLoop];
}
}
return buf;
}
}
單值場景直接調用無參數的字節序版本,例如 getInstance().getInt(bytes),內部會依當前平台自動選擇端序;如果協議明確要求固定端序,則調用帶 boolean 參數的重載,並顯式傳入 true(大端)或 false(小端)。批量轉換的幾個方法則按固定長度切片:short 佔 2 字節、int 佔 4 字節、long 佔 8 字節,因此傳入的字節數必須能被對應長度整除,否則尾部多餘的字節會被靜默丟棄。
實際項目中,如果追求性能,也可以考慮用 java.nio.ByteBuffer 配合 order() 設置字節序,代碼量更少也更不容易出錯;上面這份實現的優勢則是邏輯一目了然,便於按需定製。
Android 網路開發入門:WebView、HttpURLConnection 與 HttpClient
這一章的目標很明確:讓 App 能透過 HTTP 與伺服器對話,並把回傳的資料解析成可用的內容。這是 Android 裡最基礎也最常用的一塊網路能力,我們從 WebVie
Android 跨進程通訊解析(二):ContentProvider 實作與特性
一、ContentProvider 簡介 在 Android 中,有些資料(如通訊錄、音訊、視訊檔等)需要供多個應用程式共用。為了更好地對外提供資料,系統提供了 Cont
深入 Android Scroller 與 scrollTo:滑屏效果的原理與實作全解
本文從最基礎的座標概念出發,釐清 scrollTo() 與 scrollBy() 的差異,再帶入 Scroller 與 computeScroll() 的搭配用法,最後組
Android 裡那些容易被忽略的 UI 細節:從游標、滾動條到點九圖
做 Android 專案久了會發現,真正拉開產品質感差距的往往不是什麼高階架構,而是一堆不起眼的細節:輸入框游標的顏色、選字時那個小水滴手柄、滾動條的粗細與位置、滑到底部