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

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 32

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 18

Accepted Solution

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

16 Experts available now in Live!

Get 1:1 Help Now