schnipdip
schnipdip

Reputation: 151

Getting the binary value of a character string in Powershell

$getInput = Read-Host "ASCII or Binary? `n"
$getInput = $getInput.toLower()

if($getInput -eq "ascii"){

    ""

    #Write-Host "Type In Your ASCII" -backgroundcolor "black" 
    $getAscii = Read-Host "Type In Your ASCII`n" 
    ""
    ""
    $readAscii = @($getAscii)
    [byte[]]$outBytes = $readAscii 

}
elseif($getInput -eq "binary"){

}
else{
    Write-Host "Wrong Input... [ASCII] or [BINARY]" -backgroundcolor "red" -foregroundcolor "white"
}

I want to be able to get a users paragraph or whatever string they put in and convert it to binary. The [conver]::toString($getAscii,2) only works for integers.

Upvotes: 3

Views: 21531

Answers (3)

thepip3r
thepip3r

Reputation: 2935

'hello world' -split '' | % {
    if ($_ -ne '') {
        #[int][char]$_
        [System.Convert]::ToString(([int][char]$_),2)
    }
}
  1. Use the split operator to split the string by each character
  2. Send that down the pipeline to a foreach-object loop
  3. The split operation ends up including the space character in the string so the conditional makes sure we don't act upon it--we filter it out.
  4. The commented line was for testing purposes. Each character has a TYPE of [string] and we need it as a [char] so we explicitly cast it as such and the PowerShell engine dynamically switches it for us (as long as it can). In the same line, we explicitly cast the [char] to an [int] to get the ASCII->decimal representation. This test was just to ensure I was getting the right output and I left it commented in case the OP wanted to see it.
  5. Finally, we use the ToString() method of the System.Convert class which accepts a "base" parameter to define that we want a base2 (binary) representation of the integer supplied in position 1, casted as TYPE [string].

Upvotes: 2

ArcSet
ArcSet

Reputation: 6860

Try this

$string = "ABCDEF"
[system.Text.Encoding]::Default.GetBytes($String) | %{[System.Convert]::ToString($_,2).PadLeft(8,'0') }

[system.Text.Encoding]::Default.GetBytes($String)

This turns a string into a byte array. You can change Default to another Encoding

| %{[System.Convert]::ToString($_,2).PadLeft(8,'0') }

This turns each byte in the byte array into a binary representation. ToString([object],[Enum]), in this case the byte will have a number value like 65 if converted to string the 2 will say turn the 65 into base 2. You could also use 8(octo), 10(which is the same as none aka base 10) and 16(Hex). Then it pads the left till its 8 char long with char 0's

Upvotes: 4

Kowalchick
Kowalchick

Reputation: 448

I recommend utilizing the Encoding library similarly to this user:

$stringToConvert = "Hello World"

    $test = [System.Text.Encoding]::UTF8.GetBytes($stringToConvert) | %{ [System.Convert]::ToString($_,2).PadLeft(8,'0') }

    $test

Source: https://www.reddit.com/r/PowerShell/comments/3e82vk/convert_string_to_binary_and_back/

*Note: I believe the original poster of this method intended to assign $foo to the second conversion. I believe it will work either way because the return will be dumped to the variable below.

Upvotes: 0

Related Questions