如何使用管道获取满足标准的信息,同时比较十六进制数字

发布于 2025-01-11 03:52:09 字数 2155 浏览 0 评论 0原文

我有一个管道,用于从其他设备型号的 Excel 电子表格中获取信息,但对于此设备型号,该值是十六进制,十六进制很不寻常,因为 0x10 = 0x00010,所以我需要比较这些值在管道中。

这是我在电子表格内容返回后用于非十六进制值的管道:

$deviceErrDescMap = Process_ErrorDescMap -errorCodeListFilePath $errorCodeListFile #excel is returned
$deviceErrDescRow = $deviceErrDescMap | Where-Object 'Value' -eq $sdkNum

在这个中, $deviceErrDescMap 保存如下电子表格值:

Name   Value        Description
A      0x00000010   Check Material A
B      0x00000100   Check Interlock
C      0x00000020   Check Material C

这是我获取 excel 内容的方式,以防万一重要:

Function Process_ErrorDescMap{
    [cmdletbinding()]
      Param ([string]$errorCodeListFilePath) 
      Process
      {
            if(Test-Path $errorCodeListFilePath)
            {
              #Excel method
              #Install-Module -Name ImportExcel -Scope CurrentUser -Force (dependency - 1. Close all instances of PowerShell (console, ISE, VSCode), 2. Move the PackageManagement folder from OneDrive, 3. Open a PowerShell Console (Run As Administrator), 4. Run Install-Module ImportExcel)
              if(($errorCodeListFilePath -match "DeviceA") -or ($errorCodeListFilePath -match "DeviceB"))
              {
                $startRow = 1
              }
              else 
              {
                $startRow = 2
              }
              $importedExcel = Import-Excel -Path $errorCodeListFilePath -StartRow $startRow

              return $importedExcel #list of error desc 
            }
            else {
              Write-Host "Invalid path given: $($errorCodeListFilePath)"
              return "***Invalid Path $($errorCodeListFilePath)"
            }
      } #end Process
    }# End of Function Process_ErrorDescMap

电子表格的第一行,值 0x00000010 应与 $sdkNum=0x10 进行比较,这是第一个。因此 0x10(或 0x010)需要匹配或等于电子表格值 0x0000010 并从地图中获取它。我对如何实现这一目标有点茫然。我不知道如何将“值”转换为十六进制,并将其与此管道中 $sdkNum 的十六进制值进行比较。我正在考虑使用正则表达式从 $sdkNum 中获取 10,并使用 match 从电子表格内容中获取任何包含 10 的行,然后进一步比较。我觉得有一种更简单的方法,而且我不确定如何从十六进制字符串中获取非零数字和右侧的 0。

如果您对这种十六进制比较感到困惑,请随意使用十六进制到十进制转换网页,您将看到 0x10 = 0x000010。我也觉得很奇怪。重要的是 1 后面的 0。

这是使用 PowerShell 5.1 和 VSCode 的情况。

I have a pipeline I've used to get info taken from an excel spreadsheet for other device models, but for this device model, the value is hex, and hex is unusual, in that 0x10 = 0x00010, so I need to compare those values in the pipeline.

This is my pipeline I've used for non-hex values after the spreadsheet content is returned:

$deviceErrDescMap = Process_ErrorDescMap -errorCodeListFilePath $errorCodeListFile #excel is returned
$deviceErrDescRow = $deviceErrDescMap | Where-Object 'Value' -eq $sdkNum

In this, $deviceErrDescMap is holding spreadsheet values like this:

Name   Value        Description
A      0x00000010   Check Material A
B      0x00000100   Check Interlock
C      0x00000020   Check Material C

This is how I get excel contents, in case it matters:

Function Process_ErrorDescMap{
    [cmdletbinding()]
      Param ([string]$errorCodeListFilePath) 
      Process
      {
            if(Test-Path $errorCodeListFilePath)
            {
              #Excel method
              #Install-Module -Name ImportExcel -Scope CurrentUser -Force (dependency - 1. Close all instances of PowerShell (console, ISE, VSCode), 2. Move the PackageManagement folder from OneDrive, 3. Open a PowerShell Console (Run As Administrator), 4. Run Install-Module ImportExcel)
              if(($errorCodeListFilePath -match "DeviceA") -or ($errorCodeListFilePath -match "DeviceB"))
              {
                $startRow = 1
              }
              else 
              {
                $startRow = 2
              }
              $importedExcel = Import-Excel -Path $errorCodeListFilePath -StartRow $startRow

              return $importedExcel #list of error desc 
            }
            else {
              Write-Host "Invalid path given: $($errorCodeListFilePath)"
              return "***Invalid Path $($errorCodeListFilePath)"
            }
      } #end Process
    }# End of Function Process_ErrorDescMap

The spreadsheet's first line, with Value 0x00000010 should compare with $sdkNum=0x10, which is the first one. So 0x10 (or 0x010) needs to match, or be equal to this spreadsheet value of 0x0000010 and grab it from the map. I'm at a bit of a loss as to how to accomplish this. I'm not sure how to convert 'Value' to hex, and compare it with the hex value of $sdkNum in this pipeline. I was thinking of using a regex to get the 10 from $sdkNum, and use match to get any rows containing 10 from the spreadsheet content, and then further compare. I feel like there's an easier way, plus I'm not sure how I'd get just the non-zero number and 0's to the right of that out of the hex string.

If you are confused by this hex comparison, feel free to use a hex to decimal conversion web page, and you will see 0x10 = 0x000010. I thought it was strange too. it's the 0's after the 1 that matter.

This is with PowerShell 5.1 and VSCode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄意 2025-01-18 03:52:09

当您将字符串转换为整数类型时,PowerShell 将本机解析有效的十六进制数字:

PS ~> [int]'0x10'
16

由于 PowerShell 的所有重载比较运算符 (-eq/-ne/- gt/-ge/-lt/-le) 自动将右侧操作数转换为左侧操作数的类型-手侧操作数,您需要做的就是确保您提供的第一个操作数表达式已经是 [int]

$sdkNum = 0x10 # notice no quotation marks

# Option 1: cast the hex string to `[int]` explicitly
... |Where-Object { [int]$_.Value -eq $sdkNum }
# Option 2: $sdkNum is already an [int], PowerShell automatically converts hex string to int
... |Where-Object { $sdkNum -eq $_.Value }

PowerShell will natively parse a valid hexadecimal numeral when you convert a string to an integral type:

PS ~> [int]'0x10'
16

Since all of PowerShell's overloaded comparison operators (-eq/-ne/-gt/-ge/-lt/-le) automatically converts the right-hand side operand to the type of the left-hand side operand, all you need to do is make sure the expression you provide as the first operand is already an [int]:

$sdkNum = 0x10 # notice no quotation marks

# Option 1: cast the hex string to `[int]` explicitly
... |Where-Object { [int]$_.Value -eq $sdkNum }
# Option 2: $sdkNum is already an [int], PowerShell automatically converts hex string to int
... |Where-Object { $sdkNum -eq $_.Value }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文