[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Converting string within dictionnary

Posted on 2011-09-08
4
Medium Priority
?
191 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 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