Search code examples
powershellhexpipeline

how use pipeline to get info meeting criteria, but also comparing hex numbers


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.


Solution

  • 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 }