在Powershell中是否可以将字节数组转换为8位有符号整数数组? [英] Is it possible to convert a byte array to a 8-bit signed integer array in Powershell?

查看:82
本文介绍了在Powershell中是否可以将字节数组转换为8位有符号整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Powershell中将十六进制字符串转换为8位带符号整数数组。

I am trying to convert a Hex string to an 8-bit signed integer array in Powershell.

我正在使用以下函数将十六进制字符串(例如A591BF86E5D7D9837EE7ACC569C4B59B)转换为字节数组,然后需要将其转换为8位带符号整数数组。

I am using the following function to convert a Hex string, such as A591BF86E5D7D9837EE7ACC569C4B59B, to a byte array which I then need to convert to a 8-bit signed integer array.

Function GetByteArray {

    [cmdletbinding()]

    param(
        [parameter(Mandatory=$true)]
        [String]
        $HexString
    )

    $Bytes = [byte[]]::new($HexString.Length / 2)

    For($i=0; $i -lt $HexString.Length; $i+=2){
        $Bytes[$i/2] = [convert]::ToByte($HexString.Substring($i, 2), 16)
    }

    $Bytes  
}

使用该函数后,十六进制将转换为字节数组,如下所示:

After using the function the hex is converted to a byte array such as this:

我需要获取无符号字节数组并将o转换为8bit带符号的字节数组,如下所示:

I need to take the unsigned byte array and convert o to 8bit signed byte array, like the one below:

这可能吗?如果可以,如何实施?

Is this possible? If so how can it be implemented?

我尝试使用BitConverter类,但据我所知,它只能转换为int16。

I've tried using the BitConverter class but, as far as I saw, it can only convert to int16.

预先感谢

推荐答案

获得 [byte []] 数组 [byte] == System.Byte unsigned 8位整数类型):

To get a [byte[]] array ([byte] == System.Byte, an unsigned 8-bit integer type):

$hexStr = 'A591BF86E5D7D9837EE7ACC569C4B59B' # sample input

[byte[]] ($hexStr -split '(.{2})' -ne '' -replace '^', '0X')




  • -split'(。{2})'用2个字符分割输入字符串序列,并用(...)括起来会导致这些序列包含在返回的令牌中; -ne''然后清除 empty 令牌(从技术上讲是实际的数据令牌)。

    • -split '(.{2})' splits the input string by 2-character sequences, and enclosure in (...) causes these sequences to be included in the returned tokens; -ne '' then weeds out the empty tokens (which are technically the actual data tokens).

      -replace,'^','0X'将前缀 0X 放在每个结果之前2个十六进制数字的字符串,产生数组'0XA5','0X91',...

      -replace , '^', '0X' places prefix 0X before each resulting 2-hex-digit string, yielding array '0XA5', '0X91', ...

      将结果投射到 [byte []] 有助于直接识别此十六进制格式。

      casting the result to [byte[]] helpfully recognizes this hex format directly.


      • 注意:如果您忘记了转换,则会得到一个 strings 数组。

      • Note: If you forget the cast, you'll get an array of strings.

      要获取 [sbyte []] 数组 [sbyte] == 系统.SByte ,一个 signed 8位整数),直接将投射 [sbyte []] 代替 not 尝试组合强制转换: [sbyte []] [byte []](...)

      To get an [sbyte[]] array ([sbyte] == System.SByte, a signed 8-bit integer), cast directly to [sbyte[]] instead; do not try to combine the casts: [sbyte[]] [byte[]] (...))

      如果您 已获 然后要转换为 [sbyte []] [byte []] 数组> ,请使用以下方法(可能有更有效的方法):

      If you're given a [byte[]] array that you then want to convert to [sbyte[]], use the following (there may be more efficient approaches):

      [byte[]] $bytes = 0x41, 0xFF # sample input; decimal: 65, 255
      
      # -> [sbyte] values of:  65, -1
      [sbyte[]] $sBytes = ($bytes.ForEach('ToString', 'X') -replace '^', '0X')
      

      应用于样本值,以十进制表示:

      Applied to your sample values, in decimal notation:

      # Input array of [byte]s.
      [byte[]] $bytes = 43, 240, 82, 109, 185, 46, 111, 8, 164, 74, 164, 172
      # Convert to an [sbyte] array.
      [sbyte[]] $sBytes = ($bytes.ForEach('ToString', 'X') -replace '^', '0X')
      $sBytes # Output (each signed byte prints on its own line, in decimal form).
      

      输出:

      43
      -16
      82
      109
      -71
      46
      111
      8
      -92
      74
      -92
      -84
      

      这篇关于在Powershell中是否可以将字节数组转换为8位有符号整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