?
Solved

Converting string within dictionnary

Posted on 2011-09-08
4
Medium Priority
?
189 Views
Last Modified: 2013-12-17
hi
hello I have a numeric string and I want to convert it to alpha numeric string, below I manage the conversion when the string is already an alpha numeric to numeric by the loop through each character now I want the opposite ie looping the numeric string by 2 characters on the entire string and convert it to Alpha numeric.

thx
public class ConvertBarCode
    {


        public static void Convert()
        {
            #region Dictionnary of barCode
            //contruction du dictionnaire keys values des barcodes
            Dictionary<string, string> barcodeDecoding = new Dictionary<string, string>() 
            { 
                {"space","00"},
                {"!","01"},
                {"\"","02"},
                {"#","03"},
                {"$","04"},
                {"%","05"},
                {"&","06"},
                {"'","07"},
                {"(","08"},
                {")","09"},
                {"*","10"},
                {"+","11"},
                {",","12"},
                {"-","13"},
                {".","14"},
                {"/","15"},
                {"0","16"},
                {"1","17"},
                {"2","18"},
                {"3","19"},
                {"4","20"},
                {"5","21"},
                {"6","22"},
                {"7","23"},
                {"8","24"},
                {"9","25"},
                {":","26"},
                {";","27"},
                {"<","28"},
                {"=","29"},
                {">","30"},
                {"?","31"},
                {"@","32"},
                {"A","33"},
                {"B","34"},
                {"C","35"},
                {"D","36"},
                {"E","37"},
                {"F","38"},
                {"G","39"},
                {"H","40"},
                {"I","41"},
                {"J","42"},
                {"K","43"},
                {"L","44"},
                {"M","45"},
                {"N","46"},
                {"O","47"},
                {"P","48"},
                {"Q","49"},
                {"R","50"},
                {"S","51"},
                {"T","52"},
                {"U","53"},
                {"V","54"},
                {"W","55"},
                {"X","56"},
                {"Y","57"},
                {"Z","58"}

            };
            #endregion

            string stringToconvert = "1K1-11T";
            Console.WriteLine("chaine a convertir : " + stringToconvert + " chaine convertie : " + ConversionBarCode(stringToconvert, barcodeDecoding));
            Console.ReadKey();

        }
        /// <summary>
        /// Effectue la conversion d'une chaine
        /// </summary>
        /// <param name="stringToconvert">Chaine à convertir</param>
        /// /// <param name="barcodeDecoding">Dictionnaire contenant les keys values des elements de conversion</param>
        /// <returns>chaine qui est convertie</returns>
        static string ConversionBarCode(string stringToconvert, Dictionary<string, string> barcodeDecoding)
        {
            string convertString = string.Empty;

            //instancier un stringBuilder
            StringBuilder sbBackString = new StringBuilder();

            //pour chaque element de la chaine recuperer; on effectue la conversion dans le dictionnaire et on contruire le stringBuider
            for (int i = 0, j = stringToconvert.Length; i < j; i++)
            {
                string stringaConvert = stringToconvert.Substring(0, 2);
                sbBackString.Append(barcodeDecoding[stringaConvert[i].ToString()]);
            }

            //affecter le stringBuilder au string de retour
            convertString = sbBackString.ToString();

            return convertString;
        }
    }

Open in new window

0
Comment
Question by:abgconsulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
jagrut_patel earned 2000 total points
ID: 36501793
Try this!

public class ConvertBarCode
{


