使用破折号将十六进制数转换为GUID

时间:2016-04-19 13:51:29

标签: sql excel vba

我有几百个十六进制数字(32个字符长)从sql db中提取。我将它们存储在excel表中,需要将它们转换为带有破折号的GUID。我找到了一个在线转换器,但它一次只能做一个,这将非常耗时(http://www.windowstricks.in/online-windows-guid-converter)。有没有办法,无论是在Excel中使用VBA还是使用公式,还是在SQL中进行转换?它并不像将破折号添加到正确的位置那么简单。我试过了,这不是我需要发生的事情。十六进制和转换后的短划线分离GUID的示例:

六角

  1. 6F414B9DFB178945A3641E40BC2A4AAB
  2. C58C415E215CEC4D9B5100532573D3FA
  3. 2B0BBF00A1403E41A333C805961CEA9F
  4. 从上面的十六进制转换的GUID

    1. 48a6c53b-941C-46e2-9964-680754f71666
    2. ea0ba3f4-4905-4d9c-9d83-76c57bdb060a
    3. 18cea3f7-e1d1-4609-a4bc-9bf6fec6a2d4
    4. 非常感谢您提供的任何帮助。 感谢

3 个答案:

答案 0 :(得分:2)

此函数将十六进制字符串转换为格式化的GUID字符串:

Public Function ConvHexToGuid(hexa As String) As String
    Dim guid As String * 36
    Mid$(guid, 1) = Mid$(hexa, 7, 2)
    Mid$(guid, 3) = Mid$(hexa, 5, 2)
    Mid$(guid, 5) = Mid$(hexa, 3, 2)
    Mid$(guid, 7) = Mid$(hexa, 1, 2)
    Mid$(guid, 9) = "-"
    Mid$(guid, 10) = Mid$(hexa, 11, 2)
    Mid$(guid, 12) = Mid$(hexa, 9, 2)
    Mid$(guid, 14) = "-"
    Mid$(guid, 15) = Mid$(hexa, 15, 2)
    Mid$(guid, 17) = Mid$(hexa, 13, 2)
    Mid$(guid, 19) = "-"
    Mid$(guid, 20) = Mid$(hexa, 17, 4)
    Mid$(guid, 24) = "-"
    Mid$(guid, 25) = Mid$(hexa, 21, 16)
    ConvHexToGuid = guid
End Function

答案 1 :(得分:0)

HEX的GUID转换如下:

{33221100-5544-7766-8899-AABBCCDDEEFF}

{{1}}

答案 2 :(得分:0)

我找到了答案。感谢@florent。我不确定为什么GUID会以不同的方式出现,并且在我第一次运行时仍然有效,但我有一个解决方案。我是用VBA代码完成的:

Sub CreateGUID()
    Dim count, GUIDConverted As String

    count = 2

    Do While Range("F" & count).Value <> ""
        Range("F" & count).Select
        GUIDConverted = Range("F" & count).Value
        GUIDConverted = ConvertHexToGUID(GUIDConverted)
        Range("H" & count).Value = GUIDConverted
        count = count + 1
    Loop
End Sub

Public Function ConvertHexToGUID(hexa As String) As String
    Dim guid As String * 36
    Mid$(guid, 1) = Mid$(hexa, 7, 2)
    Mid$(guid, 3) = Mid$(hexa, 5, 2)
    Mid$(guid, 5) = Mid$(hexa, 3, 2)
    Mid$(guid, 7) = Mid$(hexa, 1, 2)
    Mid$(guid, 9) = "-"
    Mid$(guid, 10) = Mid$(hexa, 11, 2)
    Mid$(guid, 12) = Mid$(hexa, 9, 2)
    Mid$(guid, 14) = "-"
    Mid$(guid, 15) = Mid$(hexa, 15, 2)
    Mid$(guid, 17) = Mid$(hexa, 13, 2)
    Mid$(guid, 19) = "-"
    Mid$(guid, 20) = Mid$(hexa, 17, 4)
    Mid$(guid, 24) = "-"
    Mid$(guid, 25) = Mid$(hexa, 21, 16)
    ConvertHexToGUID = guid
End Function

这适用于我拥有的所有十六进制数字。 感谢所有花时间做出回应的人,在我为此工作时,他们帮了大家。