Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How convert Byte[] Array to hexadecimal string in C#

8 Answers

0 votes
using System;

class Program
{
    static void Main()
    {
        try {
            byte[] barr = {71,  73,  70,  56,  57,  97,  8,  32,  10,  32, 112,  32,  32};
 
            string hex = BitConverter.ToString(barr);
            hex = hex.Replace("-", " ");
            
            Console.WriteLine(hex.ToString());
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


/*
run:

47 49 46 38 39 61 08 20 0A 20 70 20 20

*/



answered Mar 17, 2015 by avibootz
edited Jun 21, 2025 by avibootz
0 votes
using System;
using System.Runtime.Remoting.Metadata.W3cXsd2001;

class Program
{
    static void Main()
    {
        try {
            byte[] barr = {71,  73,  70,  56,  57,  97,  8,  32,  10,  32, 112,  32,  32};
 
            SoapHexBinary shb = new SoapHexBinary(barr);
 
            Console.WriteLine(shb.ToString());
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


/*
run:

47494638396108200A20702020

*/


answered Mar 18, 2015 by avibootz
edited Jun 21, 2025 by avibootz
0 votes
using System;
using System.Runtime.Remoting.Metadata.W3cXsd2001;

class Program
{
    static void Main()
    {
        try {
            byte[] barr = {71,  73,  70,  56,  57,  97,  8,  32,  10,  32, 112,  32,  32};
 
            SoapHexBinary shb = new SoapHexBinary(barr);
 
            string hex = shb.ToString();
            for (int i = 2; i < hex.Length; i += 2) {
                hex = hex.Insert(i, " ");
                i++; // for the space " " we add in the code line above
            }
                 
            Console.WriteLine(hex);
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


/*
run:

47 49 46 38 39 61 08 20 0A 20 70 20 20

*/



answered Mar 18, 2015 by avibootz
edited Jun 21, 2025 by avibootz
0 votes
using System;

class Program
{
    static void Main()
    {
        try {
            byte[] barr = {71,  73,  70,  56,  57,  97,  8,  32,  10,  32, 112,  32,  32};
 
            Console.WriteLine(BitConverter.ToString(barr));
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


/*
run:

47-49-46-38-39-61-08-20-0A-20-70-20-20

*/


answered Mar 18, 2015 by avibootz
edited Jun 21, 2025 by avibootz
0 votes
using System;
using System.Text;

class Program
{
    static void Main()
    {
        try {
            byte[] barr = {71,  73,  70,  56,  57,  97,  8,  32,  10,  32, 112,  32,  32};
 
            StringBuilder hex = new StringBuilder(barr.Length * 2);
 
            for (int i = 0; i < barr.Length; i++) {
                hex.Append(barr[i].ToString("X2") + " ");
            }
 
            Console.WriteLine(hex);
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


/*
run:

47 49 46 38 39 61 08 20 0A 20 70 20 20 

*/



answered Mar 18, 2015 by avibootz
edited Jun 21, 2025 by avibootz
0 votes
using System;
  
class Program
{
    static void Main() {
        byte[] bArray = { 8, 16, 32, 64, 128 };
        
        string s = BitConverter.ToString(bArray).Replace("-", string.Empty);
          
        Console.Write(s);
    }
}
  
  
/*
run:
  
0810204080
  
*/

 



answered Jun 21, 2025 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        byte[] bytes = { 0, 1, 2, 3, 4, 5, 10, 20, 254, 255 };
        
        string hexString = BitConverter.ToString(bytes).Replace("-"," ");;
 
        Console.WriteLine(hexString);
    }
}


/*
run:

00 01 02 03 04 05 0A 14 FE FF

*/

 



answered Jun 21, 2025 by avibootz
0 votes
using System;
using System.Text;
  
class Program
{
    public static string ByteArrayToString(byte[] ba) {
        StringBuilder hex = new StringBuilder(ba.Length * 2);
       
        foreach (byte b in ba) {
            hex.AppendFormat("{0:X2} ", b);
        }
       
        return hex.ToString();
    }
     
    static void Main() {
        byte[] bytes = { 0, 1, 2, 3, 4, 5, 10, 20, 254, 255 };
          
        string hexString = ByteArrayToString(bytes);
   
        Console.WriteLine(hexString);
    }
}

  
/*
run:
  
00 01 02 03 04 05 0A 14 FE FF 
  
*/

 



answered Jun 21, 2025 by avibootz

Related questions

2 answers 352 views
1 answer 200 views
3 answers 219 views
219 views asked Oct 28, 2016 by avibootz
1 answer 111 views
1 answer 109 views
109 views asked Jul 24, 2022 by avibootz
1 answer 99 views
1 answer 127 views
127 views asked Aug 25, 2021 by avibootz
...