1

I am working o a project that turns message into ascii decimal values... this side is not important, the problem is it needs to read it back so the translation is basically like this:

 if (textBox1.Text.Contains("a"))
       { 
                textBox3.Text = textBox3.Text.Replace("a", "97");
            }
            if (textBox1.Text.Contains("b"))
            { 
                textBox3.Text = textBox3.Text.Replace("b", "98");
            }
.
.
.
        if (textBox1.Text.Contains("Ğ"))
        {
            textBox3.Text = textBox3.Text.Replace("Ğ", "286");
        }
        if (textBox1.Text.Contains("ş"))
        {
            textBox3.Text = textBox3.Text.Replace("ş", "351");
        }

this translation works perfect. but translating back the output is the problem. my translating back method in a nutshell:

        if (sonmesajBinary.Text.Contains("97"))
        {
            okunanMesaj.Text = okunanMesaj.Text.Replace("97", "a");
        }

        if (sonmesajBinary.Text.Contains("98"))
        {
            okunanMesaj.Text = okunanMesaj.Text.Replace("98", "b");
        }

        if (sonmesajBinary.Text.Contains("99"))
        {
            okunanMesaj.Text = okunanMesaj.Text.Replace("99", "c");
        }

and the problem is lets say the output is 140 but it also includes "40" so pc gets it wrong. That's my problem, and i require your kind help:). i am kinda noob so sorry for my mistakes and i am 17 also english is not my native language. note: ascii values might not be the real ones, these are just for example.

