How to convert it to string back? [closed]

It’s not possible unless you are 100% sure all characters in txt are in the range 0-9, 10-99, 100-999, etc. (that is: the string representation of the integer value of each character has a fixed length)

Since you are not separating them, the results are ambiguous

Update

As per the comments, you want to do some encryption and decryption. This is a very weak encryption method and I don’t really recommend you to do this, but adding a delimiter could help you do this:

  public static string Encode(string txt)
  {
      string result = string.Empty;
      foreach (char ch in txt)
      {
          // "tku" will be our delimiter
          result += Convert.ToString((int)ch, 10) + "tku";;
      }
      result = result.Replace("0", "dos");
      result = result.Replace("1", "vso");
      result = result.Replace("2", "otw");
      result = result.Replace("3", "foa");
      result = result.Replace("4", "bae");
      result = result.Replace("5", "xgd");
      result = result.Replace("6", "ymt");
      result = result.Replace("7", "ksx");
      result = result.Replace("8", "wte");
      result = result.Replace("9", "rom");
      Console.WriteLine(result);
      return result;
    }

    public static string Decode(string txt)
    {
        string result = txt;
        result = result.Replace("dos", "0");
        result = result.Replace("vso", "1");
        result = result.Replace("otw", "2");
        result = result.Replace("foa", "3");
        result = result.Replace("bae", "4");
        result = result.Replace("xgd", "5");
        result = result.Replace("ymt", "6");
        result = result.Replace("ksx", "7");
        result = result.Replace("wte", "8");
        result = result.Replace("rom", "9");
        // "tku" will be converted to spaces
        result = result.Replace("tku", " ");
        string result2 = string.Empty;
        // and we split over them
        foreach(var res in result.Split(' '))
        {
          if(!String.IsNullOrWhiteSpace(res))
          {
            result2 += ((char)Int32.Parse(res)).ToString();
          }
        }           
        return result2;
    }

I’ve made a fiddle here: https://dotnetfiddle.net/zV9zYw

Again, I strongly recommend against this, just put it to let you know how it works. I’ve not optimized the code or tried to make it any better than yours.

This encryption is extremely weak, and there are good two way encription engines built in the .NET framework.

PS: for readers, the code comes from OP’s comments, pasted here

Leave a Comment