2

I am reading a text file that and storing it to an array. During the process of creating the array I call for the character key and the frequency count. I need to be able to get the Ascii value of each character that is read from the file.

I have tried byte[] asciiBytes = Encoding.ASCII.GetBytes(count); but I can't use count as it can't be converted from Characterfrequency()

class CharacterFrequency
    {
        public int Frequency { get; set; }
        public char Char { get; set; }
        public byte Ascii { get; set; }


        public override string ToString()
        {
            return $"Character: {Char} Frequency: {Frequency}";
        }

Below is the class that loops through the file.

class Program : CharacterFrequency
    {
        static void Main(string[] args)
        {
            var count = File.ReadAllText("wap.txt")
                .ToCharArray()
                .GroupBy(x => x)
                .Select(x => new CharacterFrequency() { Char = x.Key, Frequency = x.Count() 
        })
                .ToArray();


            foreach (var item in count)

                Console.WriteLine($"{item.Char} {item.Frequency}");

            Console.ReadLine();

In the foreach it should loop through and print out the the data on each line like: Character: A(65) Frequency: 250

  • You're reading in a string, so just use https://stackoverflow.com/questions/31863204/simple-solution-for-characters-frequency-in-string-object – Yuriy Faktorovich Jan 23 '19 at 15:55
  • Do you really mean ASCII? What about `ö`? What do you want to happen if that appears in the file for example? – nvoigt Jan 23 '19 at 16:00
  • When I ran the code special characters such as: é(233) and ä(228) both had an ascii value appear for them. – Dominik Willaford Jan 23 '19 at 16:02
  • Technically, C# characters use UNICODE, not ASCII. – Merak Marey Jan 23 '19 at 16:03
  • Again , read https://stackoverflow.com/questions/5002909/getting-the-ascii-value-of-a-character-in-a-c-sharp-string – Merak Marey Jan 23 '19 at 16:04
  • @DominikWillaford ASCII only goes to 127, so you could not have an ASCII value for `ä`. That's a UNICODE value. – nvoigt Jan 23 '19 at 16:10
  • @DominikWillaford You must know the character encoding of the file. ASCII is quite rare for text files. Then you can deal with assumptions about which characters the file is expected to contain, such as only the [C0 Controls and Basic Latin](http://www.unicode.org/charts/nameslist/index.html) block of the Unicode character set. – Tom Blodget Jan 23 '19 at 17:40
  • That is great to know for future use. I had to check for ascii character values for this assignment as instructed by my professor. – Dominik Willaford Jan 24 '19 at 00:39

3 Answers3

1

To my understanding the ASCII value can be obtained directly from the char representation as follows.

Select(x => new CharacterFrequency()
                {
                  Char = x.Key,
                  Frequency = x.Count(),
                  Ascii = (byte)x.Key
                }
)
Codor
  • 17,447
  • 9
  • 29
  • 56
  • 1
    Are Ascii bytes? :) – Merak Marey Jan 23 '19 at 15:59
  • Thank you I was close just forgot to cast it to a byte – Dominik Willaford Jan 23 '19 at 15:59
  • Please read https://stackoverflow.com/questions/5002909/getting-the-ascii-value-of-a-character-in-a-c-sharp-string – Merak Marey Jan 23 '19 at 16:02
  • Suggestion based on the concern if this code support UNICODE, C# actually uses UNICODE, not ASCII.. – Merak Marey Jan 23 '19 at 16:05
  • Yes, it's a pretty long story to get from UTF-16 code units to ASCII codepoints. But, if it is acceptable to ignore that a UTF-16 code unit might only be a part of a codepoint or of a grapheme cluster, that codepoints in a grapheme cluster might be composed or decomposed, and that the characters in question might not even be in the ASCII character set, a code comment they are numerically equal would suffice. – Tom Blodget Jan 23 '19 at 17:35
0

You can simply cast your Char to an int inline

foreach (var item in count)
    Console.WriteLine($"{item.Char}({(int)item.Char}) {item.Frequency}");

Using the input "Hello World" I get the result

H(72) 1
e(101) 1
l(108) 3
o(111) 2
 (32) 1
W(87) 1
r(114) 1
d(100) 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

I think the best option is something like this:

 foreach(byte b in System.Text.Encoding.UTF8.GetBytes(count.ToCharArray()))
 {
       Console.WriteLine(b.ToString());
 }

Credits here.

Mikev
  • 2,012
  • 1
  • 15
  • 27