0

How do I convert numbers to its equivalent alphabet character and convert alphabet character to its numeric values from a string (except 0, 0 should stay 0 for obvious reasons)

So basically if there is a string

string content="D93AK0F5I";

How can I convert it to ?

string new_content="4IC11106E9";
Richardissimo
  • 5,596
  • 2
  • 18
  • 36
Bumba
  • 321
  • 3
  • 13
  • 1
    You can do this by writing code. Did you try that? What issue you are facing in that ? – Chetan Mar 11 '18 at 07:04
  • Using ASCII values , you can do it easily. Please refer ASCII topic http://sticksandstones.kstrom.com/appen.html once and have a look at https://stackoverflow.com/questions/4648781/how-to-get-character-for-a-given-ascii-value – Dot Net developer Mar 11 '18 at 07:09
  • @ChetanRanpariya I know I can convert alpha to number using e.x. `int index = char.ToUpper("K") - 64;` but how do I do the reverse of that? – Bumba Mar 11 '18 at 07:09
  • 2
    You're going to have problems converting back since there are 26 letters in the alphabet and only 10 digits. In your example, `K` translates to `11` - but you can't be sure in the translation back if it should be translated to `K` or to `AA`. – Zohar Peled Mar 11 '18 at 07:26
  • And anyway, this seems like an [XYPropblem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. What is the actual problem you are trying to solve? – Zohar Peled Mar 11 '18 at 07:29

3 Answers3

2

I'm assuming you're aware this is not reversible, and that you're only using upper case and digits. Here you go...

    private string Transpose(string input)
    {
        StringBuilder result = new StringBuilder();
        foreach (var character in input)
        {
            if (character == '0')
            {
                result.Append(character);
            }
            else if (character >= '1' && character <= '9')
            {
                int offset = character - '1';
                char replacement = (char)('A' + offset);
                result.Append(replacement);
            }
            else if (character >= 'A' && character <= 'Z') // I'm assuming upper case only; feel free to duplicate for lower case
            {
                int offset = character - 'A' + 1;
                result.Append(offset);
            }
            else
            {
                throw new ApplicationException($"Unexpected character: {character}");
            }
        }

        return result.ToString();
    }
Richardissimo
  • 5,596
  • 2
  • 18
  • 36
1

Well, if you are only going to need a one way translation, here is quite a simple way to do it, using linq:

string convert(string input)
    {
        var chars = "0abcdefghijklmnopqrstuvwxyz";
        return string.Join("", 
                           input.Select(
                               c => char.IsDigit(c) ? 
                               chars[int.Parse(c.ToString())].ToString() : 
                               (chars.IndexOf(char.ToLowerInvariant(c))).ToString())
                           );
    }

You can see a live demo on rextester.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

You can use ArrayList of Albhabets. For example

ArrayList albhabets = new ArrayList();
            albhabets.Add("A");
            albhabets.Add("B");

and so on.

And now parse your string character by character.

string s = "1BC34D";
char[] characters = s.ToCharArray();
for (int i = 0; i < characters.Length; i++)
 {
   if (Char.IsNumber(characters[0]))
    {
        var index = characters[0];
        var stringAlbhabet = albhabets[index];
    }
  else
    {
        var digitCharacter = albhabets.IndexOf(characters[0]);
    }
}

This way you can get "Alphabet" representation of number & numeric representation of "Alphabet".

Brijesh
  • 369
  • 2
  • 10