The only datatype in powershell that I'm aware of that can store 128-bits is the [decimal]
and while I seem to be able to store the variable:
PS /home/leeand00/> [decimal]$ipv6addr=0x20010db81234
I don't seem to be able to pull it back out again (clearly that's a decimal):
PS /home/leeand00/>$ipv6addr
35188897223220
And I thought maybe this would do it, but it does not:
PS /home/leeand00/>"{0:x4}" -f ($ipv6addr)
Error formatting a string: Format specifier was invalid..
At line:1 char:1
+ "{0:x4}" -f $ipv6addr
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ({0:x4}:String) [], RuntimeException
+ FullyQualifiedErrorId : FormatError
The above error doesn't make sense because...the datatypes is decimal
PS /home/leeand00/Documents/lifehacker organized/docs> $ipv6addr.GetType().Name
Decimal
PS, I'm aware there are tools for working with IPv6 in Powershell, but I'm just trying to perform some simple binary operations when learning about it.
PowerShell is just using the underlying .NET types and converters. The 'X' specifier does not support decimal input:
Standard Numeric Format Strings
Result: A hexadecimal string.
Supported by: Integral types only.
Precision specifier: Number of digits in the result string.
If you have a decimal, one option to make this work is to first cast to an int
, int64
or uint64
(depending on size of input):
"0x{0:x4}" -f [int64]ipv6addr
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.