3
$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.

2
  • one char or entire string? Commented Jun 7, 2017 at 16:36
  • String preferably, so hello world would be 01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100 0001010 Commented Jun 7, 2017 at 16:40

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

default can be changed to any character encoding?
Yup it can. Just delete and replace
2
'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].

Comments

0

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.

3 Comments

I saw this post, i just wasn't sure if it was applicable
Yes this works and I just tested it: Hello for instance returns what you were looking for - Simply set a variable to replace the current string "hello" (or a parameter) and you should have the start to a simple conversion method.
I have modified the solution to perform exactly what you were looking for: Please test it out in powershell and you will see it performs the action you intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.