0

I have an input file which has list of Hex Word records, like this "e000483c", "0120003d". I would like to read them and store as Hex numbers rather as a string. Each Hex number is a 32 bit word and should be able to process it further byte-wise. The input file can have large records, is there any faster and reliable way to achieve this?

The problem I am facing really is, if try to process the word byte wise after reading as string using stream reader or File.ReadAllLines, those are considered as characters. For example, I want 'e' to be read as '0xe' not as '0x65'.

Ultimately I want to interpret "e000483c" as "0xe000483c".

Karthik
  • 1
  • 4
  • 2
    sort of confused as to what you want, but for what it's worth: Storing "e000483c" as an `int` prefaced with "0x" is the most efficient way to handle hex values in c#. – Capn Jack Jul 24 '17 at 18:11
  • Not sure I understand. Do you want to keep them as strings with `0x` in front of them or do you want to convert them to numbers? – Deolus Jul 24 '17 at 18:11
  • I want them to convert as numbers, but as hex value – Karthik Jul 24 '17 at 18:20
  • @Karthik see my answer for some sample code to do what you want. However the take away is 1. You can parse these string as is without the "0x" with my method shown. and 2. If you want to maintain the hex as hex instead of decimal then you'll need to keep them as string, or convert to decimal, do calculations, then output the result as `"0x" + yourNewHexValue` – Capn Jack Jul 24 '17 at 18:30

3 Answers3

1

There is no such thing as a "hex number". You can either read it a string, represented in hex or you can read the string and convert it to an integer.

void Main()
{
    string hexString = "e000483c";
    int value = Convert.ToInt32(hexString, 16);

    Console.WriteLine($"The hex string {hexString} is {value} in decimal");
}

// Define other methods and classes here
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
0

From Microsoft documentation:

int value = Convert.ToInt32(hex, 16);

Will result in the string hex being converted in to the int value.

To convert the string to a "hex" string, simply put 0x in front:

string newValue = "0x" + oldValue;

The method you choose really depends on whether you want 1) an integer or 2) a hex string representing an integer, as your final result.

TomServo
  • 7,248
  • 5
  • 30
  • 47
0

Try this code:

    List<int> listOfHexValues = new List<int>();
    var anArray = File.ReadAllLines(yourPath);
    foreach (string hexStr in anArray)
    {
        try
        {
            int hex = int.Parse(temp, System.Globalization.NumberStyles.HexNumber);
            listOfHexValues.Add(hex);
        }
        catch
        {
            //the line wasn't a valid integer.
        }
    }

This is assuming your file has 1 hex number on each line. But you should know inside the int will convert your hex value to the appropriate dec value. If you want to keep that "0x00000000" format then you'll need to maintain them as strings, sorry.

Capn Jack
  • 1,201
  • 11
  • 28