Wednesday, May 12, 2021

C# Char class Examples


using System;

namespace Console_GetASCIIValuesofChars

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("First Approach: Char.ConvertToUtf32 & Char.ConvertFromUtf32");

            string alphabets = "The quick brown fox jumps over the lazy dog";

            for (int i = 0; i < alphabets.Length; i++)

            {

                Int32 x = Char.ConvertToUtf32(alphabets, i);

                Console.Write(x);

                Console.Write(" integer represents ");

                Console.Write(Char.ConvertFromUtf32(x));

                Console.WriteLine();

            }

            Console.WriteLine("\nSecond Approach : Typecast");

            string quote = "He who has a why to live can bear almost any how.";

            char[] chars = quote.ToCharArray();

            foreach (var c in chars)

            {

                Console.WriteLine(c+" "+ (int)c);

            }

            Console.ReadKey();

        }      

    }

}

OUTPUT:

First Approach: Char.ConvertToUtf32 & Char.ConvertFromUtf32

84 integer represents T

104 integer represents h

101 integer represents e

32 integer represents

113 integer represents q

117 integer represents u

105 integer represents i

99 integer represents c

107 integer represents k

32 integer represents

98 integer represents b

114 integer represents r

111 integer represents o

119 integer represents w

110 integer represents n

32 integer represents

102 integer represents f

111 integer represents o

120 integer represents x

32 integer represents

106 integer represents j

117 integer represents u

109 integer represents m

112 integer represents p

115 integer represents s

32 integer represents

111 integer represents o

118 integer represents v

101 integer represents e

114 integer represents r

32 integer represents

116 integer represents t

104 integer represents h

101 integer represents e

32 integer represents

108 integer represents l

97 integer represents a

122 integer represents z

121 integer represents y

32 integer represents

100 integer represents d

111 integer represents o

103 integer represents g

Second Approach : Typecast

H 72

e 101

  32

w 119

h 104

o 111

  32

h 104

a 97

s 115

  32

a 97

  32

w 119

h 104

y 121

  32

t 116

o 111

  32

l 108

i 105

v 118

e 101

  32

c 99

a 97

n 110

  32

b 98

e 101

a 97

r 114

  32

a 97

l 108

m 109

o 111

s 115

t 116

  32

a 97

n 110

y 121

  32

h 104

o 111

w 119

. 46



No comments:

Post a Comment

Hot Topics