In AWS each instance has an ID that looks something like this: i-1234567890abcdef ; the last 16 characters being a hexadecimal number. I would like to treat the "1234567890abcdef" part as a hex number and convert it to base36, so a-z0-9. This way I can use is as the computer's name and not go over the 15 character limit. How is that done in Powershell ?
Converting the input from hex is easy enough: skip the first two characters, and convert to UInt64:
[convert]::ToUInt64($text.Substring(2), 16)
but PowerShell (.Net) has no built-in way to convert to base 36. You'll need to implement it yourself, e.g. this code taken from https://ss64.com/ps/syntax-base36.html and adjusted for larger numbers:
function convertTo-Base36
{
[CmdletBinding()]
param (
[parameter(valuefrompipeline=$true, HelpMessage="Integer number to convert")]
[uint64]$DecimalNumber=""
)
$alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
do
{
$remainder = ($DecimalNumber % 36)
$char = $alphabet.Substring($remainder, 1)
$base36Num = "$char$base36Num"
$DecimalNumber = ($DecimalNumber - $remainder) / 36
}
while ($DecimalNumber -gt 0)
$base36Num
}
Then:
$x='i-1234567890abcdef'
$hexPart = $x.Substring(2)
$decimal = [convert]::ToUInt64($hexPart, 16)
convertTo-Base36 $decimal
# -> 9YS742MX86WF
or:
[convert]::ToUInt64('i-1234567890abcdef'.Substring(2), 16) | Convertto-Base36
Related
I am trying to convert number into IP address using powershell method and checking same online, I am getting 2 different result.
Example:
number = 16812043
result1 = 11.136.0.1 #using powershell
result2 = 1.0.136.11 #https://codebeautify.org/decimal-to-ip-converter
I have tried below code
function Convert-NumberToIP
{
param(
[Parameter(Mandatory=$true)][string]$number
)
[Int64] $numberInt = 0
if([Int64]::TryParse($number, [ref]$numberInt))
{
if(($numberInt -ge 0) -and ($numberInt -le 0xFFFFFFFFl))
{
([IPAddress] $numberInt).ToString()
}
}
}
Convert-NumberToIP -number 16812043
I am getting 2 different result not sure which 1 is correct, or should I update the function.
Use the IPAddress.NetworkToHostOrder() method to flip the endianness of the octets represented by your decimal number:
PS ~> [ipaddress]::new([ipaddress]::NetworkToHostOrder(16812043)).IPAddressToString
1.0.136.11
I'm trying to print the multiplied fps of a video with ffprobe,
.\ffprobe.exe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=avg_frame_rate -i "$videopath" > rate 2>&1
$script:rate = [IO.File]::ReadAllText(".\rate")
$script:framerate = ($rate/2)
echo "Multiplied = "$framerate
What this is supposed to do is to write the fps into a file, which ffprobe does as a fraction (1/10 in this case), and then multiply it by 2.
Since the variable is read as literal text I can't multiply it by 2, and instead it just gives me this.
Multiplied =
1/10
1/10
Is there any way to make it print Multiplied = 20 instead
Powershell doesn't understand fractions, so you'll need to convert the string into numbers and then do the math.
$rate = "1/10";
# convert the string into numbers
$parts = $rate.Split("/"); # #( "1", "10")
$numerator = [int] $parts[0]; # 1
$denominator = [int] $parts[1]; # 10
# do the math
$framerate = $denominator * 2; # 20
write-host "Multiplied = $framerate"
# Multiplied = 20
This has to be done in Perl:
I have integers on the order of e.g. 30_146_890_129 and 17_181_116_691 and 21_478_705_663.
These are supposedly made up of 6 bytes, where:
bytes 0-1 : value a
bytes 2-3 : value b
bytes 4-5 : value c
I want to isolate what value a is. How can I do this in Perl?
I've tried using the >> operator:
perl -e '$a = 330971351478 >> 16; print "$a\n";'
5050222
perl -e '$a = 17181116691 >> 16; print "$a\n";'
262163
But these numbers are not on the order of what I am expecting, more like 0-1000.
Bonus if I can also get values b and c but I don't really need those.
Thanks!
number >> 16 returns number shifted by 16 bit and not the shifted bits as you seem to assume. To get the last 16 bit you might for example use number % 2**16 or number & 0xffff. To get to b and c you can just shift before getting the last 16 bits, i.e.
$a = $number & 0xffff;
$b = ($number >> 16) & 0xffff;
$c = ($number >> 32) & 0xffff;
If you have 6 bytes, you don't need to convert them to a number first. You can use one the following depending on the order of the bytes: (Uppercase represents the most significant byte.)
my ($num_c, $num_b, $num_a) = unpack('nnn', "\xCC\xcc\xBB\xbb\xAA\xaa");
my ($num_a, $num_b, $num_c) = unpack('nnn', "\xAA\xaa\xBB\xbb\xAA\xaa");
my ($num_c, $num_b, $num_a) = unpack('vvv', "\xcc\xCC\xbb\xBB\xaa\xAA");
my ($num_a, $num_b, $num_c) = unpack('vvv', "\xaa\xAA\xbb\xBB\xcc\xCC");
If you are indeed provided with a number 0xCCccBBbbAAaa), you can convert it to bytes then extract the numbers you want from it as follows:
my ($num_c, $num_b, $num_a) = unpack('xxnnn', pack('Q>', $num));
Alternatively, you could also use an arithmetic approach like you attempted.
my $num_a = $num & 0xFFFF;
my $num_b = ( $num >> 16 ) & 0xFFFF;
my $num_c = $num >> 32;
While the previous two solutions required a Perl built to use 64-bit integers, the following will work with any build of Perl:
my $num_a = $num % 2**16;
my $num_b = ( $num / 2**16 ) % 2**16;
my $num_c = int( $num / 2**32 );
Let's look at ( $num >> 16 ) & 0xFFFF in detail.
Original number: 0x0000CCccBBbbAAaa
After shifting: 0x00000000CCccBBbb
After masking: 0x000000000000BBbb
I have the following predefined codes that represent an index in a binary bitmap:
0 = standard
1 = special
2 = regular
3 = late
4 = early
5 = on time
6 = generic
7 = rfu
An example value I would take as an input would be 213, which becomes 11010101 in binary. Index 0, 2, 4, 6, and 7 have their bit flipped indicating that this record is:
standard + regular + early + generic + rfu.
I am trying to figure out in perl how to take that binary data and build a string, like mentioned above with code + code + code, etc.
Any help would be greatly appreciated. Thanks.
Edit: My thoughts on how I might approach this are:
Convert decimal to binary
Find length of binary string
Using substr get the value (0 or 1) index by index
If index value = 1 then add relevant code to string
Is there a better way to go about this?
You can test bits on input from 0 to 7, and take only these that are set,
my $in = 213;
my #r = ("standard","special","regular","late","early","on time","generic","rfu");
print join " + ", #r[ grep { $in & (1 << $_) } 0 .. $#r ];
# or
# print join " + ", map { $in & (1<<$_) ? $r[$_] : () } 0 .. $#r;
output
standard + regular + early + generic + rfu
I am trying to convert AD maxpwdAge (a 64-bit integer) into a number of days.
According to Microsoft:
Uses the IADs interface's Get method to retrieve the value of the domain's maxPwdAge attribute (line 5).
Notice we use the Set keyword in VBScript to initialize the variable named objMaxPwdAge—the variable used to store the value returned by Get. Why is that?
When you fetch a 64-bit large integer, ADSI does not return one giant scalar value. Instead, ADSI automatically returns an IADsLargeInteger object. You use the IADsLargeInteger interface's HighPart and LowPart properties to calculate the large integer's value. As you may have guessed, HighPart gets the high order 32 bits, and LowPart gets the low order 32 bits. You use the following formula to convert HighPart and LowPart to the large integer's value.
The existing code in VBScript from the same page:
Const ONE_HUNDRED_NANOSECOND = .000000100 ' .000000100 is equal to 10^-7
Const SECONDS_IN_DAY = 86400
Set objDomain = GetObject("LDAP://DC=fabrikam,DC=com") ' LINE 4
Set objMaxPwdAge = objDomain.Get("maxPwdAge") ' LINE 5
If objMaxPwdAge.LowPart = 0 Then
WScript.Echo "The Maximum Password Age is set to 0 in the " & _
"domain. Therefore, the password does not expire."
WScript.Quit
Else
dblMaxPwdNano = Abs(objMaxPwdAge.HighPart * 2^32 + objMaxPwdAge.LowPart)
dblMaxPwdSecs = dblMaxPwdNano * ONE_HUNDRED_NANOSECOND ' LINE 13
dblMaxPwdDays = Int(dblMaxPwdSecs / SECONDS_IN_DAY) ' LINE 14
WScript.Echo "Maximum password age: " & dblMaxPwdDays & " days"
End If
How can I do this in Perl?
Endianness may come into this, but you may be able to say
#!/usr/bin/perl
use strict;
use warnings;
my $num = -37_108_517_437_440;
my $binary = sprintf "%064b", $num;
my ($high, $low) = $binary =~ /(.{32})(.{32})/;
$high = oct "0b$high";
$low = oct "0b$low";
my $together = unpack "q", pack "LL", $low, $high;
print "num $num, low $low, high $high, together $together\n";
Am I missing something? As far as I can tell from your question, your problem has nothing at all to do with 2’s complement. As far as I can tell, all you need/want to do is
use Math::BigInt;
use constant MAXPWDAGE_UNIT_PER_SEC => (
1000 # milliseconds
* 1000 # microseconds
* 10 # 100 nanoseconds
);
use constant SECS_PER_DAY => (
24 # hours
* 60 # minutes
* 60 # seconds
);
my $maxpwdage_full = ( Math::BigInt->new( $maxpwdage_highpart ) << 32 ) + $maxpwdage_lowpart;
my $days = $maxpwdage_full / MAXPWDAGE_UNIT_PER_SEC / SECS_PER_DAY;
Note that I deliberately use 2 separate constants, and I divide by them in sequence, because that keeps the divisors smaller than the range of a 32-bit integer. If you want to write this another way and you want it to work correctly on 32-bit perls, you’ll have to keep all the precision issues in mind.