We help IT Professionals succeed at work.

Converting Byte Value to ASCII printable character

Hi All,
I need a method that converts a Byte to a printable character.
I know this seems stupid, I just can't find a method that does this.

I have an array of bytes:

Dim ByteArray() as Byte = { &H48, &H54, &H54, &H50 }

String.Format("{0}", ByteArray(0).ToString)

When I use the ByteArray(0).ToString function, I get the decimal value printed instead of the actual character like "H".


Comment
Watch Question

Senior Software Engineer
Commented:
try this one:

        Dim ByteArray() As Byte = {&H48, &H54, &H54, &H50}
        Dim value As String
        For Each i In ByteArray
            value = Convert.ToChar(i).ToString()
        Next

You will be getting values in value
Most Valuable Expert 2011
Top Expert 2015
Commented:
Use:
Convert.ToChar(ByteArray(0))  ' For single char

System.Text.Encoding.ASCII.GetString(ByteArray)  ' For whole string

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Another one:


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ByteArray() As Byte = {&H48, &H54, &H54, &H50}
        Dim str As String = GetString(ByteArray)
        MessageBox.Show(str)
    End Sub

    Private Function GetString(ByVal bytes() As Byte) As String
        Dim chars As New System.Text.StringBuilder
        For Each b As Byte In bytes
            chars.Append(Chr(b))
        Next
        Return chars.ToString
    End Function
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
*I like kaufmed's answer better though.
Thx so much.  All very good and usable suggestions.