Sure a simple parsing question (hex conversion)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • i_robot73@hotmail.com

    Sure a simple parsing question (hex conversion)

    I have a file, containing hex values for dates (MMDDYYYY)<stat us
    code><??such as:

    303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 102003035303732 303031020030353 038323030310200

    breaking that down:

    303530313230303 10200
    303530323230303 10200
    303530333230303 10200
    303530343230303 10200...

    How do I break it down, convert and, say, throw it into an Array/
    HashTable/etc.?? Never messed w/ HEX (binary??) files before.

    Appreciate any and all help.

    David D.
  • Stanimir Stoyanov

    #2
    Re: Sure a simple parsing question (hex conversion)

    Hi David,

    Below is sample code I wrote how to interpret the data blocks. You should
    implement appropriate error handling when parsing non-integer strings.

    string content =
    "30353031323030 310200303530323 230303102003035 303332303031020 030353034323030 310200303530373 230303102003035 303832303031020 0";

    // TODO: Implement proper exception handling
    // Constants based on the block format
    const int datePartLength = 8;
    const int otherPartLength = 1;
    const int blockLength = (datePartLength + otherPartLength * 2) * 2;

    for (int i = 0; i < content.Length; i += blockLength)
    {
    // Each block is extracted and converted here
    string blockRaw = content.Substri ng(i, blockLength);
    string blockReadable = string.Empty;

    for (int j = 0; j < blockRaw.Length ; j += 2)
    blockReadable += (char)Convert.T oByte(blockRaw. Substring(j,
    2), 16);

    string datePart = blockReadable.S ubstring(0, datePartLength) ;

    DateTime date = DateTime.ParseE xact(datePart, "MMddyyyy",
    System.Globaliz ation.CultureIn fo.InvariantCul ture);
    int status = (int)blockReada ble[datePartLength];

    // Make use of date and status here
    }
    --
    Stanimir Stoyanov


    <i_robot73@hotm ail.comwrote in message
    news:d6f85bcd-6dc6-49d9-8a7f-cb420cdf4659@q3 5g2000hsg.googl egroups.com...
    >I have a file, containing hex values for dates (MMDDYYYY)<stat us
    code><??such as:
    >
    303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 102003035303732 303031020030353 038323030310200
    >
    breaking that down:
    >
    303530313230303 10200
    303530323230303 10200
    303530333230303 10200
    303530343230303 10200...
    >
    How do I break it down, convert and, say, throw it into an Array/
    HashTable/etc.?? Never messed w/ HEX (binary??) files before.
    >
    Appreciate any and all help.
    >
    David D.

    Comment

    • Jeff Johnson

      #3
      Re: Sure a simple parsing question (hex conversion)

      <i_robot73@hotm ail.comwrote in message
      news:d6f85bcd-6dc6-49d9-8a7f-cb420cdf4659@q3 5g2000hsg.googl egroups.com...
      >I have a file, containing hex values for dates (MMDDYYYY)<stat us
      code><??such as:
      >
      303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 102003035303732 303031020030353 038323030310200
      >
      breaking that down:
      >
      303530313230303 10200
      303530323230303 10200
      303530333230303 10200
      303530343230303 10200...
      >
      How do I break it down, convert and, say, throw it into an Array/
      HashTable/etc.?? Never messed w/ HEX (binary??) files before.
      Do you mean your file contains an ASCII representation of hex digits (in
      other words, if you open your file in Notepad you see exactly what you wrote
      above)?

      If so, you can use the Convert.ToByte( ) overload which takes a String and an
      Int32 representing the base of the number in the string. Then you can build
      a byte array and convert it to a string with the ASCIIEncoding class. At
      that point you'll just have to substring to get out the values you need.

      If, however, you were just making a visual representation of your file for
      posting purposes and it actually contains "real bytes" then you're simply
      skipping the conversion and you can decode your bytes with ASCIIEncoding
      directly.

      In other words, take a look at the ASCIIEncoding class first, especially
      methods like GetString().


      Comment

      • i_robot73@hotmail.com

        #4
        Re: Sure a simple parsing question (hex conversion)

        On Oct 30, 3:58 pm, "Jeff Johnson" <i....@enough.s pamwrote:
        <i_robo...@hotm ail.comwrote in message
        >
        news:d6f85bcd-6dc6-49d9-8a7f-cb420cdf4659@q3 5g2000hsg.googl egroups.com...
        >
        I have a file, containing hex values for dates (MMDDYYYY)<stat us
        code><??such as:
        >
        303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 ­10200303530373 230303102003035 303832303031020 0
        >
        breaking that down:
        >
        303530313230303 10200
        303530323230303 10200
        303530333230303 10200
        303530343230303 10200...
        >
        How do I break it down, convert and, say, throw it into an Array/
        HashTable/etc.??  Never messed w/ HEX (binary??) files before.
        >
        Do you mean your file contains an ASCII representation of hex digits (in
        other words, if you open your file in Notepad you see exactly what you wrote
        above)?
        >
        If so, you can use the Convert.ToByte( ) overload which takes a String andan
        Int32 representing the base of the number in the string. Then you can build
        a byte array and convert it to a string with the ASCIIEncoding class. At
        that point you'll just have to substring to get out the values you need.
        >
        If, however, you were just making a visual representation of your file for
        posting purposes and it actually contains "real bytes" then you're simply
        skipping the conversion and you can decode your bytes with ASCIIEncoding
        directly.
        >
        In other words, take a look at the ASCIIEncoding class first, especially
        methods like GetString().
        The file is from an 'in-house' vendor provided solution:

        file opened in a hex editor:

        303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 102003035303732 3030310200...

        file opened in wordpad:

        0501200105022 00105032001 ...

        Comment

        • i_robot73@hotmail.com

          #5
          Re: Sure a simple parsing question (hex conversion)

          On Oct 30, 3:39 pm, "Stanimir Stoyanov" <stoya...@REMOV ETHIS.live.com>
          wrote:
          Hi David,
          >
          Below is sample code I wrote how to interpret the data blocks. You should
          implement appropriate error handling when parsing non-integer strings.
          >
                  string content =
          "30353031323030 310200303530323 230303102003035 303332303031020 030353034323030 ­31020030353037 323030310200303 530383230303102 00";
          >
                  // TODO: Implement proper exception handling
                  // Constants based on the block format
                  const int datePartLength = 8;
                  const int otherPartLength = 1;
                  const int blockLength = (datePartLength + otherPartLength * 2) * 2;
          >
                  for (int i = 0; i < content.Length; i += blockLength)
                  {
                      // Each block is extracted and converted here
                      string blockRaw = content.Substri ng(i, blockLength);
                      string blockReadable = string.Empty;
          >
                      for (int j = 0; j < blockRaw.Length ; j += 2)
                          blockReadable += (char)Convert.T oByte(blockRaw. Substring(j,
          2), 16);
          >
                      string datePart = blockReadable.S ubstring(0, datePartLength) ;
          >
                      DateTime date = DateTime.ParseE xact(datePart, "MMddyyyy",
          System.Globaliz ation.CultureIn fo.InvariantCul ture);
                      int status = (int)blockReada ble[datePartLength];
          >
                      // Make use of date and status here
                  }
          --
          Stanimir Stoyanovhttp://stoyanoff.info
          >
          <i_robo...@hotm ail.comwrote in message
          >
          news:d6f85bcd-6dc6-49d9-8a7f-cb420cdf4659@q3 5g2000hsg.googl egroups.com...
          >
          >
          >
          I have a file, containing hex values for dates (MMDDYYYY)<stat us
          code><??such as:
          >
          303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 ­10200303530373 230303102003035 303832303031020 0
          >
          breaking that down:
          >
          303530313230303 10200
          303530323230303 10200
          303530333230303 10200
          303530343230303 10200...
          >
          How do I break it down, convert and, say, throw it into an Array/
          HashTable/etc.??  Never messed w/ HEX (binary??) files before.
          >
          Appreciate any and all help.
          >
          David D.- Hide quoted text -
          >
          - Show quoted text -
          Code works great, throws some 'odd' characters (the '0200'...looks
          like a square). Guess the last ? is....how to I READ in the file :P

          FileStream to byte[]?

          Comment

          • i_robot73@hotmail.com

            #6
            Re: Sure a simple parsing question (hex conversion)

            On Oct 31, 7:20 am, i_robo...@hotma il.com wrote:
            On Oct 30, 3:58 pm, "Jeff Johnson" <i....@enough.s pamwrote:
            >
            >
            >
            >
            >
            <i_robo...@hotm ail.comwrote in message
            >
            news:d6f85bcd-6dc6-49d9-8a7f-cb420cdf4659@q3 5g2000hsg.googl egroups.com....
            >
            >I have a file, containing hex values for dates (MMDDYYYY)<stat us
            code><??such as:
            >
            303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 ­­1020030353037 323030310200303 530383230303102 00
            >
            breaking that down:
            >
            303530313230303 10200
            303530323230303 10200
            303530333230303 10200
            303530343230303 10200...
            >
            How do I break it down, convert and, say, throw it into an Array/
            HashTable/etc.??  Never messed w/ HEX (binary??) files before.
            >
            Do you mean your file contains an ASCII representation of hex digits (in
            other words, if you open your file in Notepad you see exactly what you wrote
            above)?
            >
            If so, you can use the Convert.ToByte( ) overload which takes a String and an
            Int32 representing the base of the number in the string. Then you can build
            a byte array and convert it to a string with the ASCIIEncoding class. At
            that point you'll just have to substring to get out the values you need..
            >
            If, however, you were just making a visual representation of your file for
            posting purposes and it actually contains "real bytes" then you're simply
            skipping the conversion and you can decode your bytes with ASCIIEncoding
            directly.
            >
            In other words, take a look at the ASCIIEncoding class first, especially
            methods like GetString().
            >
            The file is from an 'in-house' vendor provided solution:
            >
            file opened in a hex editor:
            >
            303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 ­10200303530373 23030310200...
            >
            file opened in wordpad:
            >
            05012001  05022001  05032001  ...- Hide quoted text -
            >
            - Show quoted text -
            With the lovely help of all here, I've learned alot today.


            File is read as binary (decimal?) into a byte array
            each byte[] is converted & appended to string as hex

            Then use the code above to parse:



            string FILE= (@"<path to file>");
            FileStream fs = File.OpenRead(F ILE);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, (int)fs.Length) ;
            fs.Close();

            //Convert DEC (binary) byte[] to HEX
            string content = null;
            foreach (byte dec in data)
            {
            string content = += dec.ToString("X ");
            }

            Woohoo....Thank s again!

            Comment

            • Jeff Johnson

              #7
              Re: Sure a simple parsing question (hex conversion)

              <i_robot73@hotm ail.comwrote in message
              news:d8a8e2f1-43bd-4d11-af0e-9cc3d322be12@i1 8g2000prf.googl egroups.com...
              File is read as binary (decimal?)
              Okay, let me try to clear up this misunderstandin g. In the programming world
              we sometimes use words in ways that aren't always technically correct.
              Computers deal in nothing but binary (in bytes). So if you think about it,
              EVERY file is a "binary" file. However, when talking about data (files), we
              use the term "binary" to refer to files that contain characters with no
              printable representation. In general, this means that the file contains
              bytes in the range of 0 -31, with the exception of a few bytes which control
              printing, such as a carriage return (13), line feed (10), and tab (9). If a
              file contains nothing but bytes from 32-255 (plus 9, 10, and/or 13) then we
              refer to it as a "text" file, because you'll more than likely be able to
              open it up in Notepad and see all of the characters in it without "weird"
              stuff. The file you described in your original post would be considered a
              "binary" file because it contains 2 and 0.

              Now, as to binary, decimal, and hexadecimal. These are number systems (or,
              apparently more correctly, "numeral systems":
              http://en.wikipedia.org/wiki/Numeral_system). They are merely a
              REPRESENTATION of a number made for the ease of human beings. It is possible
              to represent (or display) the same amount of something in several different
              ways. For example, consider the number of bars written below:

              | | | | | | | | | | | |

              In the decimal system, which is what humanity has basically adopted globally
              (because we have 10 TOES, not fingers, or so anthropologists believe), this
              amount is represented as "12".

              In the binary system, this number is represented as "1100" (or, if you like
              your binary numbers in 8-bit chunks, "00001100") .

              In the hexadecimal system, it's "C", or if you prefer 2 digits, "0C".

              When switching between systems (changing "bases"), there is no conversion of
              the amount. Twelve things are always twelve things, but there can be
              conversions of the way the amount is DISPLAYED. This is what it means when
              you "convert" decimal to hex, for example.

              When you wrote this:

              //Convert DEC (binary) byte[] to HEX
              string content = null;
              foreach (byte dec in data)
              {
              string content = += dec.ToString("X ");
              }

              what you really meant was "make a 2-digit hexadecimal representation of the
              values in the byte array and store it in a string." Saying that your bytes
              are "in decimal" is incorrect. If anything, they are "in binary," but it
              doesn't matter. The computer knows that twelve is twelve, and forty is
              forty.

              Back to the original issue: I don't know why you've turned your data into a
              string of hex digits. I thought you were looking to extract "05012001" from
              the string and turn it into the date 2001-05-01. (Which, by the way, is just
              a way to DISPLAY a date, but that's another story. If you're American you
              probably prefer 5/1/2001, and if you're European you probably want
              1/5/2001.) You should do what I recommended: take the first 8 bytes and use
              ASCIIEncoding.G etString() to get a string from them and then use
              DateTime.Parse( ) to get a date.


              Comment

              Working...