Using FromArgb for Int32[] value results in swapped Red and Blue values - powershell

I have this Int32[] value 14508153 which should be:
R : 121
G : 96
B : 221
If I use:
[System.Drawing.Color]::FromArgb(14508153)
It returns:
R : 221
G : 96
B : 121
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : dd6079
Questions
How or why are those values swapped for R and B using that function?
Is there a built-in PowerShell method to convert them correctly?

I can't speak to why the bytes need to be rearranged (see the bottom section for thoughts), but here's how you can do it:
Add-Type -AssemblyName System.Drawing
$bytes = [System.BitConverter]::GetBytes(14508153)
[byte[]] $rearrangedBytes = $bytes[2], $bytes[1], $bytes[0], $bytes[3]
[System.Drawing.Color]::FromArgb(
[System.BitConverter]::ToInt32($rearrangedBytes, 0)
)
See System.BitConverter.GetBytes(), System.BitConverter.ToInt32().
The above yields:
R : 121
G : 96
B : 221
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : 7960dd
It appears that only 3 of the bytes in your [int] (System.Int32) value are relevant, and that they are in Big-Endian order (see the Wikipedia article about endianness).
By contrast, System.Drawing.Color.FromArgb() expects all 4 bytes to be relevant, in Little-Endian order.

Related

Problem with upgrading php version from 7.4 to 8

I am using Joomla version 3.10 and after upgrading the php version from 7.4 to 8.0, I received this error.
Fatal error: Unparenthesized a ? b : c ? d : eis not supported. Use either(a ? b : c) ? d : eora ? b : (c ? d : e) in /home/public_html/language/fa-IR/fa-IR.localise.php on line 115
I put the source code of fa-IR.localise.php on line 115 below:
`if (strpos($return, self::MONTH_LENGTH) !== false) {
$return = str_replace(self::MONTH_LENGTH, $m < 7 ? 31 : $m < 12 ? 30 : self::leap_persian($y) ? 30 : 29 , $return);`
Thank you for helping me.
When I go back to php version 7.4, the problem is solved
$return = str_replace(self::MONTH_LENGTH, ($m < 7 ? 31 : ($m < 12 ? 30 : (self::leap_persian($y) ? 30 : 29) ) ) , $return);
Operations need after PHP 7.4 this ( ... ) brace's.

How to find specific string, with certain starting and ending character in Powershell

how to find all the strings which starts with open square bracket and ends with close square bracket in text file.
used below code but getting some unwanted contents also. How to avoid those unwanted contents
Select-String -Path "text.txt" -Pattern '\['
input file:
some conents [hello]
adfhjadf adfjkkf [ghad]
[check] dfgsf sfgfs
required output file
[hello]
[ghad]
[check]
Here, try this:
$string = #'
some conents [hello]
adfhjadf adfjkkf [ghad]
[check] dfgsf sfgfs
'#
([regex]'\[.*?\]').Matches($string)
Note, this will match anything inside the brackets. If you're looking only for English characters you should refine the regex.
Output should look like this:
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 13
Length : 7
Value : [hello]
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 39
Length : 6
Value : [ghad]
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 47
Length : 7
Value : [check]
If you just want to see the matches you can do:
PS /> ([regex]'\[.*?\]').Matches($string).Value
[hello]
[ghad]
[check]

Set of data needs to be rearranged and exported as CSV

