console write line every 8 characters

I'm converting text to binary, and sending that information to the console. However, I want the console to write a new line for every 8 characters.

My convert to binary code looks like this (where Result.Text contains random text, that will be converted)

 Dim Resultconvert As String = ""
 For Each C As Char In Result.Text
   Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
   Resultconvert &= s
 Next
 Console.WriteLine(Resultconvert)

So it should look like this:

01110010
01111110
01101111
01011100
01100100
01010001
01001101
00111010
01010100

instead of:

011100100111111001101111010111000110010001010001010011010011101001010100

Any help is appreciated. Thank you in advance.

Answers


    Dim Resultconvert As String = String.Empty

    For Each C As Char In Result.Text
        Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
        Debug.Print(s)
        Resultconvert &= s
    Next

The simplest solution is to just call WriteLine separately for each byte, like this:

For Each C As Char In Result.Text
    Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
    Console.WriteLine(s)
Next

However, if you want to write it all at once, you need to append a new line to the string after each byte, like this:

Dim Resultconvert As String = ""
For Each C As Char In Result.Text
    Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
    Resultconvert &= s & Environment.NewLine
Next
Console.WriteLine(Resultconvert)

But, at that point, it would be easier and more efficient to use a StringBuilder:

Dim builder As New StringBuilder()
For Each C As Char In Result.Text
    Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
    builder.AppendLine(s)
Next
Console.WriteLine(builder.ToString())

Need Your Help

The parameter of thread function on iOS

ios objective-c cocos2d-iphone

I was testing with a thread function. But the program always feedback an error. I guess that the error probably have something to do with the argument of the thread function, but i've no idea how t...

Javascript synchronous AJAX timeout

javascript ajax asynchronous onbeforeunload

My Javascript needs to send some data to a server when the page closes, which I currently do with a synchronous AJAX (SJAX?) request in window.onbeforeunload. The problem with this, of course, is t...