?
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
Medium Priority
?
72 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
[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
  • 4
  • 2
7 Comments
 
LVL 35

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 35

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
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 

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 29

Accepted Solution

by:
Pawan Kumar earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 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