For example, is there any methods existing in ActionScript that can convert 0x4e544c4d
into ASCII string "NTLM"?
Asked
Active
Viewed 682 times
2

Roman Marusyk
- 23,328
- 24
- 73
- 116

Xueyang Zhao
- 31
- 9
2 Answers
1
You can try like this:
function HexToASCII(s:String):String {
var hexChar:String;
var finalString:String = "";
for (var i = 0; i < s.length/2; i++) {
hexChar = s.charAt(i*2).toString()+s.charAt((i*2)+1).toString();
hexChar = "0x"+hexChar;
finalString = finalString+String.fromCharCode(parseInt(hexChar));
}
return finalString;
}

Rahul Tripathi
- 168,305
- 31
- 280
- 331
-
All right, in fact I just want to know if there is a method existing in actionscript that can do it. – Xueyang Zhao Sep 23 '15 at 00:59
0
I found "a native way" to achive what you asked for: a combination of the methods writeInt
/writeByte
and toString
of flash.utils.ByteArray:
import flash.utils.ByteArray;
var hex:String = '0x4e544c4d';
var hexNum:int = parseInt(hex);
var bytes:ByteArray = new ByteArray();
bytes.writeInt(hexNum);
trace(bytes.toString()); //prints NTLM
Of course you need to take care of the maximum value parseInt
returns (Number
can only contain 53 bits.), and how that matches when writing to the ByteArray
. I made a small function to handle longer hex strings:
import flash.utils.ByteArray;
var hex:String = '0x4e544c4d204e544c4d204e544c4d';
trace(hexToAscii(hex));//NTLM NTLM NTLM
function hexToAscii(hex:String):String {
if (hex.indexOf('0x') === 0){
hex = hex.substr(2);
}
var bytes:ByteArray = new ByteArray();
while (hex.length > 1){
bytes.writeByte( parseInt(hex.substr(0,2), 16) );
hex = hex.substr(2);
}
return bytes.toString();
}
You can play with it: http://wonderfl.net/c/IUn2

karfau
- 638
- 4
- 17
-
um, it seems to be complicated, I would prefer to write a function directly. – Xueyang Zhao Sep 28 '15 at 01:03
-
um, sure, you can always write a function directly. actually I also wwrote a function called `hexToAscii`, it starts at line 6 of the second snippet and it encapsulates the native, very low level support that exists in ActionScript3 for converting hex numbers/string to an ASCII string. And I understood that you asked for something like this. But maybe I misunderstood you. And of couse it depends on the ceomplexity of your use case. when you know you only have values in range of Number, just use something like the first snippets. Anyway: I still think I gave a valid answer to your question :) – karfau Sep 28 '15 at 19:52