I have a set of data and it needs to be rearranged with header. PowerShell is a quick way, I think, to do this.
The data as an example:
AAA,
123,53,145,
text1 text12,text2,23.00,
21,6,0.21,0.00,2,
1,321,4
BBB,1,421,5,
text3 text34,
text4,41,22.5,1,
23,674,75,8,5....
As you can see the ',' seperates the values and the dot is just part of the value. The AAA and BBB represent the names and the values for AAA are before BBB. I expect the values to be like this within CSV file with each value in a new cell:
Name: AAA
abc: 123
def: 53
ghi: 145
jkl: text1 text12
mno: text2
pqr: 23.00
stu: 21
vwx: 6
yz: 0
Or the header would be like: Name abc def etc. and the values in the next cell under the header.
In total there are 16 values separated by a comma for one name (ex. AAA). There are maybe hundreds of names with random values.
I didn't get the result with Sort-Object, because it would scan the data and put the values in random order.
here's one way to do the job. it presumes that there are 18 properties and that each object ends with a line that does NOT end with a comma.
the resulting collection can be gracefully sent to a CSV via Export-CSV. [grin]
# fake reading in a text file as one multi-line string
# in real life, use Get-Content -Raw
# similar names & properties are NOT part of the data
# they are there for visually crosschecking the output
$InStuff = #'
Object_A,
A1,A2,A3,
A4 some other stuff,A5,
A6,A7 more extras,A8,A9,A10.00,
A11,A12,A13 thirteen,A14,
A15,A16.666,A17,A18
Object_B,B1.1,B2.2,B3.3,
B4 some other stuff,B5,B6,B7 more extras,B8,B9,B10.00,
B11,B12,B13 thirteen,B14,
B15,B16.666,B17,B18
'#
$ColumnHeaders = ('Name' +
' Col_01 Col_02 Col_03 Col_04 Col_05 Col_06 Col_07 Col_08 Col_09 Col_10' +
' Col_11 Col_12 Col_13 Col_14 Col_15 Col_16 Col_17 Col_18') -split ' '
$CSV = $InStuff -split "(?<!,)$([environment]::NewLine)" |
ForEach-Object {
$_ -replace ",$([environment]::NewLine)", ','
}
$Results = $CSV |
ConvertFrom-Csv -Header $ColumnHeaders
$Results
output ...
Name : Object_A
Col_01 : A1
Col_02 : A2
Col_03 : A3
Col_04 : A4 some other stuff
Col_05 : A5
Col_06 : A6
Col_07 : A7 more extras
Col_08 : A8
Col_09 : A9
Col_10 : A10.00
Col_11 : A11
Col_12 : A12
Col_13 : A13 thirteen
Col_14 : A14
Col_15 : A15
Col_16 : A16.666
Col_17 : A17
Col_18 : A18
Name : Object_B
Col_01 : B1.1
Col_02 : B2.2
Col_03 : B3.3
Col_04 : B4 some other stuff
Col_05 : B5
Col_06 : B6
Col_07 : B7 more extras
Col_08 : B8
Col_09 : B9
Col_10 : B10.00
Col_11 : B11
Col_12 : B12
Col_13 : B13 thirteen
Col_14 : B14
Col_15 : B15
Col_16 : B16.666
Col_17 : B17
Col_18 : B18

stockage in structure and iteration

I would like to understand one thing :
When I write : Propreties.Device.Time = Data.Device(lobo(:,2),1) - Data.Device(lobo(:,1),1) I obtain a stockage of all the differencies (it's great)
But when I write : Propreties.Device.Prop = sum(Data.Device(lobo(:,1) : lobo(:,2)),2)*dt I don't obtain a stockage of all results (that I would like to obtain as previous exemple) but an overwrite of each of them so at the end I have only one value :-/
Could someone explain to me what differs between the two examples and what I could implement to obtain in the second example the same type of result as in the first (ie a list of results and not a single result) (without making a loop) ?
(Matlab verison : R2017a)
Some data for exemple :
Data.Device = [1.86000000000000 675 0;1.87000000000000 685 0;1.88000000000000 695 0;1.89000000000000 705 0;1.90000000000000 710 5;1.91000000000000 715 50;1.92000000000000 700 120;1.93000000000000 685 180;1.94000000000000 655 235;1.95000000000000 620 285;1.96000000000000 565 305;1.97000000000000 505 315;1.98000000000000 435 335;1.99000000000000 360 345;2 285 355];
lobo = [1 5; 6 15];
dt = 0.01
Propreties.Device.Time = Data.Device(lobo(:,2),1) - Data.Device(lobo(:,1),1);
Propreties.Device.Prop = sum(Data.Device(lobo(:,1) : lobo(:,2)),2)*dt

How to get a child item into a variable on its own

I am relatively sure this is quite easy to do but my google fu is not running strong
At the moment I am doing:
add-pssnapin windows.serverbackup
get-wbsummary
This returns me:
NextBackupTime : 07/09/2012 12:00:00
NumberOfVersions : 210
LastSuccessfulBackupTime : 06/09/2012 21:00:13
LastSuccessfulBackupTargetPath : \\?\Volume{bf315689-e5ed-11e1-a376-d067e5f384ea}
LastSuccessfulBackupTargetLabel : SBSERVE 2012_08_21 12:20 DISK_01
LastBackupTime : 06/09/2012 21:00:13
LastBackupTarget : SBSERVE 2012_08_21 12:20 DISK_01
DetailedMessage :
LastBackupResultHR : 0
LastBackupResultDetailedHR : 0
CurrentOperationStatus : NoOperationInProgress
What I want to do is get just the result portion (not its title into a variable) so for example $lastbackuptime = 07/09/2012 12:00:00
PS> $wbs = Get-WBSummary
PS> $lastbackuptime = $wbs.NextBackupTime