編輯:關於 Android 開發
在專案開發中,int、String、byte[]、float、double 等基本型別之間的轉換可謂家常便飯。若每次都手寫轉換邏輯,既繁瑣又容易出錯。因此把這些常見操作封裝成一個工具類 BaseTypeConvertUtil,便能隨取隨用、大幅提升了開發效率。
以下為完整的工具類實作,涵蓋了補零、位元組序轉換、字串解析與去除多餘符號等多種常見場景:
package com.sinostride.smartcity.util;
import java.io.UnsupportedEncodingException;
/** * Created by lhd on 2016/10/12. * 基本類型轉換的工具類 */
public class BaseTypeConvertUtil {
/** * int轉String * * @param i 需要轉換的int * @param length 轉換後的String的長度 * @return 轉換後的String */ public static String int2String(int i, int length) { int abs = Math.abs(i); String str = abs + ""; if (str.length() < length && i >= 0) { int digit = (length - str.length()); for (int j = 0; j < digit; j++) { str = "0" + str; } } else if (str.length() < length && i < 0) { int digit = (length - str.length()) - 1; for (int j = 0; j < digit; j++) { str = "0" + str; } str = "-" + str; } return str; }
/** * int轉byte[] * 低位在前,高位在後 */ public static byte[] int2Bytes(int i) { byte[] b = new byte[4]; b[0] = (byte) (i & 0xff); b[1] = (byte) ((i >> 8) & 0xff); b[2] = (byte) ((i >> 16) & 0xff); b[3] = (byte) (i >>> 24); return b; }
/** * byte數組轉int數組,適用於(低位在前,高位在後)的順序 */ public static int[] bytes2Int(byte[] src, int offset) { int[] value = new int[src.length / 4]; for (int i=0;i<src.length/4;i++) { value[i] = (int) ((src[offset+ 4*i] & 0xFF) | ((src[offset + 1+4*i] & 0xFF) << 8) | ((src[offset + 2+4*i] & 0xFF) << 16) | ((src[offset + 3+4*i] & 0xFF) << 24)); } return value; }
public static int bytesToInt(byte[] bytes){ int value=(int) ((bytes[0] & 0xFF) | ((bytes[1] & 0xFF) << 8) | ((bytes[2] & 0xFF) << 16) | ((bytes[3] & 0xFF) << 24)); return value; }
/** * String轉int */ public static int string2Int(String s) { int i = Integer.valueOf(s).intValue(); return i; }
/** *string轉int,可指定開始位置 */ public static int string2Int(String s,int start){ if(start<s.length()) { String value = s.substring(start); int i = Integer.valueOf(value); return i; }else { return 0; } }
/** *string轉int,可指定起止位置 */ public static int string2Int(String s,int start,int end){ if(end<=s.length()&&start<end) { String value = s.substring(start, end); int i = Integer.valueOf(value); return i; }else{ return 0; } }
/** * String轉float */ public static float string2Float(String s) { Float f = Float.valueOf(s); return f; }
/** * String轉float,可指定開始位置 */ public static float string2Float(String s,int start){ if(start<s.length()) { String value = s.substring(start); float f=Float.valueOf(value); return f; }else { return 0; } }
/** * String轉double */ public static double string2Double(String s) { double b = Integer.valueOf(s).doubleValue(); return b; }
/** * string轉byte【】 */ public static byte[] string2Bytes(String src){ byte[] bytes=src.getBytes(); return bytes; }
/** * string轉byte【】,可指定編碼 */ public static byte[] string2Bytes(String src,String flag ) throws UnsupportedEncodingException { byte[] bytes=src.getBytes(flag); return bytes; }
/** * String中去掉空格以及逗號,句號等 */ public static String stringToNoSpaceNoComma(String s) { String dispose1 = s.replaceAll(" +", ""); String dispose2 = dispose1.replaceAll(",+", ""); String dispose3 = dispose2.replaceAll(";+", ""); return dispose3; }
/** * String字符串按照"," 空格"."以及"||"分開 */ public static String[] stringToSpliteString(String s){ String[] strArray=null; strArray=s.split("[, .\\\\]"); return strArray; }
/** * float轉string,並可指定長度補零 */ public static String float2String(float f, int length) { float abs = Math.abs(f); String s = String.valueOf(abs); if (s.length() < length && f > 0) { int digit = (length - s.length()); for (int j = 0; j < digit; j++) { s = "0" + s; } } else if (s.length() < length && f < 0) { int digit = (length - s.length()) - 1; for (int j = 0; j < digit; j++) { s = "0" + s; } s = "-" + s; } return s; }
/** * double轉string,並可指定長度補零 */ public static String double2String(double d, int length) { double abs = Math.abs(d); String s = String.valueOf(abs); if (s.length() < length && d > 0) { int digit = (length - s.length()); for (int j = 0; j < digit; j++) { s = "0" + s; } } else if (s.length() < length && d < 0) { int digit = (length - s.length()) - 1; for (int j = 0; j < digit; j++) { s = "0" + s; } s = "-" + s; } return s; } }這個工具類的設計要點在於:
補零處理:int2String、float2String、double2String 都能將數字轉為指定位長的字串,不足處自動補零,並妥善處理負號;
位元組序轉換:int2Bytes 與 bytes2Int 採用「低位在前、高位在後」的順序進行位運算,常見於與底層裝置或協議通訊的場景;
字串解析:string2Int、string2Float、string2Double 提供可指定起始位置甚至起止範圍的重載版本,方便從較長的字串中擷取片段轉換;
字串清理:stringToNoSpaceNoComma 與 stringToSpliteString 則負責去除多餘符號或按分隔符拆分字串。
把這些高頻操作集中管理,不僅讓程式碼更整潔,也降低了重複出錯的機率,是每個專案都值得具備的基礎工具。
Remote Displayer for Android V1.0:遠端手機畫面顯示工具
Remote Displayer for Android V1.0 功能說明 這款工具主要用於在電腦端遠端查看 Android 裝置畫面,並可進行螢幕擷取。使用時,電腦
Android 開源對話框利器 SweetAlertDialog:精緻動畫與多種提示樣式一次滿足
SweetAlertDialog 是什麼 SweetAlertDialog(sweet-alert-dialog)是一套相當受歡迎的 Android 第三方開源對話框與訊
用 Redhat Crash Utility 從 ramdump 中提取 ftrace log
用 Redhat Crash Utility 從 ramdump 中提取 ftrace log最近準備用 Redhat 的 crash utility 來解析 ramdu
如何寫一個簡易的文件系統(四):實現 umount
哈哈,時隔幾年,又從磁盤深處翻出了當年的原始代碼 myfs.zip。 -----------------------------------------無敵分割線---