Avatar of XK8ER
XK8ERFlag 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
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Hi,

Do you have the encoder code?

Giannis
Avatar of XK8ER
XK8ER
Flag of United States of America image

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

Avatar of XK8ER
XK8ER
Flag of United States of America image

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
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
Avatar of XK8ER
XK8ER
Flag of United States of America image

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
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of XK8ER
XK8ER
Flag of United States of America image

ASKER

okay.. thanks I got it
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo