Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with reading Bytes

Problem with reading Bytes

Scheduled Pinned Locked Moved C#
databasedata-structureshelp
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Honeyboy_20
    wrote on last edited by
    #1

    Hi , I have array and I am try to read as a block where block start from 55 55 and end with 204 204 and within the start and the end I am read message where each message should start with 99 and end with 13 (all as a Hex) and if the message length is 30 I will do operation and if it is 6 I will do another operation. but I am receive some exceptions when I read different bytes from different bytes. Here's a sample from different blocks ( Which Stored In arraylist called :VopMessageDetails)

    55 55 1C 09 0A 0B

    99 1E 3F 41 56 42 30 31 31 30 31 31 4E 4E 20 20 30 31 30 20 20 20 20 01 08 42 0B 59 CF 0D

    99 1E 3F 41 56 42 30 32 31 30 31 32 4E 4E 20 20 30 32 30 20 20 20 31 02 08 93 0D D3 CE 0D

    99 0A 41 2F 01 08 E7 03 00 0D

    99 1E 3F 41 56 43 30 33 31 30 31 32 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D

    99 1E 3F 41 56 41 31 35 31 30 31 32 4E 4E 20 20 31 35 30 20 20 20 20 04 08 00 00 00 00 0D

    CC CC

    ==============================================

    55 55 1C 09 0A 0C

    CC CC

    ============================================
    55 55 1C 09 0A 0D

    55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30
    32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32
    1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC

    99 0A 41 2F 02 08 78 03 00 0D

    CC CC

    ===========================================

    55 55 1C 09 0A 0E

    99 1E 3F 41 56 41 31 33 31 30 31 35 4E 4E 20 20 31 33 30 20 20 20 20 01 08 A0 0B F7 DB 0D

    99 0A 41 2F 01 08 E7 03 00 0D

    99 1E 3F 41 56 42 30 31 31 30 31 35 4E 4E 20 20 30 31 30 20 20 20 20 02 08 42 0B 59 CF 0D

    99 0A 41 2F 02 08 E7 03 00 0D

    99 1E 3F 41 56 43 30 33 31 30 31 35 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D

    99 0A 41 2F 03 08 E7 03 00 0D

    99 1E 3F 41 56 41 30 34 31 30 31 35 4E 4E 20 20 30 34 30 20 20 20 20 04 08 DB 10 C5 CE 0D

    99 0A 41 2F 04 08 E7 03 00 0D

    CC CC

    55 55 16 0C 0D 2B 99 1E 3F 41 56 41 30 36 31 33
    34 33 4E 4E 20 20 30 36 30 20 20 20 20 01 08 C8
    11 4E D2 0D 99 1E 3F 48 56 41 30 31 31 33 34 34
    59 59 53 34 30 34 30 43 4C 59 36 01 00 0E 1F 2C
    DA 0D 99 1E 3F 42 56 41 31 33 31 33 34 35 4E 4E
    20 20 31 33 30 20 20 20 20 01 0A D4 0E 48 DE 0D
    CC CC 55 55 16 0C 0E 13 99 1E 3

    H C 3 Replies Last reply
    0
    • H Honeyboy_20

      Hi , I have array and I am try to read as a block where block start from 55 55 and end with 204 204 and within the start and the end I am read message where each message should start with 99 and end with 13 (all as a Hex) and if the message length is 30 I will do operation and if it is 6 I will do another operation. but I am receive some exceptions when I read different bytes from different bytes. Here's a sample from different blocks ( Which Stored In arraylist called :VopMessageDetails)

      55 55 1C 09 0A 0B

      99 1E 3F 41 56 42 30 31 31 30 31 31 4E 4E 20 20 30 31 30 20 20 20 20 01 08 42 0B 59 CF 0D

      99 1E 3F 41 56 42 30 32 31 30 31 32 4E 4E 20 20 30 32 30 20 20 20 31 02 08 93 0D D3 CE 0D

      99 0A 41 2F 01 08 E7 03 00 0D

      99 1E 3F 41 56 43 30 33 31 30 31 32 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D

      99 1E 3F 41 56 41 31 35 31 30 31 32 4E 4E 20 20 31 35 30 20 20 20 20 04 08 00 00 00 00 0D

      CC CC

      ==============================================

      55 55 1C 09 0A 0C

      CC CC

      ============================================
      55 55 1C 09 0A 0D

      55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30
      32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32
      1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC

      99 0A 41 2F 02 08 78 03 00 0D

      CC CC

      ===========================================

      55 55 1C 09 0A 0E

      99 1E 3F 41 56 41 31 33 31 30 31 35 4E 4E 20 20 31 33 30 20 20 20 20 01 08 A0 0B F7 DB 0D

      99 0A 41 2F 01 08 E7 03 00 0D

      99 1E 3F 41 56 42 30 31 31 30 31 35 4E 4E 20 20 30 31 30 20 20 20 20 02 08 42 0B 59 CF 0D

      99 0A 41 2F 02 08 E7 03 00 0D

      99 1E 3F 41 56 43 30 33 31 30 31 35 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D

      99 0A 41 2F 03 08 E7 03 00 0D

      99 1E 3F 41 56 41 30 34 31 30 31 35 4E 4E 20 20 30 34 30 20 20 20 20 04 08 DB 10 C5 CE 0D

      99 0A 41 2F 04 08 E7 03 00 0D

      CC CC

      55 55 16 0C 0D 2B 99 1E 3F 41 56 41 30 36 31 33
      34 33 4E 4E 20 20 30 36 30 20 20 20 20 01 08 C8
      11 4E D2 0D 99 1E 3F 48 56 41 30 31 31 33 34 34
      59 59 53 34 30 34 30 43 4C 59 36 01 00 0E 1F 2C
      DA 0D 99 1E 3F 42 56 41 31 33 31 33 34 35 4E 4E
      20 20 31 33 30 20 20 20 20 01 0A D4 0E 48 DE 0D
      CC CC 55 55 16 0C 0E 13 99 1E 3

      H Offline
      H Offline
      Honeyboy_20
      wrote on last edited by
      #2

      and here's My code #region 1: Count Numbers of blocks. int blockCounter = 0; int countNumberOfBytes = 0; for (int i = 0; i < VopMessageDetails.Count; i++) { if (VopMessageDetails[countNumberOfBytes].ToString() == "85") { /* * I am use this loop becasuse the first byte is not 204 * so it will through expection so I make it go to first condition * till it reach to the require condition which is else if condition. */ } else if (VopMessageDetails[countNumberOfBytes].ToString() == "204" & VopMessageDetails[countNumberOfBytes - 1].ToString() == "204") { blockCounter++; } countNumberOfBytes++; } Console.WriteLine(blockCounter); #endregion // 2 : Read Each Block As Alone. // To Store Each block in catchBlockBytes Array where each Index represent a block. List[] catchBlockBytes = new List[blockCounter]; //After we Determine Number of Block to catchBlockBytes , Reassignment blockCounter to Zero to set index of each object step by step. blockCounter = 0; catchBlockBytes[blockCounter] = new List(); // To Get the Number of Previous Bytes. int PrevBlockCounter = 0; // Catch Each Block. for (int k = 0; k < VopMessageDetails.Count; k++) { if (VopMessageDetails[0 + PrevBlockCounter].ToString() == "85" & VopMessageDetails[1 + PrevBlockCounter].ToString() == "85") { if (VopMessageDetails[k].ToString() != "204") { catchBlockBytes[blockCounter].Add(VopMessageDetails[k].ToString()); } else { // I am 2 to K because the previous if condtion will not add the last 2 bytes ( 204 and 204) // So I am add 2 to start again from 85 and continue with condition. PrevBlockCounter = k + 2; // blo

      L L 2 Replies Last reply
      0
      • H Honeyboy_20

        Hi , I have array and I am try to read as a block where block start from 55 55 and end with 204 204 and within the start and the end I am read message where each message should start with 99 and end with 13 (all as a Hex) and if the message length is 30 I will do operation and if it is 6 I will do another operation. but I am receive some exceptions when I read different bytes from different bytes. Here's a sample from different blocks ( Which Stored In arraylist called :VopMessageDetails)

        55 55 1C 09 0A 0B

        99 1E 3F 41 56 42 30 31 31 30 31 31 4E 4E 20 20 30 31 30 20 20 20 20 01 08 42 0B 59 CF 0D

        99 1E 3F 41 56 42 30 32 31 30 31 32 4E 4E 20 20 30 32 30 20 20 20 31 02 08 93 0D D3 CE 0D

        99 0A 41 2F 01 08 E7 03 00 0D

        99 1E 3F 41 56 43 30 33 31 30 31 32 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D

        99 1E 3F 41 56 41 31 35 31 30 31 32 4E 4E 20 20 31 35 30 20 20 20 20 04 08 00 00 00 00 0D

        CC CC

        ==============================================

        55 55 1C 09 0A 0C

        CC CC

        ============================================
        55 55 1C 09 0A 0D

        55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30
        32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32
        1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC

        99 0A 41 2F 02 08 78 03 00 0D

        CC CC

        ===========================================

        55 55 1C 09 0A 0E

        99 1E 3F 41 56 41 31 33 31 30 31 35 4E 4E 20 20 31 33 30 20 20 20 20 01 08 A0 0B F7 DB 0D

        99 0A 41 2F 01 08 E7 03 00 0D

        99 1E 3F 41 56 42 30 31 31 30 31 35 4E 4E 20 20 30 31 30 20 20 20 20 02 08 42 0B 59 CF 0D

        99 0A 41 2F 02 08 E7 03 00 0D

        99 1E 3F 41 56 43 30 33 31 30 31 35 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D

        99 0A 41 2F 03 08 E7 03 00 0D

        99 1E 3F 41 56 41 30 34 31 30 31 35 4E 4E 20 20 30 34 30 20 20 20 20 04 08 DB 10 C5 CE 0D

        99 0A 41 2F 04 08 E7 03 00 0D

        CC CC

        55 55 16 0C 0D 2B 99 1E 3F 41 56 41 30 36 31 33
        34 33 4E 4E 20 20 30 36 30 20 20 20 20 01 08 C8
        11 4E D2 0D 99 1E 3F 48 56 41 30 31 31 33 34 34
        59 59 53 34 30 34 30 43 4C 59 36 01 00 0E 1F 2C
        DA 0D 99 1E 3F 42 56 41 31 33 31 33 34 35 4E 4E
        20 20 31 33 30 20 20 20 20 01 0A D4 0E 48 DE 0D
        CC CC 55 55 16 0C 0E 13 99 1E 3

        C Offline
        C Offline
        carbon_golem
        wrote on last edited by
        #3

        What exceptions are you receiving? If you could elaborate it may help get the root of the problem.

        "Simplicity carried to the extreme becomes elegance."
        -Jon Franklin

        H 1 Reply Last reply
        0
        • C carbon_golem

          What exceptions are you receiving? If you could elaborate it may help get the root of the problem.

          "Simplicity carried to the extreme becomes elegance."
          -Jon Franklin

          H Offline
          H Offline
          Honeyboy_20
          wrote on last edited by
          #4

          when I have block start with 55 55 but not content any message and end with with 204 204 like this 55 55 1C 09 0A 0C CC CC and like those 55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30 32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32 1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC 99 0A 41 2F 02 08 78 03 00 0D CC CC

          H C 2 Replies Last reply
          0
          • H Honeyboy_20

            and here's My code #region 1: Count Numbers of blocks. int blockCounter = 0; int countNumberOfBytes = 0; for (int i = 0; i < VopMessageDetails.Count; i++) { if (VopMessageDetails[countNumberOfBytes].ToString() == "85") { /* * I am use this loop becasuse the first byte is not 204 * so it will through expection so I make it go to first condition * till it reach to the require condition which is else if condition. */ } else if (VopMessageDetails[countNumberOfBytes].ToString() == "204" & VopMessageDetails[countNumberOfBytes - 1].ToString() == "204") { blockCounter++; } countNumberOfBytes++; } Console.WriteLine(blockCounter); #endregion // 2 : Read Each Block As Alone. // To Store Each block in catchBlockBytes Array where each Index represent a block. List[] catchBlockBytes = new List[blockCounter]; //After we Determine Number of Block to catchBlockBytes , Reassignment blockCounter to Zero to set index of each object step by step. blockCounter = 0; catchBlockBytes[blockCounter] = new List(); // To Get the Number of Previous Bytes. int PrevBlockCounter = 0; // Catch Each Block. for (int k = 0; k < VopMessageDetails.Count; k++) { if (VopMessageDetails[0 + PrevBlockCounter].ToString() == "85" & VopMessageDetails[1 + PrevBlockCounter].ToString() == "85") { if (VopMessageDetails[k].ToString() != "204") { catchBlockBytes[blockCounter].Add(VopMessageDetails[k].ToString()); } else { // I am 2 to K because the previous if condtion will not add the last 2 bytes ( 204 and 204) // So I am add 2 to start again from 85 and continue with condition. PrevBlockCounter = k + 2; // blo

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Why are you converting byte values to strings to test for numeric equality? Just test against the decimal or hex value. And please use <pre></pre> tags not <code></code> to surround your code blocks; that is why the button is called "code block".

            Just say 'NO' to evaluated arguments for diadic functions! Ash

            1 Reply Last reply
            0
            • H Honeyboy_20

              when I have block start with 55 55 but not content any message and end with with 204 204 like this 55 55 1C 09 0A 0C CC CC and like those 55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30 32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32 1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC 99 0A 41 2F 02 08 78 03 00 0D CC CC

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              Yes that's fine. But what error message or exception do you get? Or do you not get an error where you expected one? If you won't help us, we can't help you. What is the error?

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              1 Reply Last reply
              0
              • H Honeyboy_20

                and here's My code #region 1: Count Numbers of blocks. int blockCounter = 0; int countNumberOfBytes = 0; for (int i = 0; i < VopMessageDetails.Count; i++) { if (VopMessageDetails[countNumberOfBytes].ToString() == "85") { /* * I am use this loop becasuse the first byte is not 204 * so it will through expection so I make it go to first condition * till it reach to the require condition which is else if condition. */ } else if (VopMessageDetails[countNumberOfBytes].ToString() == "204" & VopMessageDetails[countNumberOfBytes - 1].ToString() == "204") { blockCounter++; } countNumberOfBytes++; } Console.WriteLine(blockCounter); #endregion // 2 : Read Each Block As Alone. // To Store Each block in catchBlockBytes Array where each Index represent a block. List[] catchBlockBytes = new List[blockCounter]; //After we Determine Number of Block to catchBlockBytes , Reassignment blockCounter to Zero to set index of each object step by step. blockCounter = 0; catchBlockBytes[blockCounter] = new List(); // To Get the Number of Previous Bytes. int PrevBlockCounter = 0; // Catch Each Block. for (int k = 0; k < VopMessageDetails.Count; k++) { if (VopMessageDetails[0 + PrevBlockCounter].ToString() == "85" & VopMessageDetails[1 + PrevBlockCounter].ToString() == "85") { if (VopMessageDetails[k].ToString() != "204") { catchBlockBytes[blockCounter].Add(VopMessageDetails[k].ToString()); } else { // I am 2 to K because the previous if condtion will not add the last 2 bytes ( 204 and 204) // So I am add 2 to start again from 85 and continue with condition. PrevBlockCounter = k + 2; // blo

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, your code is hard to read, you should use PRE tags instead of CODE tags, as that would preserve formatting and indentation. You also should HTML-escape the < and > and & signs, possibly using the widgets the edit page offers. That way the generic lists would show properly. You could still edit and improve the existing message. Here are some comments: 1. When the data is numeric, it is just silly to convert it to string for comparison purposes. You really should be using something like

                if (myIntOrMyByte==0x55)...

                and not mix decimal and hexadecimal all the time. C# knows about hex literals! 2. Is this an official standard you're implementing? I'm not familiar with double 0x55 and double 0xCC as special markers. And what makes one so sure they will never appear in the data? 3. If you're getting exceptions, you should watch the stack traceback; for a debug build it holds class names, file names *and* line numbers, pointing to the exact line that bombs. And if you can't figure it out, show the exact exception message and indicate the line where it happens. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Season's Greetings to all CPians.

                1 Reply Last reply
                0
                • H Honeyboy_20

                  when I have block start with 55 55 but not content any message and end with with 204 204 like this 55 55 1C 09 0A 0C CC CC and like those 55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30 32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32 1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC 99 0A 41 2F 02 08 78 03 00 0D CC CC

                  C Offline
                  C Offline
                  carbon_golem
                  wrote on last edited by
                  #8

                  Let me get this straight, because I think there are language barriers here. In reference to your example data... a 'BLOCK' is considered everything between 0x55 and 0xCC right? If this is the case, then a couple of your example data sets have multiple blocks. Luc is right in his previous post that data has to escape the fencepost identifiers if they appear inside the payload. And the issue is that your dividing into BLOCKS is not working as expected? No exceptions, just not valid results, ya?

                  "Simplicity carried to the extreme becomes elegance."
                  -Jon Franklin

                  1 Reply Last reply
                  0
                  • H Honeyboy_20

                    Hi , I have array and I am try to read as a block where block start from 55 55 and end with 204 204 and within the start and the end I am read message where each message should start with 99 and end with 13 (all as a Hex) and if the message length is 30 I will do operation and if it is 6 I will do another operation. but I am receive some exceptions when I read different bytes from different bytes. Here's a sample from different blocks ( Which Stored In arraylist called :VopMessageDetails)

                    55 55 1C 09 0A 0B

                    99 1E 3F 41 56 42 30 31 31 30 31 31 4E 4E 20 20 30 31 30 20 20 20 20 01 08 42 0B 59 CF 0D

                    99 1E 3F 41 56 42 30 32 31 30 31 32 4E 4E 20 20 30 32 30 20 20 20 31 02 08 93 0D D3 CE 0D

                    99 0A 41 2F 01 08 E7 03 00 0D

                    99 1E 3F 41 56 43 30 33 31 30 31 32 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D

                    99 1E 3F 41 56 41 31 35 31 30 31 32 4E 4E 20 20 31 35 30 20 20 20 20 04 08 00 00 00 00 0D

                    CC CC

                    ==============================================

                    55 55 1C 09 0A 0C

                    CC CC

                    ============================================
                    55 55 1C 09 0A 0D

                    55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30
                    32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32
                    1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC

                    99 0A 41 2F 02 08 78 03 00 0D

                    CC CC

                    ===========================================

                    55 55 1C 09 0A 0E

                    99 1E 3F 41 56 41 31 33 31 30 31 35 4E 4E 20 20 31 33 30 20 20 20 20 01 08 A0 0B F7 DB 0D

                    99 0A 41 2F 01 08 E7 03 00 0D

                    99 1E 3F 41 56 42 30 31 31 30 31 35 4E 4E 20 20 30 31 30 20 20 20 20 02 08 42 0B 59 CF 0D

                    99 0A 41 2F 02 08 E7 03 00 0D

                    99 1E 3F 41 56 43 30 33 31 30 31 35 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D

                    99 0A 41 2F 03 08 E7 03 00 0D

                    99 1E 3F 41 56 41 30 34 31 30 31 35 4E 4E 20 20 30 34 30 20 20 20 20 04 08 DB 10 C5 CE 0D

                    99 0A 41 2F 04 08 E7 03 00 0D

                    CC CC

                    55 55 16 0C 0D 2B 99 1E 3F 41 56 41 30 36 31 33
                    34 33 4E 4E 20 20 30 36 30 20 20 20 20 01 08 C8
                    11 4E D2 0D 99 1E 3F 48 56 41 30 31 31 33 34 34
                    59 59 53 34 30 34 30 43 4C 59 36 01 00 0E 1F 2C
                    DA 0D 99 1E 3F 42 56 41 31 33 31 33 34 35 4E 4E
                    20 20 31 33 30 20 20 20 20 01 0A D4 0E 48 DE 0D
                    CC CC 55 55 16 0C 0E 13 99 1E 3

                    C Offline
                    C Offline
                    carbon_golem
                    wrote on last edited by
                    #9
                    1. The first 'for' loop does not use its indexer. You can easily change the 'for' loop to a 'foreach' loop and iterate through the bytes. This will simplify your code a little. 2) Do comparisons of bytes, don't convert to strings. 3) If you're working in hex numbers use hex numbers throughout your code, don't mix up number bases. This will make your code easier to understand. 4) Your logical comparisons should use the logical && operator and not the & operator. The & operator returns a bitwise operation resulting in a number, not true or false. They are VERY different. 5) You should always put magic numbers in your code as constants.

                      public const Byte StartDelimiter = 0x55;

                    You should never sprinkle those into code manually, you run a huge risk if you are forced to refactor your work. Not to mention that you may mistype one. 6) I would avoid doing tricks with your loop variable like incrementing/decrementing it inside your loop block unless it is absolutely necessary - which should be never. 7) Match your braces. Just because you don't necessarily have to put them in after an 'if' statement doesn't mean you shouldn't. IMHO. 8) Do yourself a favor and look up the State Machine design pattern, it works very well for these kinds of problems. 9) When I ran your program (with healthy fixups) I found that you presumed that every 0xCC,0xCC was an end delimiter. This, apparently, is a false presumption. CCCC appears in your third dataset as valid information, and should not be treated as a fencepost delimiter. fix that up and I think you should get valid results, or at least get you further along. Regards,

                    "Simplicity carried to the extreme becomes elegance."
                    -Jon Franklin

                    R 1 Reply Last reply
                    0
                    • C carbon_golem
                      1. The first 'for' loop does not use its indexer. You can easily change the 'for' loop to a 'foreach' loop and iterate through the bytes. This will simplify your code a little. 2) Do comparisons of bytes, don't convert to strings. 3) If you're working in hex numbers use hex numbers throughout your code, don't mix up number bases. This will make your code easier to understand. 4) Your logical comparisons should use the logical && operator and not the & operator. The & operator returns a bitwise operation resulting in a number, not true or false. They are VERY different. 5) You should always put magic numbers in your code as constants.

                        public const Byte StartDelimiter = 0x55;

                      You should never sprinkle those into code manually, you run a huge risk if you are forced to refactor your work. Not to mention that you may mistype one. 6) I would avoid doing tricks with your loop variable like incrementing/decrementing it inside your loop block unless it is absolutely necessary - which should be never. 7) Match your braces. Just because you don't necessarily have to put them in after an 'if' statement doesn't mean you shouldn't. IMHO. 8) Do yourself a favor and look up the State Machine design pattern, it works very well for these kinds of problems. 9) When I ran your program (with healthy fixups) I found that you presumed that every 0xCC,0xCC was an end delimiter. This, apparently, is a false presumption. CCCC appears in your third dataset as valid information, and should not be treated as a fencepost delimiter. fix that up and I think you should get valid results, or at least get you further along. Regards,

                      "Simplicity carried to the extreme becomes elegance."
                      -Jon Franklin

                      R Offline
                      R Offline
                      Roger Wright
                      wrote on last edited by
                      #10

                      This is one of the best, and most thorough answers I have ever read, and I agree with every bit of it. I even learned a bit. Thanks! :-D

                      Will Rogers never met me.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups