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
69 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 34

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 34

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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 

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 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

Industry Leaders: 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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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