C
C#2y ago
badb0nny

❔ trying to encode and decode a user inputted string

Im struggling to figure out why it doesnt decode back into the original string?
using System;

namespace Encode_Decode_Program
{
class Globals
{
public static string encodeDecodeKey;
public static int encodeInteger;
public static string encodedString;
public static string decodedString;
}

class Program
{
static void Main()
{
Random rnd = new Random();
Console.WriteLine("Enter a string which you want decoded: "); string encodeString = Console.ReadLine();

for (int x = 0; x < encodeString.Length; x++)
{
int asciiValue = encodeString[x];
Globals.encodeInteger = rnd.Next(1, 9);
Globals.encodeDecodeKey = Globals.encodeDecodeKey + Globals.encodeInteger;
char encodedCharacter = (char)(asciiValue + Globals.encodeInteger);
Console.WriteLine(Globals.encodeDecodeKey);
Console.WriteLine(encodedCharacter);
Globals.encodedString = Globals.encodedString + encodedCharacter;
}

for (int x = 0; x < Globals.encodedString.Length; x++)
{
int asciiValue = Globals.encodedString[x];
char decodedCharacter = (char)(asciiValue - Globals.encodeDecodeKey[x]);
Console.WriteLine(decodedCharacter);
Globals.decodedString = Globals.decodedString + decodedCharacter;
}

Console.WriteLine("Encoded string: " + Globals.encodedString);
Console.WriteLine("Decoded string: " + Globals.decodedString);
Console.ReadLine();
}
}
}
using System;

namespace Encode_Decode_Program
{
class Globals
{
public static string encodeDecodeKey;
public static int encodeInteger;
public static string encodedString;
public static string decodedString;
}

class Program
{
static void Main()
{
Random rnd = new Random();
Console.WriteLine("Enter a string which you want decoded: "); string encodeString = Console.ReadLine();

for (int x = 0; x < encodeString.Length; x++)
{
int asciiValue = encodeString[x];
Globals.encodeInteger = rnd.Next(1, 9);
Globals.encodeDecodeKey = Globals.encodeDecodeKey + Globals.encodeInteger;
char encodedCharacter = (char)(asciiValue + Globals.encodeInteger);
Console.WriteLine(Globals.encodeDecodeKey);
Console.WriteLine(encodedCharacter);
Globals.encodedString = Globals.encodedString + encodedCharacter;
}

for (int x = 0; x < Globals.encodedString.Length; x++)
{
int asciiValue = Globals.encodedString[x];
char decodedCharacter = (char)(asciiValue - Globals.encodeDecodeKey[x]);
Console.WriteLine(decodedCharacter);
Globals.decodedString = Globals.decodedString + decodedCharacter;
}

Console.WriteLine("Encoded string: " + Globals.encodedString);
Console.WriteLine("Decoded string: " + Globals.decodedString);
Console.ReadLine();
}
}
}
I know its not the neatest code of all time im still trying to get into the habit of writing neat code
3 Replies
Buddy
Buddy2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.