Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how can i change this code in for loop

What I have tried:

private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
            return sb.ToString().ToUpper();

        }
Posted
Updated 4-Mar-17 3:53am
Comments
Graeme_Grant 4-Mar-17 9:36am    
I don't understand the question - there is already a for loop...
Yugal Kishor 4-Mar-17 9:45am    
i want to change this code. like

for (int i=0; i < 20; i++)
{
}
Yugal Kishor 4-Mar-17 9:46am    
i want to change this code. like

for (int i=0; i < 20; i++)
{
}
like this
PIEBALDconsult 4-Mar-17 9:37am    
With an editor I should think.
[no name] 4-Mar-17 9:44am    
How did you change it to begin with? We will need more to work with, please explain in detail what you want to achieve.

C#
private string ByteArrayToHexString(byte[] data)
     {
         StringBuilder sb = new StringBuilder(data.Length * 3);
         for (int i = 0; i < data.Length; i++)
         {
             sb.Append(Convert.ToString(data[i], 16).PadLeft(2, '0'));
         }
         return sb.ToString().ToUpper();
     }
 
Share this answer
 
private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            for(int i = 0; i < data.Length; i++)
                {
                sb.Append(Convert.ToString(data[i], 16).PadLeft(2, '0'));
                }
            return sb.ToString().ToUpper();
        }

Or just:
string hex = BitConverter.ToString(data);
 
Share this answer
 

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