1

I'm trying to convert String to ASCII code, so i use this function :

        public List<string> PrimaryCode(string OrginalStr)
        {
            List<string> lstResult = new List<string>();
            int lenOrginal = OrginalStr.Length;
            string subOrginalStr;
            byte[] AsciisubOrginalStr;
            int AC;

            for (int i = 0; i < lenOrginal; i++)
            {
                subOrginalStr = OrginalStr.Substring(i, 1);
                AsciisubOrginalStr = Encoding.ASCII.GetBytes(subOrginalStr);
                if (AsciisubOrginalStr[0] > 100)
                {
                    AC = Convert.ToInt32(AsciisubOrginalStr[0]);
                    lstResult.Add((AC ).ToString());
                }
                else
                {
                    AC = Convert.ToInt32(AsciisubOrginalStr[0]);
                    lstResult.Add((AC).ToString());
                }
            }
            return lstResult;
        }

The other part of my project i need to convert the ASCII code to original text as you can see i use this function :

   public List<string> PrimaryCodeRev(List<string> CodedStr)
{
    string res = "";
    foreach (string s in CodedStr)
    {
        res = res+s;
    }
    List<string> lstResult = new List<string>();
    int lenOrginal = res.Length;
    string subOrginalStr;
    byte[] AsciisubOrginalStr;
    int AC;

    for (int i = 0; i < lenOrginal; i++)
    {
        subOrginalStr = res.Substring(i, 1);
        AsciisubOrginalStr = Encoding.ASCII.GetBytes(subOrginalStr);
        if (AsciisubOrginalStr[0] < 100)
        {
            AC = Convert.ToInt32(AsciisubOrginalStr[0]);
            lstResult.Add((AC).ToString());
        }
        else
        {
            AC = Convert.ToInt32(AsciisubOrginalStr[0]);
            lstResult.Add((AC).ToString());
        }
    }
    return lstResult;
}

The string input hello

convert to ascii result :

enter image description here

Convert ascii to main text :

enter image description here

But it doesn't work and it doesn't return the main text. Why ?

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • 1
    Be specific in telling us about it "not working". –  Sep 22 '14 at 11:42
  • 1
    By the way, I can see only one character of difference between these two methods (the `if` expression). You should definitely consider refactoring. –  Sep 22 '14 at 11:43
  • Ok i will add my result asap – Ehsan Akbar Sep 22 '14 at 11:46
  • See this..http://stackoverflow.com/questions/10349753/converting-a-string-of-ascii-into-normal-string-c-sharp – jiten Sep 22 '14 at 11:46
  • This makes no real sense to me at all. `Encoding.ASCII.GetBytes()` and `Encoding.ASCII.GetString()` convert to and from ASCII. What is all that code in the Q meant to do? Why would you want a list of strings with the numeric codes of the characters? Your two functions are identical. You only need one of them. Why have two functions that do identical things? And if you convert `string` to `List`, then the inverse of that converts `List` to `string`. I do suggest that you try to explain at a high level what you are attempting to do. – David Heffernan Sep 22 '14 at 11:51
  • @DavidHeffernan you know i just want to encode my data for creating a license ,i am so new in ascci code – Ehsan Akbar Sep 22 '14 at 12:03
  • 1
    Far better would be to convert the string to UTF-8 and then base64 encode it – David Heffernan Sep 22 '14 at 12:04
  • @DavidHeffernan could you give me an example for more detail ? – Ehsan Akbar Sep 22 '14 at 12:05
  • 2
    Not easily given the question that you asked. When would be useful to understand is why you need to convert the string at all. Why can't you use it as-is. – David Heffernan Sep 22 '14 at 12:06
  • See: [How do I encode and decode a base64 string?](http://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string) – Olivier Jacot-Descombes Sep 22 '14 at 12:57
  • If your function only allows the ASCII range of Unicode characters, it should do a check and throw [ArgumentException](http://msdn.microsoft.com/en-us/library/system.argumentexception%28v=vs.110%29.aspx) if the string contains other characters.`On the other hand, why should it be so limited? – Tom Blodget Sep 23 '14 at 00:37

2 Answers2

1

You seem to be making it too complicated...

If your input string only ever contains ASCII characters (which must be a requirement), then you can encode it as follows:

public static IEnumerable<string> ToDecimalAscii(string input)
{
    return input.Select(c => ((int)c).ToString());
}

You can convert it back to a string like so:

public static string FromDecimalAscii(IEnumerable<string> input)
{
    return new string(input.Select(s => (char)int.Parse(s)).ToArray());
}

Putting it together into a compilable console program:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            string original = "hello";

            var encoded = ToDecimalAscii(original);

            Console.WriteLine("Encoded:");
            Console.WriteLine(string.Join("\n", encoded));

            Console.WriteLine("\nDecoded: " + FromDecimalAscii(encoded));
        }

        public static IEnumerable<string> ToDecimalAscii(string input)
        {
            return input.Select(c => ((int)c).ToString());
        }

        public static string FromDecimalAscii(IEnumerable<string> input)
        {
            return new string(input.Select(s => (char)int.Parse(s)).ToArray());
        }
    }
}

Let me reiterate: This will ONLY work if your input string is guaranteed to contain only characters that are in the ASCII set.


This does not really answer the question of why you want to do this. If you are trying to encode something, you might be better using some sort of encrypting method which outputs an array of bytes, and converting that array to base 64.

This does look like an X-Y question to me.

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
-2

For getting Ascii code simply do as follows.

byte[] asciiBytes = Encoding.ASCII.GetBytes(value);

For string from AScii code,

char c1 = (char) asciiCode;
mrsrizan
  • 301
  • 1
  • 8