Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
If I have a byte[] array and I want to display it in a textbox like 0x3130353030353032, how would I do this? I know how to get it's actual value which is "10500502". but I want to know how to take the C# byte[] object and display it as 0x3130353030353032 basically the same way that SQL does it in a query window.

Thanks
Posted

C#
myTextBox.Text="0x";
foreach(byte b in array)
{
  myTextBox.Text += b.ToString("X2");
}


If you array is relatively long, you may consider using StringBuilder::Append method[^], instead of the String '+=' operator for better performance.
 
Share this answer
 
v2
It might be helpful,


C#
class Program
{
    static void Main(string[] args)
    {
        byte[] myBytes = Encoding.ASCII.GetBytes("Hello World");
        Console.WriteLine(GetString(myBytes));
    }
    private static String GetString(byte[] myBytes)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("0x");
        Array.ForEach(myBytes, item => builder.Append(item.ToString()));
        return builder.ToString();
    }
}


:)
 
Share this answer
 
Comments
Gordon Beeming 11-Aug-11 7:20am    
the only thing missing from your solution is the ToString("X2") instead of the plain ToString(), thank you though :)
Mohammad A Rahman 11-Aug-11 7:31am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900