Avatar of XK8ER
XK8ER
Flag for United States of America asked on

c# decode string

Hello,
I have a string decoder that its partially working because some letters are decoded incorrectly.. how can I fix it?

original string
>>123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz

decoded incorrectly
123456789-¿¿¿t°©¾H¿¿¿U°©¿PQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Debug.Print(Hash_Decoder("bdfhjlnprZ‚„†ˆŠŒŽ’”–˜šœž ¢¤¦¨ª¬®°²´ZÂÄÆÈÊÌÎÐÒÔÖØÚÜÞàâäæèêìîðòô"));
        }

        private string Hash_Decoder(string strUserInput)
        {       
            int intLength = 0;
            int CurX = 0;
            int lngCodes = 0;
            int EncodeNumber = 2;
            string strCodes = null;
            string FirstI = null;            
            intLength = strUserInput.Length;

            while (!(CurX == intLength))
            {
                FirstI = strUserInput.Substring(CurX, 1);
                lngCodes = (int)(FirstI)[0] / EncodeNumber;
                strCodes += (char)lngCodes;
                CurX += 1;
            }
            return strCodes;
        }

    }
}

Open in new window

C#.NET Programming

Avatar of undefined
Last Comment
XK8ER

8/22/2022 - Mon
Ioannis Paraskevopoulos

Hi,

Do you have the encoder code?

Giannis
XK8ER

ASKER
yes but its vb6


Option Explicit

Const EncodeNumber = 2

Private Sub cmdEncode_Click()
    Dim intLength, CurX As Integer
    Dim lngCodes As Long
    Dim strUserInput, strCodes As String
    Dim FirstI As String
    
    intLength = Len(txtUserInput.Text)
    strUserInput = txtUserInput
    
    Do Until CurX = intLength
        CurX = CurX + 1
        FirstI = Mid$(strUserInput, CurX, CurX + 1)
        lngCodes = Asc(FirstI) * EncodeNumber
        strCodes = strCodes & Chr$(lngCodes)
        txtEnCoded.Text = strCodes
    Loop

End Sub

Private Sub cmdDecode_Click()
    Dim intLength, CurX As Integer
    Dim lngCodes As Long
    Dim strUserInput, strCodes As String
    Dim FirstI As String
    
    
    intLength = Len(txtEnCoded.Text)
    strUserInput = txtEnCoded.Text
    
    Do Until CurX = intLength
        CurX = CurX + 1
        FirstI = Mid$(strUserInput, CurX, CurX + 1)
        lngCodes = Asc(FirstI) \ EncodeNumber
        strCodes = strCodes & Chr$(lngCodes)
        txtUserInput.Text = strCodes
    Loop
End Sub

Open in new window

XK8ER

ASKER
I am open for suggestions if maybe encoding and decoding into HEX or DEC or something better so we dont get into trouble with comas i dont know
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Ioannis Paraskevopoulos

Well,
I don't have vb6 so i really don't know what is the real output of this code, though it looks fine.

What i did is putting an encoding function in the c# project:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ
            Debug.Print(Hash_Encoder("123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
            Debug.Print(Hash_Decoder(Hash_Encoder("123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ")));
            Debug.Print(Hash_Decoder("bdfhjlnprZ‚„†ˆŠŒŽ’”–˜šœž ¢¤¦¨ª¬®°²´"));
            //Debug.Print(Hash_Decoder("123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
        }

        private string Hash_Decoder(string strUserInput)
        {
            string functionReturnValue = null;
            functionReturnValue = null;
            int intLength = 0;
            int CurX = 0;
            int lngCodes = 0;
            string strCodes = null;
            char FirstI;
            const short EncodeNumber = 2;
            intLength = strUserInput.Length;

            while (!(CurX == intLength))
            {
                FirstI = strUserInput.Substring(CurX, 1)[0];
                lngCodes = (int)FirstI / EncodeNumber;
                strCodes = strCodes + (char)lngCodes;
                functionReturnValue = strCodes;

                CurX += 1;
            }
            return functionReturnValue;
        }
        private string Hash_Encoder(string strUserInput)
        {
            string functionReturnValue = null;
            functionReturnValue = null;
            int intLength = 0;
            int CurX = 0;
            int lngCodes = 0;
            string strCodes = null;
            char FirstI;
            const short EncodeNumber = 2;
            intLength = strUserInput.Length;

            while (!(CurX == intLength))
            {
                FirstI = strUserInput.Substring(CurX, 1)[0];
                lngCodes = (int)FirstI * EncodeNumber;
                strCodes = strCodes + (char)lngCodes;
                functionReturnValue = strCodes;

                CurX += 1;
            }
            return functionReturnValue;
        }

    }
}

Open in new window


This actually demonstrates, that if the input is correct, then the output is correct too.

Giannis
XK8ER

ASKER
with your code implemented I still get the same input..

this is my output
123456789-¿¿¿t°©¾H¿¿¿U°©¿QRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz

this is how my code looks like


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Debug.Print(Hash_Decoder("bdfhjlnprZ‚„†ˆŠŒŽ’”–˜šœž ¢¤¦¨ª¬®°²´ZÂÄÆÈÊÌÎÐÒÔÖØÚÜÞàâäæèêìîðòô"));
        }

        private string Hash_Decoder(string strUserInput)
        {
            int intLength = 0;
            int CurX = 0;
            int lngCodes = 0;            
            string strCodes = null;
            char FirstI;
            const short EncodeNumber = 2;
            intLength = strUserInput.Length;

            while (!(CurX == intLength))
            {
                FirstI = strUserInput.Substring(CurX, 1)[0];
                lngCodes = (int)FirstI / EncodeNumber;
                strCodes += (char)lngCodes;
                CurX += 1;
            }

            return strCodes;
        }

    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Ioannis Paraskevopoulos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
XK8ER

ASKER
okay.. thanks I got it
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.