Solved

Converting string within dictionnary

Posted on 2011-09-08
4
182 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
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
jagrut_patel earned 500 total points
Comment Utility
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
Comment Utility
It is working well !!!!
0
 

Author Comment

by:abgconsulting
Comment Utility
Thank U jagrut_patel !!! ;-)
0
 
LVL 8

Expert Comment

by:jagrut_patel
Comment Utility
My pleasure.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now