Dim hex = "41"
Dim text As New System.Text.StringBuilder
For i As Integer = 0 To hex.Length - 2 Step 2
text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
Next
System.Diagnostics.Debug.WriteLine(text.ToString)
working perfectly.. System.Diagnostics.Debug.WriteLine writes the output itself in a new line.
Dim hex = "4100"
Dim text As New System.Text.StringBuilder
For i As Integer = 0 To hex.Length - 2 Step 2
text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
Next
System.Diagnostics.Debug.WriteLine(text.ToString)
but yet this fails.. (the output isn’t itself in a new line) what’s the explanation for that?
From what I know, doesn’t System.Diagnostics.Debug.Writeline does something which looks like this:
System.Diagnostics.Debug.Write(input)
System.Diagnostics.Debug.Write("\n")
so regardless of my input it should always have a newline char even if there is a terminating 00 char in my input?
Here is the details describing what is going on here:
The
stringclass in .net is an array ofcharstart by pointer to the firstcharin the array and ends by the special “terminal” char\0.When you convert the
00to byte you get0. but0is just equals the terminal char\0So the
Console.Writewill ignore all characters after the first\0!. Not only the console will behive this way but also the control components. for instancetextBox.Textif you apply a string to it that does have the\0character, it will not will not display any character after\0“including the\0itself”.Edit:
I guesses that the
Debug.WriteLineare not implemented in this way or else we will not notice that behavior. maybe it does concatenate the string with new line likeinput + "\n".I can diagnosing the
Console.WriteLineusingResharperprogram. the code is:So it is concatenate the string value with the new line. so it will cause the same effect.