【问题标题】:Adobe Flex: How to convert UID to a hex string?Adobe Flex:如何将 UID 转换为十六进制字符串?
【发布时间】:2012-08-27 13:54:34
【问题描述】:

我需要在 Flex 中将像“ff5ac81c-fc51-9442-b993-60cff48c6b39”这样的 UID 转换为十六进制字符串。 在 C# 中,我可以像这样使用格式参数:

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();
}

如何在动作脚本中做到这一点?

【问题讨论】:

  • 您是否要求我们为您转换该代码?还是希望我们向您描述算法?还是别的什么?
  • 先转换代码。如果我知道带有参数“{0:x2}”的 appendFormat-Method 内部发生了什么 - 更好
  • 我也不知道会发生什么;但我不确定为什么你不能谷歌它并弄清楚该方法的作用。这就是我必须做的。
  • 我做了几个小时......我没有找到任何东西
  • 这里是 StringBuilder.AppendFormat 上的文档:msdn.microsoft.com/en-us/library/…。它应该给你一个起点。

标签: apache-flex actionscript hex guid uid


【解决方案1】:

在 MSDN 中我找不到任何东西,有什么可以帮助我的。在另一页上,我发现了一个将字节数组转换为十六进制字符串的示例。我不得不对其进行一些修改,但现在它可以工作了:

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;
    }
}

我不知道为什么我必须更改 ByteArray 转换为正确格式的前 4 个字节、第 5 和第 6 以及第 7 和第 8 个字节的顺序。我希望有人能给我解释一下。

【讨论】:

    猜你喜欢
    • 2013-02-07
    • 2017-08-23
    • 2014-03-19
    • 2011-08-25
    • 2014-04-28
    • 2018-01-31
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多