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.

40,026 questions

51,982 answers

573 users

How to print the hex value of each character in a string in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string s = "Let's StartUP";
                char[] char_arr = s.ToCharArray();

                for (int i = 0; i < char_arr.Length; i++)
                {
                    int n = Convert.ToInt32(char_arr[i]);
                    string hex = String.Format("{0:X}", n);
                    
                    Console.WriteLine("char_arr[{0}] = {1} hex = {2}", i, char_arr[i], hex);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
  
char_arr[0] = L hex = 4C
char_arr[1] = e hex = 65
char_arr[2] = t hex = 74
char_arr[3] = ' hex = 27
char_arr[4] = s hex = 73
char_arr[5] =   hex = 20
char_arr[6] = S hex = 53
char_arr[7] = t hex = 74
char_arr[8] = a hex = 61
char_arr[9] = r hex = 72
char_arr[10] = t hex = 74
char_arr[11] = U hex = 55
char_arr[12] = P hex = 50
 
*/


answered Mar 26, 2015 by avibootz

Related questions

1 answer 155 views
1 answer 88 views
1 answer 78 views
78 views asked Jan 3, 2025 by avibootz
1 answer 148 views
1 answer 157 views
1 answer 149 views
...