none
Display byte array in a string RRS feed

  • Question

  • hey, ive been looking for this for a while but im new to C# and was wonderin how i can display byte's that i got by converting a string into a visual studio C# textbox? 
    Wednesday, September 24, 2014 4:24 PM

Answers

  • Hi,

    I you want to convert an array of bytes to string, you may use:

                byte[] bytes = { 1,2,3,4 };
    
                string StringByte= BitConverter.ToString(bytes);

    • Marked as answer by Ryan Mazer Wednesday, September 24, 2014 5:14 PM
    Wednesday, September 24, 2014 4:55 PM

All replies

  • Hi,

    I you want to convert an array of bytes to string, you may use:

                byte[] bytes = { 1,2,3,4 };
    
                string StringByte= BitConverter.ToString(bytes);

    • Marked as answer by Ryan Mazer Wednesday, September 24, 2014 5:14 PM
    Wednesday, September 24, 2014 4:55 PM
  • Grammar is important.  I think what you mean is

    how i can display [bytes that i got by converting a string] in a visual studio C# textbox?

    I'm not sure what you mean by "bytes that I got by converting a string".  But I'll assume you mean an array of bytes.

    First of all, to set the text of a textbox you would use:

    textBox1.Text = "Hello";


    To convert a byte array to a string of hex digits you might use something like this:

    byte[] myBytes = new byte[] { 72, 101, 108, 108, 111 };
    textBox1.Text = string.Join( " ", myBytes.Select( x => x.ToString( "X2" ) ) );

    This results in "48 65 6C 6C 6F"

    Or if you want to recover the string from an array of bytes you can use:

    textBox1.Text = Encoding.UTF8.GetString( myBytes );
    ...or whatever encoding you are using.

    Not sure which of these (if any) you are actually after.

    Wednesday, September 24, 2014 4:55 PM