0

I was facing a problem where I got data (String) from a database with Linebreaks as (Hex) 0D. I displayed this data in a Textbox, which did not use the 0D as a linbreak. I found that the Textbox needs 0D-0A (LF & CR, dont know which is which) to actually show the new line. To solve this problem I came up with the following code.

Private Function convertString(txt As String) As String
    Dim data = System.Text.Encoding.Default.GetBytes(txt)
    Dim hexString As String = BitConverter.ToString(data)
    hexString = hexString.Replace("0D", "0D-0A")
    Dim arr As [String]() = hexString.Split("-"c)
    Dim array As Byte() = New Byte(arr.Length - 1) {}
    For i As Integer = 0 To arr.Length - 1
        array(i) = Convert.ToByte(arr(i), 16)
    Next
    Return System.Text.Encoding.Default.GetString(array)
End Function

Explanation/procedure:
1. Convert String to ByteArray
2. Convert ByteArray to Hex-String (Hex-Chars separated by '-' )
3. Adding the missing lf or cr by replacing the solo one
4. Convert Hex-String back to ByteArray
5. Convert ByteArray back to String

Now my question:
I am pretty sure there is a better way to do that. How can I simplify those lines of code?

Luke
  • 751
  • 2
  • 7
  • 32
  • 2
    The whole hex string thing is probably confusing the issue... how about `Return txt.Replace(vbCr, vbCrLf)`? – Mark May 05 '17 at 14:30
  • Can't tell. Tried a few combinations of replace vbcr vblf vbcrlf. Maybe I missed exactly this one. According to wikipedia you are probably right tho... – Luke May 05 '17 at 14:42
  • And `Environment.NewLine` is what you should be using since it is platform independent. – djv May 05 '17 at 14:48
  • Since WinForms is Windows only anyways, how is Environment.Newline different? Windows will Probably always work or at least be able to work with CR & LF.. // Should've mentioned Winforms earlier tho – Luke May 05 '17 at 14:51
  • @djv But is `TextBox` platform independent (wondering about WinForms in Mono)? – Mark May 05 '17 at 14:52
  • Great answer [here](http://stackoverflow.com/a/27225418/832052). *"...there are historical reasons for the existence of [`vbCr`, `vbLf`, and `vbCrLf`], which are now often irrelevant: and perhaps the best course of action in .NET is to use `Environment.NewLine` which means someone else has decided for you which to use, and future portability issues should be reduced."* – djv May 05 '17 at 15:00
  • @djv While using `Environment.NetLine` is probably best for most situations, that isn't universally true... e.g. the [HTTP specs](https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2) require CRLF line endings. WinForms on mono may have been coded to match Windows and expect CRLF line ending, or maybe not, which would determine if using `Environment.NewLine` would work in this situation, so unfortunately it's not as simple as saying you should always use it. :-) – Mark May 07 '17 at 15:05
  • `...the Textbox needs 0D-0A...` When displaying strings to the user, based on platform, Environment.NewLine is the way to go. Of course, there are innumerable protocols which call for different line termination character(s) which are outside the scope of this question, unless op specifies it. Environment.NewLine is also a consistent approach between c# and vb.net, otherwise you are using `\r\n` and `vbCrLf` which is really just one of those VB6 remnants... Both ways work 99% of the time. I just think [this](http://stackoverflow.com/a/3986129/832052) simplifies things. – djv May 09 '17 at 14:04

1 Answers1

1

You should be able to just Replace vbCr with vbCrLf:

Dim txt = "This is a" & vbCr & "test"
Encoding.UTF8.GetBytes(txt).HexDump()

gives (HexDump is a custom utility method, but not relevant to the question):

00000000   54 68 69 73 20 69 73 20  61 0D 74 65 73 74         This is a·test  
Dim txt2 = txt.Replace(vbCr, vbCrLf)
Encoding.UTF8.GetBytes(txt2).HexDump()

gives:

00000000   54 68 69 73 20 69 73 20  61 0D 0A 74 65 73 74      This is a··test 

So, your whole method would be:

Private Function convertString(txt As String) As String
    Return txt.Replace(vbCr, vbCrLf)
End Function
Mark
  • 8,140
  • 1
  • 14
  • 29