在PowerShell中将十六进制转换为ASCII [英] Convert Hex to ASCII in PowerShell
问题描述
我有一系列的十六进制值,如下所示:
I have a series of hex values which looks like this:
68 65 6c 6c 6f 57 6f 72 6c 64 7c 31 2f 30 38 31 35 7c 41 42 43 2d 31 35 02 08
我现在需要将此转换十六进制值转换为ASCII,结果如下所示:
I now need to convert this hex value to ASCII so that the result looks like:
helloWorld|1/0815|ABC-15
我尝试了很多东西,但从来没有找到最终的代码。我试图用各种可以想象的方式使用convert函数,但没有取得任何成功。
I tried so many things but I never came to the final code. I tried to use the convert-function in every imaginable way without any success.
目前我使用本网站进行转换,但我需要在PowerShell脚本中执行此操作。
At the moment I use this website to convert, but I need to do this in my PowerShell script.
$hexString = "68 65 6c 6c 6f 57 6f 72 6c 64 7c 31 2f 30 38 31 35 7c 41 42 43 2d 31 35 02 08"
我们有字符串,现在我们可以使用空格分开并分别获取每个值。这些值可以转换为int16,这是字符的ASCII代码表示形式。
We have the string, now we can use the spaces to split and get each value separately. These values can be converted to an int16, which is a ascii code representation for the character
$hexString.Split(" ") | forEach {[char]([convert]::toint16($_,16))}
唯一的问题是它返回一个单个字符的数组。我们可以迭代并连接成一个字符串。
The only problem is that it returns an array of single characters. Which we can iterate through and concatenate into a string
$hexString.Split(" ") | forEach {[char]([convert]::toint16($_,16))} | forEach {$result = $result + $_}
$result
这篇关于在PowerShell中将十六进制转换为ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!