Solved

Converting string within dictionnary

Posted on 2011-09-08
4
186 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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