Added bytesToString() and stringToBytes().

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4733 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2007-06-14 01:07:04 +00:00
parent 0c448bc92d
commit d59811b8cc
+29
View File
@@ -245,6 +245,35 @@ public class StringUtil
return array;
}
/**
* Turn the specified byte array, containing only ascii characters, into a String.
*/
public static function fromBytes (bytes :ByteArray) :String
{
var s :String = "";
if (bytes != null) {
for (var ii :int = 0; ii < bytes.length; ii++) {
s += String.fromCharCode(bytes[ii]);
}
}
return s;
}
/**
* Turn the specified String, containing only ascii characters, into a ByteArray.
*/
public static function toBytes (s :String) :ByteArray
{
if (s == null) {
return null;
}
var ba :ByteArray = new ByteArray();
for (var ii :int = 0; ii < s.length; ii++) {
ba[ii] = int(s.charCodeAt(ii)) & 0xFF;
}
return ba;
}
/**
* Generates a string from the supplied bytes that is the hex encoded
* representation of those byts. Returns the empty String for a