【问题标题】:Show hex values of Powershell output of GetInvalidFileNameChars()显示 GetInvalidFileNameChars() 的 Powershell 输出的十六进制值
【发布时间】:2020-01-22 06:11:02
【问题描述】:

在带有 PowerShell v. 2 的 Windows 7 中,我尝试使用 [System.IO.Path]::GetInvalidFileNameChars() 查看它的无效字符列表。

在提示符下输入时,它会列出字符,每行一个。我想要的是获取他们的十六进制(或十进制)代码。

我尝试过的事情:

  • ForEach ($char in [System.IO.Path]::GetInvalidFileNameChars()) {"{0:x}" -f $char} :输出字符,而不是十六进制。
  • [System.IO.Path]::GetInvalidFileNameChars() | { $_.ToString("X2") }

表达式只能作为管道的第一个元素。

  • [System.IO.Path]::GetInvalidFileNameChars() | Format-Hex -Raw -Encoding Ascii

无法识别术语“格式-十六进制”...

  • ForEach ($c in [System.IO.Path]::GetInvalidFileNameChars()) {echo $c.ToString("X2")}

找不到“ToString”的重载和参数计数:“1”。

我熟悉 Bash、Perl,甚至是蹩脚的 cmd.exe,但很遗憾对 PowerShell 一无所知。

但我确信必须有一种简单易行的方法来列出这些字符的十六进制值。

更新: 感谢下面的答案,我最终使用的命令是这个命令,除了十六进制值之外,它还打印十进制值和字符本身:

ForEach ($c in [System.IO.Path]::GetInvalidFileNameChars()) { $i=([int]$c); "{0,3:d}  {1,2:x2}  {2,1}" -f $i,$i,$c }

【问题讨论】:

    标签: windows powershell hex powershell-2.0


    【解决方案1】:

    除了缺少foreach,GetInvalidFileNameChars() 输出char,

    [System.IO.Path]::GetInvalidFileNameChars() | foreach { $_.gettype() }
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Char                                     System.ValueType
    True     True     Char                                     System.ValueType
    

    而 tostring('x2') 方法适用于整数(或字节)。

    (1).tostring('x2')
    01
    

    您可以通过省略括号来获得定义。第二个是您要运行的那个。

    (1).tostring
    
    OverloadDefinitions
    -------------------
    string ToString()
    string ToString(string format)
    string ToString(System.IFormatProvider provider)
    string ToString(string format, System.IFormatProvider provider)
    string IConvertible.ToString(System.IFormatProvider provider)
    string IFormattable.ToString(string format, System.IFormatProvider formatProvider)
    

    所以(因为 .tostring 的优先级高于 [int],我们需要额外的括号)(我在 osx 中,所以只有 2 个结果。)

    [System.IO.Path]::GetInvalidFileNameChars() | foreach { ([int]$_).tostring('x2') }
    00
    2f
    

    如果您想要十六进制格式,请升级您的 powershell。

    与上一个相同。该方法适用于整数:

    ForEach ($c in [System.IO.Path]::GetInvalidFileNameChars()) { 
      echo ([int]$c).ToString("X2") }
    

    Int32.ToString 方法:https://docs.microsoft.com/en-us/dotnet/api/system.int32.tostring?view=netframework-4.8#System_Int32_ToString_System_String_

    【讨论】:

      【解决方案2】:

      试试

      [System.IO.Path]::GetInvalidFileNameChars() | ForEach-Object {'{0:X2}' -f [int]$_ }
      

      【讨论】:

        【解决方案3】:

        我想要的是获得他们的十六进制(或十进制)代码。

        由于[System.IO.Path]::GetInvalidFileNameChars() 返回[char[]][char] 实例的数组 ([]),您可以直接将该数组转换为 [int[]] 以获得 Unicode 代码点 .NET [char] 实例表示的 UTF-16 代码单元;默认情况下,它们呈现为十进制数字

        PS> [int[]] [System.IO.Path]::GetInvalidFileNameChars()
        
        34
        60
        62
        124
        0
        ...
        

        要获取 十六进制 数字(作为字符串),请通过管道传递到 ForEach-Object 并通过传递给 PowerShell 的字符串格式化运算符 -f 的格式字符串格式化每个数字;例如,将每个代码点格式化为以0x 为前缀的十六进制。将 0-left-padded 到 2 位的数字(PSv4+ 语法;在 PSv3- 中,用管道连接到 ForEach-Object,就像在 Theo's helpful answer 中一样,这会更慢):

        PS> [System.IO.Path]::GetInvalidFileNameChars().ForEach({ '0x{0:x2}' -f [int] $_ })
        
        0x22
        0x3c
        0x3e
        0x7c
        0x00
        ...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-10-21
          • 2013-02-02
          • 1970-01-01
          • 1970-01-01
          • 2017-01-18
          • 2014-10-12
          • 2012-03-13
          相关资源
          最近更新 更多