Solved

Converting string within dictionnary

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

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:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 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