    public static void Convert()
    {
        #region Dictionnary of barCode
        //contruction du dictionnaire keys values des barcodes
        Dictionary<string, string> barcodeDecoding = new Dictionary<string, string>() 
        { 
            {"space","00"},
            {"!","01"},
            {"\"","02"},
            {"#","03"},
            {"$","04"},
            {"%","05"},
            {"&","06"},
            {"'","07"},
            {"(","08"},
            {")","09"},
            {"*","10"},
            {"+","11"},
            {",","12"},
            {"-","13"},
            {".","14"},
            {"/","15"},
            {"0","16"},
            {"1","17"},
            {"2","18"},
            {"3","19"},
            {"4","20"},
            {"5","21"},
            {"6","22"},
            {"7","23"},
            {"8","24"},
            {"9","25"},
            {":","26"},
            {";","27"},
            {"<","28"},
            {"=","29"},
            {">","30"},
            {"?","31"},
            {"@","32"},
            {"A","33"},
            {"B","34"},
            {"C","35"},
            {"D","36"},
            {"E","37"},
            {"F","38"},
            {"G","39"},
            {"H","40"},
            {"I","41"},
            {"J","42"},
            {"K","43"},
            {"L","44"},
            {"M","45"},
            {"N","46"},
            {"O","47"},
            {"P","48"},
            {"Q","49"},
            {"R","50"},
            {"S","51"},
            {"T","52"},
            {"U","53"},
            {"V","54"},
            {"W","55"},
            {"X","56"},
            {"Y","57"},
            {"Z","58"}

        };
        #endregion

        string stringToconvert = "1K1-11T";
        Console.WriteLine("chaine a convertir : " + stringToconvert + " chaine convertie : " + ConversionBarCode(stringToconvert, barcodeDecoding)); //Outputs: 17431713171752

        stringToconvert = "17431713171752";
        Console.WriteLine("chaine a convertir : " + stringToconvert + " chaine convertie : " + ReverseConvertBarCode(stringToconvert, barcodeDecoding)); //Outputs: 1K1-11T
        Console.ReadKey();

    }
    /// <summary>
    /// Effectue la conversion d'une chaine
    /// </summary>
    /// <param name="stringToconvert">Chaine à convertir</param>
    /// /// <param name="barcodeDecoding">Dictionnaire contenant les keys values des elements de conversion</param>
    /// <returns>chaine qui est convertie</returns>
    static string ConversionBarCode(string stringToconvert, Dictionary<string, string> barcodeDecoding)
    {
        string convertString = string.Empty;

        //instancier un stringBuilder
        StringBuilder sbBackString = new StringBuilder();

        //pour chaque element de la chaine recuperer; on effectue la conversion dans le dictionnaire et on contruire le stringBuider
        for (int i = 0, j = stringToconvert.Length; i < j; i++)
        {
            sbBackString.Append(barcodeDecoding[stringToconvert[i].ToString()]);
        }

        //affecter le stringBuilder au string de retour
        convertString = sbBackString.ToString();

        return convertString;
    }

    static string ReverseConvertBarCode(string stringToconvert, Dictionary<string, string> barcodeDecoding)
    {
        Dictionary<string, string> reverseDict = ReverseDictionary(barcodeDecoding);
        string convertString = string.Empty;

        //instancier un stringBuilder
        StringBuilder sbBackString = new StringBuilder();

        //pour chaque element de la chaine recuperer; on effectue la conversion dans le dictionnaire et on contruire le stringBuider
        for (int i = 0, j = stringToconvert.Length; i < j; i+=2)
        {
            string stringaConvert = stringToconvert.Substring(i, 2);
            sbBackString.Append(reverseDict[stringaConvert]);
        }

        //affecter le stringBuilder au string de retour
        convertString = sbBackString.ToString();

        return convertString;
    }

    static Dictionary<string, string> ReverseDictionary(Dictionary<string, string> barcodeDecoding)
    {
        Dictionary<string, string> reverseDict = new Dictionary<string, string>();
        foreach (KeyValuePair<string, string> keyValuePair in barcodeDecoding)
        {
            reverseDict.Add(keyValuePair.Value, keyValuePair.Key);
        }
        return reverseDict;
    }
}

Open in new window


HTH!
0
 

Author Closing Comment

by:abgconsulting
ID: 36502087
It is working well !!!!
0
 

Author Comment

by:abgconsulting
ID: 36502093
Thank U jagrut_patel !!! ;-)
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36502184
My pleasure.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question