-1

Am very (very) new to C#, I would really appreciate your assistance to code in C# a program that allows me to calculate the sum of two ASCII characters in their numeric equivalence, any ASCII character. I have tried declaring it as an int: int A = 60,etc. Also:

    char a = 'A';
    int i = (int) a;
    Console.WriteLine("{0}", i);

I have two days trying to do it, but my brain is not working. Thanks!!!!

Flores
  • 8,226
  • 5
  • 49
  • 81
Ro Conde
  • 11
  • 3
  • 2
    Please post what you have tried so we have some idea what you're stuck on. – Dour High Arch Jan 24 '18 at 21:02
  • Have you tried casting to into and using char.GetNumericValue(somevarhere) Ex on this thread - https://stackoverflow.com/questions/3665757/how-to-convert-char-to-int – trebleCode Jan 24 '18 at 21:05
  • thanks, the deal is that not only 'A' and 'F' would be used, but any letter, and the program calculates their numeric equivalence – Ro Conde Jan 24 '18 at 21:49
  • C# doesn't use ASCII. Not many languages you would encounter do. `char` is a UTF-16 code unit, one or two are the encoding for a [Unicode](http://www.unicode.org/charts/nameslist/index.html) codepoint. (Two UTF-16 code units would be stored in a `String`. For example, `""` is two, as is `""`.) BTW—What does character code addition mean to you? – Tom Blodget Jan 25 '18 at 01:57
  • Hi, what am being asked to do is to assign the numeric value to the ASCII character, then the program will ask to enter two letters and depending of the letter, add the numeric value, e.g. A = 60, B= 61, C= 62, etc the program will ask enter any letter... enter any other letter... and then add the numeric values of those letters and get the result – Ro Conde Jan 25 '18 at 13:51
  • If the exercise allows, you could just comment that although ASCII letters are expected, you are omitting input validation and relying on ASCII having the same character codes as Unicode codepoints, and that UTF-16 encodes codepoints in that range in one `char` each, so given those assumptions arithmetic on ASCII bytes is the same as arithmetic on C# `char`. Otherwise, see @jonathana's [answer](https://stackoverflow.com/a/48431728/2226988). – Tom Blodget Jan 27 '18 at 15:33

4 Answers4

2

You can add the characters and assign them to an integer

char char1 = 'A';
char char2 = 'F';

int value = char1 + char2;
GodsCrimeScene
  • 1,260
  • 2
  • 17
  • 35
1

If they are char types just add them together:

static int Add(char a, char b)
{
    var c = a + b;
    return c;
}

If they are string types you can use the following code (after you imported the "System.Linq" namespace) (updated with ckuri's suggestion):

static int Add(string a, string b)
{
    var c = (a + b).Select(f=>(int)f).Sum();
    return c;
}

Demo: https://dotnetfiddle.net/qbVWIP

Wyns
  • 321
  • 1
  • 5
  • 1
    String already implements `IEnumerable`, therefore it can be shortened to `(a + b).Select(f=>(int)f).Sum();`. – ckuri Jan 24 '18 at 21:35
  • Thanks for pointing that out, you're absolutely right. I updated the answer with it. – Wyns Jan 24 '18 at 21:42
1
int sumOfAsciiCodes = Convert.ToInt32('A') + Convert.ToInt32('F');
fdafadf
  • 809
  • 5
  • 13
  • You don't need `Convert.ToInt32` as char and int have implicit conversions with each other. – ckuri Jan 24 '18 at 21:30
1

You can create a reusable method (GetAscii()) for that task:

private void Init()
{ 
    var result = GetAscii("AF");
    int sum = 0;

    foreach (var itm in result)
    {
       // show each ascii value
       Console.WriteLine(itm.ToString());
        sum += (int)itm;
    }
    // sum all values
    Console.WriteLine(sum.ToString());
}

private byte[] GetAscii(string value)
{
    byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
    return asciiBytes; 
}
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52