Using Visual Studio, Text Box vs RichTextBox, same string, different results

Devon Nullman 60 Reputation points
2026-01-08T17:11:19.9933333+00:00

Using SHA512 to get the hash of two byte arrays

Actual Results are:

­­Original Data Hash  = 321F74804533BAC063E5A13E6FCE9309DB1C6C24BC66B1A0AEFFACE43D7F66EF986F90838C72DF62DF297973C6C753B291F8B0BD65E6EBDC11776D045A05B164
Decrypted Data Hash = 321F74804533BAC063E5A13E6FCE9309DB1C6C24BC66B1A0AEFFACE43D7F66EF986F90838C72DF62DF297973C6C753B291F8B0BD65E6EBDC11776D045A05B164

If Displayed in a RichTextBox:

­­Original Data Hash  = 321F74804533BAC063E5A13E6FCE9309DB1C6C24BC66B1A0AEFFACE43D7F66EF986F90838C72DF62DF297973C6C753B291F8B0BD65E6EBDC11776D045A05B164
Decrypted Data Hash = 321F74804533BAC063E5A13E6FCE9309DB1C6C24BC66B1A0AEFFACE43D7F66EF986F90838C72DF62DF297973C6C753B291F8B0BD65E6EBDC11776D045A05B164

If Displayed in a TextBox:

--­­Original Data Hash  = 321F74804533BAC063E5A13E6FCE9309DB1C6C24BC66B1A0AEFFACE43D7F66EF986F90838C72DF62DF297973C6C753B291F8B0BD65E6EBDC11776D045A05B164
Decrypted Data Hash = 321F74804533BAC063E5A13E6FCE9309DB1C6C24BC66B1A0AEFFACE43D7F66EF986F90838C72DF62DF297973C6C753B291F8B0BD65E6EBDC11776D045A05B164

Note the two "dashes" before the word "Original", they are character 0xAD, not dashes

0xAD is the hexadecimal code for the Unicode character Soft Hyphen (SHY), a formatting mark that's invisible unless a line break occurs, signaling a preferred hyphenation point for words. Huh?

Where it is coming from I do not know:

Hash Calculator Code:
    Public Function CreateSHA512(Bytes As Byte()) As String
        Dim sOutput As New StringBuilder()
        Using S512 As SHA512 = SHA512.Create()
            Dim B() As Byte = S512.ComputeHash(Bytes)
            For i As Integer = 0 To B.Length - 1
                sOutput.Append(B(i).ToString("X2"))
            Next
            Return sOutput.ToString()
        End Using
    End Function
Windows development | Windows App SDK
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 10,475 Reputation points Microsoft External Staff Moderator
    2026-01-09T07:23:33.1466667+00:00

    Hello @Devon Nullman ,

    I did try to manually insert a soft hyphen and display it in both controls, and I observed the same behavior.

    User's image

    It seems like the soft hypen character is being introduced into your strings somewhere outside of your hash function.

    For example, if somewhere in your code you have something like:

    result = "Original Data Hash  = " & CreateSHA512(bytes)
    

    And that string literal "Original Data Hash = " was copied from Word or another formatted source, it would contain the hidden 0xAD characters.

    Also, about why you're seeing different behavior in different controls, you should check this document about complex script processing, but in summary:

    • RichEdit Control: Follows proper Unicode rendering rules and hides soft hyphens. They're invisible unless a line break occurs at that position, which is the correct behavior according to the Unicode specification.
    • Standard TextBox (Edit Control): Has basic Unicode support and attempts to render soft hyphens as visible characters, which is why you're seeing them as dashes.

    So ensure that all your string literals are clean and free of hidden formatting characters. You can do this by retyping them manually or using a plain text editor to strip out any unwanted characters.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.