0

我需要将一串完全随机的字符转换成我可以读回的东西!我的想法是:

示例字符串:hi

h (Ascii) -> 68 (hex) 
i (Ascii) -> 69 (hex)

所以转换hi我必须有6869

我的值现在在Base64(我用 a 得到Convert.ToBase64String()),这个“ascii 到 hex”转换正确吗?在 base64 中,我的值类似于 "4kIw0ueWC/+c=" 但我只需要字符,特殊字符会弄乱我的系统

vb.net Convert 只能转换为 base64 字符串 :(

编辑:这是我的最终解决方案:我在enc变量中获取了 base64 字符串,并首先将其转换为 ASCII,然后使用对应的十六进制转换:

Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(enc)
Dim hex As String = BitConverter.ToString(bytes).Replace("-", String.Empty)

之后,我用以下方法扭转了这一点:

Dim b((input.Length \ 2) - 1) As Byte
For i As Int32 = 0 To b.GetUpperBound(0)
     b(i) = Byte.Parse(input.Substring(i * 2, 2), Globalization.NumberStyles.HexNumber)
Next i
Dim enc As New System.Text.ASCIIEncoding()
result = enc.GetString(b)

毕竟这一切我拿回了我的base64string并最后一次转换了Convert.FromBase64String(result)

完毕!感谢您的提示:)

4

1 回答 1

2

首先Byte()从您的base64字符串中获取:

Dim data = Convert.FromBase64String(inputString)

然后使用BitConverter

String hex = BitConverter.ToString(data)
于 2013-03-04T10:45:48.430 回答