Results 1 to 6 of 6

Thread: Textbox text format

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    35

    Textbox text format

    When I copy a bunch of bytes out of a hex editor, the format is the following:
    Code:
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 etc...
    I want the format to be like the following:
    Code:
    00000000 00000000
    00000000 00000000
    00000000 00000000
    etc...
    What code should I use?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Textbox text format

    What do you mean by copy? Using the clipboard, redirecting output, some other esoteric method?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    35

    Re: Textbox text format

    In HxD, one side shows hexadecimal and the other shows ASCII. When I copy a bunch of bytes from the hex side, and paste them in a notepad for example, they're like in the post above.

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    35

    Re: Textbox text format

    Bump.

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Textbox text format

    Assuming you have your copied data assigned to a string variable, you could strip out all the white space (space characters and line terminator characters) from that string, and then build a new string by inserting a space after 8 characters, then a NewLine after another 8 characters and repeating till the end of the string.

    Using a StringBuilder for the sake of efficiency:

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.     Dim inputString As String = Clipboard.GetText
    4.  
    5.     '  Make sure TextBox1 is multiline and uses
    6.     '   a non proportional font such as Courier New
    7.     '   or Lucida Console
    8.     TextBox1.Text = ReformatHexString(inputString)
    9.  
    10. End Sub
    11.  
    12. Private Function ReformatHexString(input As String) As String
    13.     Dim outputBuilder As New System.Text.StringBuilder
    14.  
    15.     '  Get rid of spaces and NewLines
    16.     input = input.Replace(ControlChars.Cr, "").Replace(ControlChars.Lf, "").Replace(" ", "")
    17.  
    18.     ' Take 8 characters at a time
    19.     ' Make sure not to step past the end of the String
    20.     '   by using  Math.Min(8, input.Length - indx)
    21.     For indx = 0 To input.Length - 1 Step 8
    22.         outputBuilder.Append(input.Substring(indx, Math.Min(8, input.Length - indx)))
    23.  
    24.         '  Alternately add a Space and a NewLine
    25.         '   after every 8 characters added
    26.         If indx Mod 16 = 0 Then outputBuilder.Append(" ") Else outputBuilder.Append(Environment.NewLine)
    27.     Next
    28.  
    29.     '  Output as a String, losing any white space added
    30.     '   to the end in the above code, and
    31.     '   terminate with a NewLine
    32.     Return outputBuilder.ToString.TrimEnd & Environment.NewLine
    33. End Function
    Last edited by Inferrd; Feb 17th, 2013 at 10:16 AM.

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2013
    Posts
    35

    Re: Textbox text format

    Oh, man, you are a legend! Thank you so much!

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