Solved

Using C#, how would you set up an array with pairs of values, 1 of type char and the other of type int?

Posted on 2016-11-12
7
52 Views
Last Modified: 2016-11-12
I am writing a C# application with .Net Framework 4.0 and I intend to create a lookup array in which I will store the values by
hardcoding them in my application.
The concept is to have a
character and its corresponding related integer value.

For example, the first 3 sets of values are as follows:

char                 related decimal value
------                 ---------------------------------
<                      76
(                       77
&                      80


The reason I need this array is ultimately I will be reading in a txt file that is made up of
various record types (for ex: 01, 10, 20 etc) . The record types in the txt file will include a record size field with a value of one of the char values in my lookup array that will tell me the record length for each distinct record type.

Thus, if the field "01" Record Type has a record size field value of '&', then by looking up this value in the array, I can retrieve the value of '80' as the record size for this particular record type.  

How would you set this up in C# using an array and the 3 sample rows I showed above?
0
Comment
Question by:zimmer9
  • 4
  • 2
7 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 41884909
For this kind of lookup you don't use an array, but an Dictionary<char, int>.

Question: why are you lookup values different from the ASCII values?
0
 

Author Comment

by:zimmer9
ID: 41884924
I'm using a mainframe download file.

http://www.simotime.com/asc2ebc1.htm
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41884979
Depending on the file, you can use the built-in Encoding. EBDIC is recognized. So parse your file and translate the string data.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:zimmer9
ID: 41884987
Depending on the file, you can use the built-in Encoding. EBDIC is recognized. So parse your file and translate the string data.
-------------------

Do you mean like this because I don't get the result I'm looking for:

byte[] ebcdicData;
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();  
ebcdicData = enc.GetBytes(filePath);
0
 

Author Comment

by:zimmer9
ID: 41884991
0
 

Author Comment

by:zimmer9
ID: 41884993
I have attached a txt file that has an ampersand (&) in position 4. I am trying to convert this '&' into the decimal equivalent of 80. But I can't get it to work via Encoding. Therefore, I am trying to create my own array of values.
0
 
LVL 23

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41885005
You should use Hash Table for this kind of requirement.  Try...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable hashtable = new Hashtable();
            hashtable["<"] = 76;
            hashtable["("] = 77;
            hashtable["&"] = 80;

            foreach (DictionaryEntry entry in hashtable)
            {
                Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
            }

            Console.WriteLine();
        }
    }
}

Open in new window


Output

EE.jpg
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

937 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

1 Experts available now in Live!

Get 1:1 Help Now