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
60 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 28

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

821 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