Results 1 to 12 of 12

Thread: Textbox line by line

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Textbox line by line

    Hi
    Maybe I'm just to tired to see what I'm missing, but I could need some help with this.
    I have 2 textboxes, one with hex data and one which I convert to ASCII. The first textbox is sized to fit 16 chr, this will be equal to 8 Ascii. I wan't my code to convert line by line. Which means that max chr in a line for textbox2 should be 8. Maybe I should use a StreamWriter, instead of my code?

    Code:
    Public Class Form1
        Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim hex As String = TextBox1.Text
            Dim hexNumber As Integer
            Dim i As Integer
            For i = 0 To hex.Length - 1 Step 2
                hexNumber = Convert.ToInt32(TextBox1.Text.Substring(i, 2), 16)
    
                For Each line As String In TextBox1.Lines
                    TextBox2.AppendText(Chr(hexNumber & vbCrLf))
                Next
            Next
        End Sub
    
    End Class

  2. #2
    Junior Member
    Join Date
    Sep 2008
    Location
    Finland
    Posts
    20

    Re: Textbox line by line

    Here you go

    Code:
    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
        Dim hexNumber As Integer
        Dim i As Integer
    
        For Each line As String In TextBox1.Lines
          For i = 0 To line.Length - 1 Step 2
            hexNumber = Convert.ToInt32(line.Substring(i, 2), 16)
            TextBox2.AppendText(Chr(hexNumber))
          Next i
          TextBox2.AppendText(vbCrLf)
        Next
    
    End Sub
    Teme64 @ windevblog.blogspot.com

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Re: Textbox line by line

    Hi
    Thank's
    Well the outcome is about the same as i had, yours will though goto next line everytime the button is clicked. But it will fill the whole Textbox2 line, even if Textbox 1 line is shorter. Textbox1 line1 should be equal to Textbox2 line1, but converted to ASCII.

  4. #4
    Junior Member
    Join Date
    Sep 2008
    Location
    Finland
    Posts
    20

    Re: Textbox line by line

    Maybe I missed something. TextBox1 contains, lets say:
    64646464
    65656565
    that's two lines with 4 hex values (8 characters). The code converts it to:
    dddd
    eeee
    which is 4 ascii chars. You try to do that, don't you?

    But it will fill the whole Textbox2 line, even if Textbox 1 line is shorter
    Don't understand what you mean by that.

    Does your TextBox1 contain some binary data to be converted to ascii?

    Teme64 @ windevblog.blogspot.com

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Re: Textbox line by line

    Hi
    Well, sort of. Here's an example:

    Code:
    45 00 01 35 24 DC 00 00 80 11 90 27 C0 A8 01 66 
    C0 A8 01 FE FA A1 56 F2 01 21 A3 7B 64 31 3A 72 
    64 32 3A 69 64 32 30 3A 1B 4F 71 18 55 CC E1 B8 
    E8 6B 95 96 18 A0 66 91 B8 F9 5C 37 35 3A 6E 6F 
    64 65 73 32 30 38 3A 1B 54 D9 EE 37 75 5D ED D6 
    00 C4 DF C7 1B ED 3C 6E 26 03 8A 4D CA 25 7B 4A 
    3D 1B 54 AA 77 5F 9B BF 17 6E 37 73 99 7D D6 AB 
    37 B5 AF A0 1B C0 A8 01 FE 49 CA 1B 53 B0 12 25 
    92 6F 71 43 B5 E5 9C CD 4A EE 8A 53 6B 0F B6 55 
    31 FB DA 3E AE 1B 53 46 48 9C C6 4A 6A F6 BA A1 
    04 87 27 38 E3 3A 5C F3 EE 7D E8 89 58 56 E7 1B 
    53 6B E7 7B AB 01 71 3E 1E 3E 97 94 90 EA D8 94 
    2A 76 82 5B 43 0D 33 62 2B 1B 52 3F 46 19 4D 43 
    FE B9 64 B6 2B A2 95 EC 18 37 31 FF 8D 43 B7 88
    If you copy and past this into textbox1 then textbox2 will have 6 lines, instead of 14.
    Oh, and I'm using:

    Code:
     TextBox1.Text = Replace(TextBox1.Text, Chr(32), String.Empty) 'Space
    Just for the blanks.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Textbox line by line

    the line wraps in textboxes are dictated by 2 different things, and they are very different in actual functionality.

    A line in a textbox of course only wraps when the textbox is multiline.

    However a line wraps in a multiline textbox when

    1) a newline character (crlf) is appended to the text, as is the case when you hit enter to go to a new line
    2) your current line takes up more than the visible area of the textbox, and the wordwrap property is set to true (which is the default).

    So for what you are trying to do, you really want to separate your lines by actual newline characters. You could use environment.newline to get that via code.

    Also, if you do it that way, you can access the textbox's lines property to access each individual line. This doesn't work if you just have the lines wrapped because of the size of your textbox and you have wordwrap on, because its still technically just 1 long single line string being displayed over multiple lines visually.

    So to iterate over every line when you do it as I mentioned, is as simple as this:

    Code:
            For Each S As String In TextBox1.Lines
                   'do stuff with S
            Next

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Re: Textbox line by line

    Thank's for reply

    Well using this will only convert the first hex value:

    Code:
      Dim hexNumber As Integer
            Dim i As Integer
            For Each S As String In TextBox1.Lines
                hexNumber = Convert.ToInt32(TextBox1.Text.Substring(i, 2), 16)
                TextBox2.AppendText(Chr(hexNumber & vbCrLf))
            Next

  8. #8
    Junior Member
    Join Date
    Sep 2008
    Location
    Finland
    Posts
    20

    Re: Textbox line by line

    Use my previous reply:

    Code:
    For Each line As String In TextBox1.Lines
        For i = 0 To line.Length - 1 Step 2
          hexNumber = Convert.ToInt32(line.Substring(i, 2), 16)
          TextBox2.AppendText(Chr(hexNumber))
        Next i
        TextBox2.AppendText(vbCrLf)
     Next
    It loops
    1) lines in TextBox1 (multiline=True)
    2) for each line, it loops each two character (=1 hex char) pair
    and you may substitute TextBox2.AppendText(vbCrLf) with TextBox2.AppendText(Environment.Newline) which kleinma stated. It's preferred way over vbCrLf in .NET.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Textbox line by line

    i had to try.

    Code:
            Dim HexVals() As Byte = New Byte() {&H45, &H0, &H1, &H35, &H24, &HDC, &H0, _
                                                &H0, &H80, &H11, &H90, &H27, &HC0, &HA8, _
                                                &H1, &H66, &HC0, &HA8, &H1, &HFE, &HFA, _
                                                &HA1, &H56, &HF2, &H1, &H21, &HA3, &H7B, _
                                                &H64, &H31, &H3A, &H72, &H64, &H32, &H3A, _
                                                &H69, &H64, &H32, &H30, &H3A, &H1B, &H4F, _
                                                &H71, &H18, &H55, &HCC, &HE1, &HB8, &HE8, _
                                                &H6B, &H95, &H96, &H18, &HA0, &H66, &H91, _
                                                &HB8, &HF9, &H5C, &H37, &H35, &H3A, &H6E, _
                                                &H6F, &H64, &H65, &H73, &H32, &H30, &H38, _
                                                &H3A, &H1B, &H54, &HD9, &HEE, &H37, &H75, _
                                                &H5D, &HED, &HD6, &H0, &HC4, &HDF, &HC7, _
                                                &H1B, &HED, &H3C, &H6E, &H26, &H3, &H8A, _
                                                &H4D, &HCA, &H25, &H7B, &H4A, &H3D, &H1B, _
                                                &H54, &HAA, &H77, &H5F, &H9B, &HBF, &H17, _
                                                &H6E, &H37, &H73, &H99, &H7D, &HD6, &HAB, _
                                                &H37, &HB5, &HAF, &HA0, &H1B, &HC0, &HA8, _
                                                &H1, &HFE, &H49, &HCA, &H1B, &H53, &HB0, _
                                                &H12, &H25, &H92, &H6F, &H71, &H43, &HB5, _
                                                &HE5, &H9C, &HCD, &H4A, &HEE, &H8A, &H53, _
                                                &H6B, &HF, &HB6, &H55, &H31, &HFB, &HDA, _
                                                &H3E, &HAE, &H1B, &H53, &H46, &H48, &H9C, _
                                                &HC6, &H4A, &H6A, &HF6, &HBA, &HA1, &H4, _
                                                &H87, &H27, &H38, &HE3, &H3A, &H5C, &HF3, _
                                                &HEE, &H7D, &HE8, &H89, &H58, &H56, &HE7, _
                                                &H1B, &H53, &H6B, &HE7, &H7B, &HAB, &H1, _
                                                &H71, &H3E, &H1E, &H3E, &H97, &H94, &H90, _
                                                &HEA, &HD8, &H94, &H2A, &H76, &H82, &H5B, _
                                                &H43, &HD, &H33, &H62, &H2B, &H1B, &H52, _
                                                &H3F, &H46, &H19, &H4D, &H43, &HFE, &HB9, _
                                                &H64, &HB6, &H2B, &HA2, &H95, &HEC, &H18, _
                                                &H37, &H31, &HFF, &H8D, &H43, &HB7, &H88}
            Dim strr As Char() = New Char(HexVals.Length - 1) {}
            strr = System.Text.Encoding.GetEncoding("Windows-1252").GetChars(HexVals)
            Dim ts As String
            ts = New String(strr, 0, strr.Length - 1)
            Dim TBh As New TextBox, TBa As New TextBox
            Const perLnH As Integer = 16
            Dim ctHch As Integer = 0
            For z As Integer = 0 To ts.Length - 1
                TBh.Text &= HexVals(z).ToString("X").PadLeft(2, "0"c) & " "
                TBa.Text &= ts.Substring(z, 1) & " "
                ctHch += 1
                If ctHch >= perLnH Then
                    TBh.Text &= Environment.NewLine
                    TBa.Text &= Environment.NewLine
                    ctHch = 0
                End If
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Junior Member
    Join Date
    Sep 2008
    Location
    Finland
    Posts
    20

    Re: Textbox line by line

    One more modification (remove spaces):

    Code:
    Dim hex As String = TextBox1.Text
    Dim hexNumber As Integer
    Dim i As Integer
    
    For Each line As String In TextBox1.Lines
      line = Replace(line, Chr(32), String.Empty)
      For i = 0 To line.Length - 1 Step 2
        hexNumber = Convert.ToInt32(line.Substring(i, 2), 16)
        TextBox2.AppendText(Chr(hexNumber))
      Next i
      TextBox2.AppendText(vbCrLf)
    Next
    and this code works. But the point is, printable ASCII characters are in the range 32-127, and some of your sample hex values are outside of this range. The code above should check if the hex value is not a printable ASCII it outputs, lets say a dot (.) character. Like this:

    Code:
    Dim hex As String = TextBox1.Text
    Dim hexNumber As Integer
    Dim i As Integer
    
    For Each line As String In TextBox1.Lines
      line = Replace(line, Chr(32), String.Empty)
      For i = 0 To line.Length - 1 Step 2
        hexNumber = Convert.ToInt32(line.Substring(i, 2), 16)
        If hexNumber >= 32 AndAlso hexNumber <= 127 Then
          TextBox2.AppendText(Chr(hexNumber))
        Else
          TextBox2.AppendText(".")
        End If
      Next i
      TextBox2.AppendText(vbCrLf)
    Next
    If you copy and past this into textbox1 then textbox2 will have 6 lines, instead of 14.
    I copy pasted those 14 lines in to my test code and got out 14 lines. Check your line break characters. Some lines may have vbCr or vbLf instead of vbCrLf. If the original content comes from a file this may well be the case.

    Teme @ windevblog.blogspot.com

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Re: Textbox line by line

    Well your code work's. But let's take an other example, now I'm back to 6 lines??

    Code:
    45 00 05 AC 7F 8C 40 00 2C 06 7D E9 3F EC 49 DC 
    C0 A8 01 66 00 50 08 70 F5 B7 16 86 B7 FF C7 77 
    50 10 1F DA 68 E1 00 00 48 54 54 50 2F 31 2E 31 
    20 32 30 30 20 4F 4B 0D 0A 44 61 74 65 3A 20 53 
    75 6E 2C 20 32 38 20 53 65 70 20 32 30 30 38 20 
    32 30 3A 32 30 3A 32 30 20 47 4D 54 0D 0A 53 65 
    72 76 65 72 3A 20 41 70 61 63 68 65 0D 0A 45 78 
    70 69 72 65 73 3A 20 30 0D 0A 43 61 63 68 65 2D 
    43 6F 6E 74 72 6F 6C 3A 20 70 72 69 76 61 74 65 
    2C 20 70 6F 73 74 2D 63 68 65 63 6B 3D 30 2C 20 
    70 72 65 2D 63 68 65 63 6B 3D 30 2C 20 6D 61 78 
    2D 61 67 65 3D 30 0D 0A 50 72 61 67 6D 61 3A 20 
    6E 6F 2D 63 61 63 68 65 0D 0A 4B 65 65 70 2D 41 
    6C 69 76 65 3A 20 74 69 6D 65 6F 75 74 3D 35 2C
    How come?

  12. #12
    Junior Member
    Join Date
    Sep 2008
    Location
    Finland
    Posts
    20

    Re: Textbox line by line

    From that data, I get:
    Code:
    E....@.,.}.?.I.
    ...f.P.p.......w
    P...h...HTTP/1.1
     200 OK..Date: S
    un, 28 Sep 2008 
    20:20:20 GMT..Se
    rver: Apache..Ex
    pires: 0..Cache-
    Control: private
    , post-check=0, 
    pre-check=0, max
    -age=0..Pragma: 
    no-cache..Keep-A
    live: timeout=5,
    when non-printable ASCII chars were replaced with dot. And that's 14 lines.

    Are you sure your TextBox2 is a) MultiLine=True and b) ScrollBars=Vertical so that you can see all the lines?

    Another point, your hex sample contains six 0D 0A values (=CR+LF) which would explain why you got six lines. So, how do you fill TextBox1 in the first place? I got 14 lines because I copy pasted text from your post, not original binary data. Do you fill TextBox1:

    Code:
    Dim ByteArr() As Byte
    Dim i As Integer
    For i = 0 To ByteArr.Length - 1
      TextBox1.Text = TextBox1.Text & String.Format("{0:X2} ", ByteArr(i))
    Next i
    or with any similar code? Or do you "dump" binary data straight into TextBox1? (Ok, I admit I don't know how you could do that )

    Teme @ windevblog.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width