gpt4 book ai didi

vbscript - 使用 VbScript 将十六进制转换为字符串

转载 作者:行者123 更新时间:2023-12-04 13:39:03 30 4
gpt4 key购买 nike

这是一个将字符串转换为十六进制代码的脚本:

strString = "test"
strHex =""
For i=1 To Len(strString)
strHex = strHex & " " & Hex(Asc(Mid(strString,i,1)))
Next

strHex = Right(strHex,Len(strHex)-1)

WScript.Echo strHex

我想做一个将十六进制转换为字符串的反向操作,这可以使用 vbscript 吗?

最佳答案

VBScript 使用“&H”将数字标记为十六进制。所以

>> WScript.Echo Chr("&H" & "41")
>>
A
>>

原则上演示策略。演示代码:

Option Explicit

Function s2a(s)
ReDim a(Len(s) - 1)
Dim i
For i = 0 To UBound(a)
a(i) = Mid(s, i + 1, 1)
Next
s2a = a
End Function

Function s2h(s)
Dim a : a = s2a(s)
Dim i
For i = 0 To UBound(a)
a(i) = Right(00 & Hex(Asc(a(i))), 2)
Next
s2h = Join(a)
End Function

Function h2s(h)
Dim a : a = Split(h)
Dim i
For i = 0 To UBound(a)
a(i) = Chr("&H" & a(i))
Next
h2s = Join(a, "")
End Function

Dim s : s = "test"
WScript.Echo 0, s
WScript.Echo 1, s2h(s)
WScript.Echo 2, h2s(s2h(s))

输出:

0 test
1 74 65 73 74
2 test

更新评论/unicode:

使用AscW/ChrW ( VB ref ) 来处理 UTF 16。

Option Explicit

Function s2a(s)
ReDim a(Len(s) - 1)
Dim i
For i = 0 To UBound(a)
a(i) = Mid(s, i + 1, 1)
Next
s2a = a
End Function

Function s2h(s)
Dim a : a = s2a(s)
Dim i
For i = 0 To UBound(a)
a(i) = Right("0000" & Hex(AscW(a(i))), 4)
Next
s2h = Join(a)
End Function

Function h2s(h)
Dim a : a = Split(h)
Dim i
For i = 0 To UBound(a)
a(i) = ChrW("&H" & a(i))
Next
h2s = Join(a, "")
End Function

Dim s : s = "abcä" & ChrW("&H" & "d98a")
WScript.Echo 0, s
WScript.Echo 1, s2h(s)
WScript.Echo 2, h2s(s2h(s))

关于vbscript - 使用 VbScript 将十六进制转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24342626/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com