PrimeGamer25
  • 33
  • 1
  • 7
  • For the "translation" part review this https://stackoverflow.com/q/94037/125981 and for c# https://stackoverflow.com/q/4648781/125981 and this https://stackoverflow.com/q/3414900/125981 – Mark Schultheiss Dec 29 '18 at 18:54
  • It's not the right way to go about doing this but if you changed your ifs to if/else-ifs and inverted your logic so it starts with larger numbers it would work. Assuming you're doing 1 character at a time because I don't see how you're getting each individual character again. – Nathan Champion Dec 29 '18 at 18:56
  • the problem is how do we translate it back like the input is 1009798102 and we need it to be chars again – PrimeGamer25 Dec 29 '18 at 18:56
  • sir sorry but i couldn't understand the part of inverting my logic – PrimeGamer25 Dec 29 '18 at 18:57
  • Have you considered a possible pair with an array of the numbers also? – Mark Schultheiss Dec 29 '18 at 18:57
  • no, how can i do that? – PrimeGamer25 Dec 29 '18 at 18:59
  • Forget about ASCII. You are not using it. (Its use is extremely limited.) Do find out which character encoding you are using and [edit] to add it to your question. (Also, consider using UTF-8 instead.) – Tom Blodget Dec 29 '18 at 19:00
  • it is a custom one but supposed to be identical to ascii – PrimeGamer25 Dec 29 '18 at 19:01
  • ok right now i realized that i am too stupid to understand, i wanna cry:( – PrimeGamer25 Dec 29 '18 at 19:06
  • The ASCII character set doesn't include the characters Ğ or ş. That's why ASCII is foreign to most programming environments that process human text, including C#/.NET. (ASCII is identical to Unicode's [C0 Controls and Basis Latin](http://www.unicode.org/charts/nameslist/index.html) block.) – Tom Blodget Dec 29 '18 at 19:06
  • the characters doesn't matter they are my native languages special ones. That is why i say it is a custom one – PrimeGamer25 Dec 29 '18 at 19:07
  • 1
    As you've discovered you need some sort of reversable formatting. Consider a delimiter or fixed witdth format (such as byte values in two hexadecimal digits). – Tom Blodget Dec 29 '18 at 19:11
  • 1
    hmm, that is seriously brilliant!, i can add 0'es at the beggining of the ones with 2 digits, so every one of the would be 3 digits limited let me see if it works – PrimeGamer25 Dec 29 '18 at 19:12
  • There are far more effective ways of dealing with this, for instance you could turn every character of a .NET string into its unicode codepoint hex value equivalent with this single statement: `string.Join("", input.ToCharArray().Select(c => ((int)c).ToString("X4")))`, and then back again with `new string(Enumerable.Range(0, encoded.Length / 4).Select(idx => encoded.Substring(idx * 4, 4)).Select(hex => Convert.ToInt32(hex, 16)).Select(ordinal => (char)ordinal).ToArray())`, which both can likely be converted to something shorter and better. – Lasse V. Karlsen Dec 29 '18 at 19:22
  • so, that wouldn't require me to add every native characters? and i really try hard to understand your code but i am very noob at programing, so would you be a little bit easy sir? – PrimeGamer25 Dec 29 '18 at 19:24
  • I can post it as an answer, though depending on whether you need a specific format or not, it may not actually answer your question, but I can post it, and you can see. – Lasse V. Karlsen Dec 29 '18 at 19:34
  • i would be very happy if you could post as an example answer – PrimeGamer25 Dec 29 '18 at 19:35
  • Before I do that, would you mind telling us *why* you're doing this? Just so that I don't help you fix the Y of a X/Y problem, and instead I could give you a better Y for your X. If you don't know what a X/Y problem is, it's when you've a problem X, for which you've come up with solution Y, and then you can't really manage to implement Y so you post a question about that, whereas there might be completely different and much better solutions for problem X. So to sum it up, why do you need to encode your string in this manner at all? What problem is this trying to solve? – Lasse V. Karlsen Dec 29 '18 at 19:36
  • it is my project homework to encyrpt a file using number pi, so i take it and take a whole random part of it and at the and when i decyrpt it, it just leaves me with the ascii' like values of the text, so i am almost done with this part and the adding 0'es is seeming to be working but i wanted to learn that short and more effective method. – PrimeGamer25 Dec 29 '18 at 19:40
  • You might also consider using a List or Dictionary to avoid all this hard coding of values. – Mark Schultheiss Dec 29 '18 at 20:24

2 Answers2

1

There are many problems with your code there. Checking Contains will return true for any number of occurrences of a character at any location. You're checking in textBox1 and replacing in textBox3. You're checking each character known to you but it is possible there are more! There are easier ways of getting the byte/int/number equivalent of your character based on the encoding of your input.

Here's a rudimentary solution based on comments following the question. You however need to read more about code pages and then encodings. This is only part of the Encrypt operation. I'm sure you can figure out how to replace the contents and later also Decrypt to usable format. Cheers! Happy coding.

        static void Main(string[] args)
        {
            string fileContents = "";
            int encryptKey = 3; // Consider getting this from args[0], etc.
            using (FileStream fs = File.OpenRead(@"C:\Users\My\Desktop\testfile.txt"))
            using (TextReader tr = new StreamReader(fs))
            {
                fileContents = tr.ReadToEnd();
            }
            byte[] asciiBytesOfFile = Encoding.ASCII.GetBytes(fileContents);
            int[] encryptedContents = Encrypt(encryptKey, asciiBytesOfFile);
        }

        private static int[] Encrypt(int encryptKey, byte[] asciiBytesOfFile)
        {
            int[] encryptedChars = new int[asciiBytesOfFile.Length];
            for (int i = 0; i < asciiBytesOfFile.Length; i++)
            {
                encryptedChars[i] = encryptKey ^ asciiBytesOfFile[i];
            }
            return encryptedChars;
        }
KSK
  • 666
  • 4
  • 22
  • Finally, an easy code that i can read and understand:) Thank you sir! I will try this – PrimeGamer25 Dec 29 '18 at 20:36
  • Looks like you're a student. Here's some additional reading: https://www.w3.org/International/questions/qa-what-is-encoding – KSK Dec 29 '18 at 23:08
  • I will be reading that, thanks again sir, and yes I am a student in highschool but the people still downvotes me as you can see. They judge me like i was a software engineer – PrimeGamer25 Dec 30 '18 at 07:18
0

It was fixed thanks to Tom Blodget, all I needed to do was delimit. So I added 0 to beginning of every 2 digit values:D

if (textBox1.Text.Contains("a"))
{ 
    textBox3.Text = textBox3.Text.Replace("a", "097");
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
PrimeGamer25
  • 33
  • 1
  • 7