Utility class to convert Hex strings to ByteArray or String types.
import flash.utils.ByteArray; public class Hex { /** * Support straight hex, or colon-laced hex. * (that means 23:03:0e:f0, but *NOT* 23:3:e:f0) * Whitespace characters are ignored. */ public static function toArray(hex:String):ByteArray { hex = hex.replace(/\s|:/gm,''); var a:ByteArray = new ByteArray; if (hex.length&1==1) hex="0"+hex; for (var i:uint=0;i<hex.length;i+=2) { a[i/2] = parseInt(hex.substr(i,2),16); } return a; } public static function fromArray(array:ByteArray, colons:Boolean=false):String { var s:String = ""; for (var i:uint=0;i<array.length;i++) { s+=("0"+array[i].toString(16)).substr(-2,2); if (colons) { if (i<array.length-1) s+=":"; } } return s; } /** * * @param hex * @return a UTF-8 string decoded from hex * */ public static function toString(hex:String):String { var a:ByteArray = toArray(hex); return a.readUTFBytes(a.length); } /** * * @param str * @return a hex string encoded from the UTF-8 string str * */ public static function fromString(str:String, colons:Boolean=false):String { var a:ByteArray = new ByteArray; a.writeUTFBytes(str); return fromArray(a, colons); } }
內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。
相關文章
- How to Convert a Class File to a Ja
- Python convert string to unicode numberPythonUnicode
- How to Convert Class File to Java File Online?Java
- C# convert sql blob type to plain stringC#SQLAI
- err Invalid input of type: 'dict'. Convert to a byte, string or number first
- bytes and bytearray
- Convert DataFrame string complex i to j python // “Cloning” row or column vectorsPython
- PHP 每日一函式 — 字串函式 convert_cyr_string ()PHP函式字串
- PG: Utility queries
- 【Hex】什麼是英特爾 HEX 檔案格式
- ASM Metadata Dump UtilityASM
- 7.60 CONVERT
- hex檔案分析
- Encode and Decode Strings
- no-strings-attached
- MQTT接收HEX(2/2)MQQT
- 04_python——元組(tuple)、字串 、 bytes bytearrayPython字串
- Python內建資料結構--bytes、bytearrayPython資料結構
- SMART Utility for mac (硬碟檢測)Mac硬碟
- Partition|Disk Utility 如何分割磁碟
- PbootCMS附件上傳失敗報錯UNKNOW: Code: 8192; Desc: stripos(): Non-string needles will be interpreted as strings in the future.boot
- Add Strings 字串相加字串
- strings 和 strconv 包
- Go Standard library - stringsGo
- Python的資料結構(四)bytes和bytearrayPython資料結構
- css12 CSS HEX ColorsCSS
- 硬碟檢測工具:SMART Utility for mac硬碟Mac
- [LeetCode] 205. Isomorphic StringsLeetCode
- PHP 每日一函式 — 字串函式 convert_uuencode () & convert_uudecode ()PHP函式字串
- Python函式每日一講 - hex()Python函式
- 字串函式 hex2bin ()字串函式
- [Linux] convert .img to .iso fileLinux
- DocTo convert doc(x) to pdf
- Convert型別轉換型別
- 聊聊Java 9的Compact StringsJava
- [ABC343G] Compress Strings
- golang中的strings.EqualFoldGolang
- CF482C Game with StringsGAM