diff --git a/src/as/com/threerings/presents/client/Communicator.as b/src/as/com/threerings/presents/client/Communicator.as index b27f0b137..94583a9aa 100644 --- a/src/as/com/threerings/presents/client/Communicator.as +++ b/src/as/com/threerings/presents/client/Communicator.as @@ -10,7 +10,7 @@ import flash.net.Socket; import flash.utils.ByteArray; import flash.utils.Endian; -import com.threerings.util.Util; +import com.threerings.util.StringUtil; import com.threerings.io.FrameAvailableEvent; import com.threerings.io.FrameReader; @@ -157,7 +157,7 @@ public class Communicator // write the message (ends up in _outBuffer) _outStream.writeObject(msg); -// Log.debug("outBuffer: " + Util.bytesToString(_outBuffer)); +// Log.debug("outBuffer: " + StringUtil.unhexlate(_outBuffer)); // Frame it by writing the length, then the bytes. // We add 4 to the length, because the length is of the entire frame @@ -199,7 +199,7 @@ public class Communicator // convert the frame data into a message from the server var frameData :ByteArray = event.getFrameData(); //Log.debug("length of in frame: " + frameData.length); - //Log.debug("inBuffer: " + Util.bytesToString(frameData)); + //Log.debug("inBuffer: " + StringUtil.unhexlate(frameData)); _inStream.setSource(frameData); var msg :DownstreamMessage; try { diff --git a/src/as/com/threerings/util/StringUtil.as b/src/as/com/threerings/util/StringUtil.as index 96ef27b66..923337150 100644 --- a/src/as/com/threerings/util/StringUtil.as +++ b/src/as/com/threerings/util/StringUtil.as @@ -1,5 +1,7 @@ package com.threerings.util { +import flash.utils.ByteArray; + import mx.utils.*; public class StringUtil @@ -94,5 +96,49 @@ public class StringUtil return s.substring(0, maxLength - append.length) + append; } } + + /** + * Generates a string from the supplied bytes that is the hex encoded + * representation of those byts. Returns the empty String for a + * null or empty byte array. + */ + public static function hexlate (bytes :ByteArray) :String + { + var str :String = ""; + if (bytes != null) { + for (var ii :int = 0; ii < bytes.length; ii++) { + var b :int = bytes[ii]; + str += HEX[b >> 4] + HEX[b & 0xF]; + } + } + return str; + } + + /** + * Turn a hexlated String back into a ByteArray. + */ + public static function unhexlate (hex :String) :ByteArray + { + if (hex == null || (hex.length % 2 != 0)) { + return null; + } + + hex = hex.toLowerCase(); + var data :ByteArray = new ByteArray(); + for (var ii :int = 0; ii < hex.length; ii += 2) { + var value :int = HEX.indexOf(hex.charAt(ii)) << 4; + value += HEX.indexOf(hex.charAt(ii + 1)); + + // TODO: verify + // values over 127 are wrapped around, restoring negative bytes + data[ii / 2] = value; + } + + return data; + } + + /** Hexidecimal digits. */ + protected static const HEX :Array = [ "0", "1", "2", "3", "4", + "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" ]; } } diff --git a/src/as/com/threerings/util/Util.as b/src/as/com/threerings/util/Util.as index abdb184fe..b22013c7f 100644 --- a/src/as/com/threerings/util/Util.as +++ b/src/as/com/threerings/util/Util.as @@ -6,25 +6,6 @@ import com.threerings.util.StringBuilder; public class Util { - public static function bytesToString (bytes :ByteArray) :String - { - var buf :StringBuilder = new StringBuilder(); - for (var ii :int = 0; ii < bytes.length; ii++) { - var b :int = bytes[ii]; - buf.append(HEX[b >> 4], HEX[b & 0xF]); - } - return buf.toString(); - } - - public static function toHex (value :uint) :String - { - var buf :StringBuilder = new StringBuilder("0x"); - for (var ii :int = 7; ii >= 0; ii--) { - buf.append(HEX[(value >> (ii * 4)) & 0xF]); - } - return buf.toString(); - } - /** * A nicer test for equals that works if the objects implement Equalable */ @@ -44,8 +25,5 @@ public class Util throw new TypeError("value is not a " + clazz); } } - - private static const HEX :Array = new Array("0", "1", "2", "3", "4", "5", - "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"); } }