Decoding string Char.Parse issue

Hidalgo

I have the following code to encode a plain text:

int AddNumber;
int AsciiNumber;
string OneChar;
string String1 = "DAVE SMITH";
string String2 = "";
for (int i = 0; i < String1.Length; i++)
{
    AddNumber = i + 95;
    AsciiNumber = (int)Char.Parse(String1.Substring(i,1));

    byte[] NewAscii = new byte[] { Convert.ToByte( AsciiNumber + AddNumber ) };

    // Get string of the NewAscii
    OneChar = Encoding.GetEncoding(1252).GetString(NewAscii);

    String2 = String2 + OneChar;
 }

The problem I have is how to decode the string back to plain text. Here is my attempt code:

String1 = "";
for (int i = 0; i < String2.Length; i++)
{
    AddNumber = i + 95;

    AsciiNumber = (int)Char.Parse(String2.Substring(i,1));

    byte[] NewAscii = new byte[] { Convert.ToByte( AsciiNumber - AddNumber ) };

    // Get string of the NewAscii
    OneChar = Encoding.GetEncoding(1252).GetString(NewAscii);

    String1 = String1 + OneChar;
 }

The problem is that above, on processing the encoded empty space (between DAVE and SMITH), the value AsciiNumber = (int)Char.Parse(String2.Substring(i,1)) is 402 where it should be 131.

Do you see what I am misunderstanding?

Mahmoud Darwish

For the decoding part

        String1 = "";
        for (int i = 0; i < String2.Length; i++)
        {
            var charByte = System.Text.Encoding.GetEncoding(1252).GetBytes(String2.Substring(i, 1));
            AddNumber = i + 95;
            AsciiNumber = Convert.ToInt32(charByte[0]) - AddNumber;
            String1 += Convert.ToChar(AsciiNumber);

        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Some Decoding Issue With String in Python

From Dev

LinkedList with Char (String Issue

From Dev

String to char issue

From Dev

Decoding HttpEntity into android string - encoding issue

From Dev

Decoding HttpEntity into android string - encoding issue

From Dev

Regex issue to parse a string

From Dev

Issue in initializer for char string array

From Dev

Issue in initializer for char string array

From Dev

Java String to DateTime parse issue

From Dev

Parsing issue when decoding a JSON String to generate the objects in java

From Dev

Instance of Most Vexing Parse with std::string and char*

From Dev

How do I parse a char from a String?

From Dev

Java loop issue involving char and String

From Dev

Parse JSON String and array with NSJSONSerialization issue?

From Dev

Issue on parsing string at JSON.parse

From Dev

Parse a query string into variable with parse_str()function related issue

From Dev

SwiftyJSON won't parse a JSON string that will later parse with no issue

From Dev

trying to parse inputs that go int string string char int

From Dev

c - const char* and char* casting (ADPCM decoding)

From Dev

Encoding and Decoding GameState Class Issue

From Dev

Issue decoding ajax post in php

From Dev

Parse a long string using gsub and replacement is reserved char in R

From Dev

Gson library when parse string contain special char \u

From Dev

How to convert char array into string for DateTime.Parse?

From Dev

Encoding and Decoding string

From Dev

Decoding Anchor Tag in String

From Dev

Decoding a String Containing Percentage(%)

From Dev

Decoding a hex string

From Dev

Decoding bytes as unicode string

Related Related

HotTag

Archive