当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 将字节数组转换为十六进制字符串

作者:精品下载站 日期:2024-12-14 22:03:31 浏览:11 分类:玩电脑

PowerShell 将字节数组转换为十六进制字符串


使用 PowerShell 中的 [System.BitConverer] 类的 ToString() 方法将字节数组转换为十六进制字符串。 此方法采用一个字节数组作为输入参数,并获取该字节数组的十六进制表示。

十六进制字符串是十六进制格式的二进制数据的表示。它是一种以 16 为基数的编号系统,使用 0-9 数字和 A-F 字母来表示 0 到 15 的值。

[System.BitConverter] 类提供了将数字类型值与字节数组相互转换以及将字节数组转换为其他数据类型的方法。

在本文中,我们将讨论如何使用 PowerShell 中的 ToString() 方法将字节数组转换为十六进制字符串。

使用 ToString() 方法在 PowerShell 中将字节数组转换为十六进制字符串

使用 PowerShell 中的 [System.BitConverter] 类的 ToString() 方法将字节数组转换为十六进制字符串。

让我们通过一个示例来了解如何创建字节数组并使用它将字节数组转换为十六进制字符串。

# Declare string variable
$str = "ShellGeek"

# converting string to bytes array
$bytesArr = [System.Text.Encoding]::UTF8.GetBytes($str)

# Print the bytes array
$bytesArr

# Convert byte array to hex string
$hex = [System.BitConverter]::ToString($bytesArr)

# Print the hex string
$hex

在上面的 PowerShell 脚本中,$str 变量是一个字符串类型,包含值“ShellGeek”。使用 GetBytes() 方法,它将字符串转换为字节数组,并将结果分配给包含该字节数组的变量 $bytesArr

要将字节数组转换为十六进制字符串,它使用 System.BitConverter 类的 ToString() 方法,该方法将字节数组作为输入参数并返回十六进制值细绳

PowerShell 中上述脚本的输出将字节数组转换为十六进制字符串。

[玩转系统] PowerShell 将字节数组转换为十六进制字符串

酷提示:如何在 PowerShell 中将字符串转换为整数!

在 PowerShell 中将字节数组转换为十六进制字符串

使用提供方法ToString()[System.BitConverter]类将字节数组转换为十六进制字符串。

在以下脚本中,$bytesArr 变量包含字符串“ShellGeek” 的二进制表示形式。

# Create a byte array
$bytesArr = [byte[]]@(83,104,101,108,108,71,101,101,107)         

# Convert byte array to hex string
$hex = [System.BitConverter]::ToString($bytes)       

# Print the string
$hex

在上面的 PowerShell 脚本中,System.BitConverter 类在 PowerShell 中具有 GetString() 方法,用于将字节数组转换为十六进制字符串。此方法将输入参数作为字节数组并返回一个字符串。

脚本的输出将字节数组转换为十六进制字符串。

# Create a byte array
PS C:\> $bytesArr = [byte[]]@(83,104,101,108,108,71,101,101,107)  

# Convert byte array to hex string
PS C:\> $hex = [System.BitConverter]::ToString($bytes) 

# Print the hex string
PS C:\> $hex

PS C:\> ShellGeek

从 PowerShell 版本 7.1 开始,可以使用 [System.Convert] 类的 FromHexString() 方法将十六进制字符串转换为字节数组。

# byte array
$bytesArr = [byte[]]@(83,104,101,108,108,71,101,101,107)

# Convert byte array to hex string
$hex = [System.BitConverter]::ToString($bytes)

# remove - between hex string
$hexString = $hex.Replace('-','')

# convert hex string to byte array using FromHexString method
$bytes = [System.Convert]::FromHexString($hexString)

# Print the bytes
Write-Host $bytes

在上面的 PowerShell 脚本中,创建一个字节数组并使用 System.BitConveter 类的 ToString() 方法将其转换为十六进制字符串。

在 PowerShell 7.1 版及更高版本中,System.Convert 类提供了 FromHexString() 方法,可将十六进制字符串转换为字节数组。

在旧版本的 PowerShell 中,使用以下脚本将十六进制字符串转换为字节数组。

# byte array
$bytesArr = [byte[]]@(83,104,101,108,108,71,101,101,107)

# Convert byte array to hex string
$hex = [System.BitConverter]::ToString($bytes)

# remove - between hex string
$hexString = $hex.Replace('-','')

# Convert hex string to byte array
 [byte[]]@(for ($i = 0; $i -lt $hexString.Length; $i += 2) { [convert]::ToByte($hexString.Substring($i, 2), 16) })    

酷提示:如何在 PowerShell 中将 guid 转换为字符串!

结论

希望上面关于如何在PowerShell中使用UTF8编码格式的GetString()方法将字节数组转换为十六进制字符串的文章对您有所帮助。

将字节数组转换为十六进制字符串对于生成加密密钥或处理二进制数据等任务非常有用。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