PageRenderTime 31ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/util/Hex.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 66 lines | 35 code | 6 blank | 25 comment | 5 complexity | dbe77a3bf04c7fac4d8a493b021e9e21 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * Hex
  3. *
  4. * Utility class to convert Hex strings to ByteArray or String types.
  5. * Copyright (c) 2007 Henri Torgemane
  6. *
  7. * See LICENSE.txt for full license information.
  8. */
  9. package com.hurlant.util
  10. {
  11. import flash.utils.ByteArray;
  12. public class Hex
  13. {
  14. /**
  15. * Support straight hex, or colon-laced hex.
  16. * (that means 23:03:0e:f0, but *NOT* 23:3:e:f0)
  17. * Whitespace characters are ignored.
  18. */
  19. public static function toArray(hex:String):ByteArray {
  20. hex = hex.replace(/\s|:/gm,'');
  21. var a:ByteArray = new ByteArray;
  22. if (hex.length&1==1) hex="0"+hex;
  23. for (var i:uint=0;i<hex.length;i+=2) {
  24. a[i/2] = parseInt(hex.substr(i,2),16);
  25. }
  26. return a;
  27. }
  28. public static function fromArray(array:ByteArray, colons:Boolean=false):String {
  29. var s:String = "";
  30. for (var i:uint=0;i<array.length;i++) {
  31. s+=("0"+array[i].toString(16)).substr(-2,2);
  32. if (colons) {
  33. if (i<array.length-1) s+=":";
  34. }
  35. }
  36. return s;
  37. }
  38. /**
  39. *
  40. * @param hex
  41. * @return a UTF-8 string decoded from hex
  42. *
  43. */
  44. public static function toString(hex:String):String {
  45. var a:ByteArray = toArray(hex);
  46. return a.readUTFBytes(a.length);
  47. }
  48. /**
  49. *
  50. * @param str
  51. * @return a hex string encoded from the UTF-8 string str
  52. *
  53. */
  54. public static function fromString(str:String, colons:Boolean=false):String {
  55. var a:ByteArray = new ByteArray;
  56. a.writeUTFBytes(str);
  57. return fromArray(a, colons);
  58. }
  59. }
  60. }