none
C# A Reverse Range ASCII Algorithm Method RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApplication10
    {
    class Program
    {
    public static void Main(string[] args)
    {
     
    Console.Write("Geben Sie bitte eine zu verschlüsselnde Nummer ein: ");
    string newWords = Console.ReadLine();
    string newWords2 = newWords.ToUpper();
    Console.Write("Welchen Abstand möchten Sie? ");
    int num1 = Convert.ToInt32(Console.ReadLine());
    decimal choice = Convert.ToDecimal(num1);
    Console.Write("Wählen Sie Zählen vorwärts (1) oder rückwärts zählen (2) ");
    string opt = Console.ReadLine();
    newWords2 = newWords2.Replace("Ö", "OE");
    newWords2 = newWords2.Replace("Ä", "AE");
    newWords2 = newWords2.Replace("Ü", "UE");
    newWords2 = newWords2.Replace("ß", "SS");
    newWords2 = newWords2.Replace(" ", " ");
     
     
    Char[] arrayNewWords = newWords2.ToCharArray();
     
     
    foreach (char Newchar in arrayNewWords)
    {
    decimal a = Convertion(Newchar, choice, opt);
    int b = Convert.ToInt32(a);
    char c = Convert.ToChar(b);
    string result = c.ToString();
    // Console.Write(value);
    Console.Write(result);
    }
    Console.ReadLine();
    }
     
     
    public static decimal Convertion(char Newchar, decimal choice, string opt)
    {
    decimal NewcharDecimal = Convert.ToInt32(Newchar);
    decimal TotalTotal = 0;
    if (opt == "1")
    {
    decimal TotalValuePlus = NewcharDecimal + choice;
    while ((TotalValuePlus > 90) || (TotalValuePlus < 64))
    {
    if (TotalValuePlus >= 90)
    {
    TotalValuePlus -= 90;
     
    }
    else if (TotalValuePlus <= 64)
    {
    TotalValuePlus += 64;
    }
    }
    if ((TotalValuePlus <= 90) && (TotalValuePlus >= 65))
    {
    TotalTotal = TotalValuePlus;
    // return TotalTotal;
    }
    }
    else if (opt == "2")
    {
    decimal TotalValueMinus = NewcharDecimal - choice;
    while ((TotalValueMinus > 90) || (TotalValueMinus < 64))
    {
    if (TotalValueMinus >= 90)
    {
    TotalValueMinus += 90;
     
    }
    else if (TotalValueMinus <= 64)
    {
    TotalValueMinus -= 64;
    }
    }
    if ((TotalValueMinus <= 90) && (TotalValueMinus >= 65))
    {
    TotalTotal = TotalValueMinus;
    // return TotalTotal;
    }
    }
    return TotalTotal;
    }
    }
    }

    the BOLD words is the IF ELSE method needed to be Change.

    I need help for a Algorithm method reversing from the range 65 =A to 90 = Z with a variable adjuster which tells that the program must count backwards in the ascii table Table. 

    for example in my head when i calculate A CHAR = 65 DEC and i insert the number - 3 DEC the answer should be X CHAR= 88 DEC in the ascii table Table. 

    Does someone know how to figure this out or what is the algorithm for that?

    All help is much appreciated.. THANK YOU


    Wednesday, February 8, 2017 5:56 PM

All replies

  • Ouch my brain!  I think you are just trying to get the letters to "wrap around" after applying an offset.

    To do that, you need the letter's index mod 26.

    // first convert the letter to an index from 0 to 25.
    int letterIndex = Newchar - (int)'A';
    // then add the offset (which you have called choice)
    int newLetterIndex = letterIndex + choice;
    // and take the modulus, (one way is to use % twice and add N in between to make it non-negative)
    int adjustedIndex = ((newLetterIndex % 26) + 26) % 26;
    // then add the index to 'A' to get the character.
    char result = 'A' + adjustedIndex;

    and if you just want the ASCII code at this point, that's (int)result

    • Proposed as answer by Kristin Xie Wednesday, February 22, 2017 1:25 AM
    Thursday, February 9, 2017 9:34 PM
  • FYI, you are abusing the decimal type.  It's for things like currency.
    Thursday, February 9, 2017 9:35 PM