0

I need to convert a UID like 'ff5ac81c-fc51-9442-b993-60cff48c6b39' to a Hex-String in Flex. In C# I can use the format parameter like this:

public static string GuidToHex(Guid guid) {
    return ByteToHex(guid.ToByteArray());
}

public static string ByteToHex(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.Length * 2);
    foreach (byte b in bytes) {
        sb.AppendFormat("{0:x2}", b);
    }
    return sb.ToString().ToUpper();
}

How can I do this in action script?

LKa
  • 11
  • 4
  • Are you asking us to convert that code for you? Or do want us to describe the algorithm to you? Or something else? – JeffryHouser Aug 27 '12 at 14:21
  • First to convert the code. If I know what happens internally in the appendFormat-Method with the parameter "{0:x2}" - even better – LKa Aug 27 '12 at 15:12
  • I don't know what happens either; but I'm not sure why you can't Google it and figure it out what the method does. That is what I'd have to do. – JeffryHouser Aug 27 '12 at 15:27
  • I did for several hours... I didn't find anything – LKa Aug 27 '12 at 15:29
  • Here are the docs on StringBuilder.AppendFormat: http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.appendformat%28v=vs.71%29.aspx . It should give you a jumping off point. – JeffryHouser Aug 27 '12 at 15:50

1 Answers1

0

In the MSDN I couldn't find anything, what would help me. On a other page I found an example where a bytearray was converted into a HEX-string. I had to modify it a little, but now it works:

import flash.utils.ByteArray;

import mx.collections.ArrayCollection;
import mx.utils.UIDUtil;

public class RawTypeUtil
{

    private static const hexValues:Array = [ '0', '1', '2', '3', '4', '5', '6', '7',
                                             '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ];

    private static const convertSequence:ArrayCollection = new ArrayCollection([ 3, 2,
                                                             1, 0, 5, 4, 7, 6, 8, 9,
                                                             10, 11, 12, 13, 14, 15 ]);

    public function RawTypeUtil(lock:Class)
    {
        if (lock != RawTypeUtilLock)
        {
            throw new Error("Invalid RawTypeUtil access.");
        }
    }

    public static function guidToHex(guid:String) : String
    {
        // the UIDUtil expects an uppercase uid...
        guid = guid.toUpperCase();
        if (!UIDUtil.isUID(guid))
        {
            return null;
        }

        var bytes:ByteArray = UIDUtil.toByteArray(guid);
        var hex:String = "";

        var byteArray:ArrayCollection = new ArrayCollection();
        for (var i:uint = 0; i < bytes.length; i++)
        {
            var byte:uint = bytes.readUnsignedByte();
            byteArray.addItem(byte);
        }

        for (var j:uint = 0; j < convertSequence.length; j++)
        {
            var index:uint = convertSequence.getItemAt(j) as uint;
            var value:uint = byteArray.getItemAt(index) as uint;
            var l:int = value / 16;
            var r:int = value % 16;
            hex += hexValues[l] + hexValues[r];
        }

        return hex;
    }
}

I don't know why I have to change the order of the first 4 bytes, 5th and 6th, and 7th and 8th that the ByteArray is converted in a correct format. I hope somebody can explain that to me.

LKa
  • 11
  • 4