Added hexlate() and unhexlate() to StringUtil.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4363 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
* <code>null</code> 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" ];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user