4 * Utility class to convert Hex strings to ByteArray or String types.
\r
5 * Copyright (c) 2007 Henri Torgemane
\r
7 * See LICENSE.txt for full license information.
\r
9 package com.hurlant.util
\r
11 import flash.utils.ByteArray;
\r
16 * Support straight hex, or colon-laced hex.
\r
17 * (that means 23:03:0e:f0, but *NOT* 23:3:e:f0)
\r
18 * Whitespace characters are ignored.
\r
20 public static function toArray(hex:String):ByteArray {
\r
21 hex = hex.replace(/\s|:/gm,'');
\r
22 var a:ByteArray = new ByteArray;
\r
23 if (hex.length&1==1) hex="0"+hex;
\r
24 for (var i:uint=0;i<hex.length;i+=2) {
\r
25 a[i/2] = parseInt(hex.substr(i,2),16);
\r
30 public static function fromArray(array:ByteArray, colons:Boolean=false):String {
\r
32 for (var i:uint=0;i<array.length;i++) {
\r
33 s+=("0"+array[i].toString(16)).substr(-2,2);
\r
35 if (i<array.length-1) s+=":";
\r
44 * @return a UTF-8 string decoded from hex
\r
47 public static function toString(hex:String):String {
\r
48 var a:ByteArray = toArray(hex);
\r
49 return a.readUTFBytes(a.length);
\r
56 * @return a hex string encoded from the UTF-8 string str
\r
59 public static function fromString(str:String, colons:Boolean=false):String {
\r
60 var a:ByteArray = new ByteArray;
\r
61 a.writeUTFBytes(str);
\r
62 return fromArray(a, colons);
\r