控制台每8个字符写一行

时间:2014-03-12 12:44:14

标签: vb.net binary console

我将文本转换为二进制文件,并将该信息发送到控制台。但是,我希望控制台为每8个字符写一个新行。

我转换为二进制代码看起来像这样(其中Result.Text包含随机文本,将被转换)

 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)

所以看起来应该是这样的:

01110010
01111110
01101111
01011100
01100100
01010001
01001101
00111010
01010100

而不是:

011100100111111001101111010111000110010001010001010011010011101001010100

感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:1)

    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

答案 1 :(得分:0)

最简单的解决方案是分别为每个字节调用WriteLine,如下所示:

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

但是,如果要一次写入所有内容,则需要在每个字节后面的字符串中添加一个新行,如下所示:

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)

但是,在那时,使用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())