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. Updating portion of byte array not working

Updating portion of byte array not working

Scheduled Pinned Locked Moved C#
databasedata-structuresquestionannouncement
13 Posts 4 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.
  • M MichCl

    I'm trying to update a portion of a byte array with modified data and for some reason it's always unchanged. Any input would be appreciated. Right now, I have a temporary array that I'm trying to do an array copy for part of the array to update the data. This isn't working. Before that, I didn't have a temporary array and was doing basically the same thing with the destination array. The data locations I'm writing to are 0 after the update, but I am seeing the correct data in my tempStrings and convertStringToBytes method.

    private void updateDatWithData()
    {
    int returnCode = 0;
    byte[] tempByteArr = new byte[Dat.Length];

            //ex. 005R00099 ==> 005 R 00099 ==> 005 52 00099 ==> 00 55 20 00 99
            String tempString = (configNum\[0\].ToString() + configNum\[1\].ToString()); //ex. 00
            byte tempByte = 0;
            returnCode = convertStringToByte(tempString, ref tempByteArr\[index + 4\]); // ??Dat isn't getting updated below.
    

    ...
    Array.Copy(tempByteArr, (index+ 4), Dat, (index + 4), 5);
    }

    private int convertStringToByte(String theString, ref byte theByte)
    {
    int returnCode = 1;
    try
    {
    theByte = System.Convert.ToByte(theString);
    }
    return returnCode;
    }

    K Offline
    K Offline
    Keith Barrow
    wrote on last edited by
    #2

    You have a try without a catch or finally, so I don't see how your code is compiling. Simplifying your problem:

    private int convertStringToByte(String theString, ref byte theByte)
    {
    theByte = System.Convert.ToByte(theString);
    return 1;
    }

    public void RunSnippet()
    {
        byte\[\] tempByteArr = new byte\[\]{1};
        convertStringToByte("3", ref tempByteArr\[0\]);
        Console.WriteLine(tempByteArr\[0\]);
    }
    

    The above code works, so the basic logic of what you have is OK, just an implementation problem. I'd suggest a different methodology. It looks like you are trying to implement Byte.TryParse method, you may want to look at it. If you need more information than a plain boolean, implementing your own is easy:

    public static int ConvertStringToByte(string s, out byte result)
    {
    try
    {
    theByte = System.Convert.ToByte(theString);

        }
        catch
        {
            return 0;
            //Or whatever - don't catch "all exceptions" like this!
        }
        return 1;
    }
    

    Sort of a cross between Lawrence of Arabia and Dilbert.[^]
    -Or-
    A Dead ringer for Kate Winslett[^]

    M 2 Replies Last reply
    0
    • K Keith Barrow

      You have a try without a catch or finally, so I don't see how your code is compiling. Simplifying your problem:

      private int convertStringToByte(String theString, ref byte theByte)
      {
      theByte = System.Convert.ToByte(theString);
      return 1;
      }

      public void RunSnippet()
      {
          byte\[\] tempByteArr = new byte\[\]{1};
          convertStringToByte("3", ref tempByteArr\[0\]);
          Console.WriteLine(tempByteArr\[0\]);
      }
      

      The above code works, so the basic logic of what you have is OK, just an implementation problem. I'd suggest a different methodology. It looks like you are trying to implement Byte.TryParse method, you may want to look at it. If you need more information than a plain boolean, implementing your own is easy:

      public static int ConvertStringToByte(string s, out byte result)
      {
      try
      {
      theByte = System.Convert.ToByte(theString);

          }
          catch
          {
              return 0;
              //Or whatever - don't catch "all exceptions" like this!
          }
          return 1;
      }
      

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
      -Or-
      A Dead ringer for Kate Winslett[^]

      M Offline
      M Offline
      MichCl
      wrote on last edited by
      #3

      My code actually has a catch for my try. I just didn't include it here to simplify. So it works for you basically as-is?

      K 1 Reply Last reply
      0
      • K Keith Barrow

        You have a try without a catch or finally, so I don't see how your code is compiling. Simplifying your problem:

        private int convertStringToByte(String theString, ref byte theByte)
        {
        theByte = System.Convert.ToByte(theString);
        return 1;
        }

        public void RunSnippet()
        {
            byte\[\] tempByteArr = new byte\[\]{1};
            convertStringToByte("3", ref tempByteArr\[0\]);
            Console.WriteLine(tempByteArr\[0\]);
        }
        

        The above code works, so the basic logic of what you have is OK, just an implementation problem. I'd suggest a different methodology. It looks like you are trying to implement Byte.TryParse method, you may want to look at it. If you need more information than a plain boolean, implementing your own is easy:

        public static int ConvertStringToByte(string s, out byte result)
        {
        try
        {
        theByte = System.Convert.ToByte(theString);

            }
            catch
            {
                return 0;
                //Or whatever - don't catch "all exceptions" like this!
            }
            return 1;
        }
        

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
        -Or-
        A Dead ringer for Kate Winslett[^]

        M Offline
        M Offline
        MichCl
        wrote on last edited by
        #4

        I wonder if it's my data. My strings are "00", "55", "20", "00", and "99".

        1 Reply Last reply
        0
        • M MichCl

          My code actually has a catch for my try. I just didn't include it here to simplify. So it works for you basically as-is?

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #5

          It is working. Given what you have said, the problem could well be in the catch. You need to step through your code to determine what is happening exactly, unless someone here come up with a full solution.

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]
          -Or-
          A Dead ringer for Kate Winslett[^]

          M 1 Reply Last reply
          0
          • K Keith Barrow

            It is working. Given what you have said, the problem could well be in the catch. You need to step through your code to determine what is happening exactly, unless someone here come up with a full solution.

            Sort of a cross between Lawrence of Arabia and Dilbert.[^]
            -Or-
            A Dead ringer for Kate Winslett[^]

            M Offline
            M Offline
            MichCl
            wrote on last edited by
            #6

            Ok. I figured it out. I was constructing one of my strings wrong and wound up with one that was 282, so it was throwing an exception when it tried to convert to byte and never got to copying the temp array to my final Dat array.

            K L 2 Replies Last reply
            0
            • M MichCl

              Ok. I figured it out. I was constructing one of my strings wrong and wound up with one that was 282, so it was throwing an exception when it tried to convert to byte and never got to copying the temp array to my final Dat array.

              K Offline
              K Offline
              Keith Barrow
              wrote on last edited by
              #7

              Glad you found it!

              Sort of a cross between Lawrence of Arabia and Dilbert.[^]
              -Or-
              A Dead ringer for Kate Winslett[^]

              1 Reply Last reply
              0
              • M MichCl

                Ok. I figured it out. I was constructing one of my strings wrong and wound up with one that was 282, so it was throwing an exception when it tried to convert to byte and never got to copying the temp array to my final Dat array.

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

                Lesson learnt: Never write an empty catch block.

                K M 2 Replies Last reply
                0
                • L Lost User

                  Lesson learnt: Never write an empty catch block.

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #9

                  void Get_NameChangedToProtectTheInnocent_()
                  {
                  try
                  {
                  ...
                  }
                  catch
                  {
                  //Bury the error, like the classic site
                  }
                  }

                  There you go - not empty! Written by yours truly, only 10mins ago :-O . In my defence, it's important that the site doesn't throw errors and this is what the rest of the site does. :~

                  Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                  -Or-
                  A Dead ringer for Kate Winslett[^]

                  D 1 Reply Last reply
                  0
                  • L Lost User

                    Lesson learnt: Never write an empty catch block.

                    M Offline
                    M Offline
                    MichCl
                    wrote on last edited by
                    #10

                    Actually, my catch block had a lot in it. It was just not included in my post!

                    L 1 Reply Last reply
                    0
                    • M MichCl

                      Actually, my catch block had a lot in it. It was just not included in my post!

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

                      In Visual Studio, You can enable breaking at an exception even when it is caught and handled. This will be very useful during debugging.

                      1 Reply Last reply
                      0
                      • K Keith Barrow

                        void Get_NameChangedToProtectTheInnocent_()
                        {
                        try
                        {
                        ...
                        }
                        catch
                        {
                        //Bury the error, like the classic site
                        }
                        }

                        There you go - not empty! Written by yours truly, only 10mins ago :-O . In my defence, it's important that the site doesn't throw errors and this is what the rest of the site does. :~

                        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                        -Or-
                        A Dead ringer for Kate Winslett[^]

                        D Offline
                        D Offline
                        DaveyM69
                        wrote on last edited by
                        #12

                        Fair enough, but it should at least be logged for investigation. If it's unimportant and not worth logging then it should be handled in code so the exception never arises.

                        Dave
                        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                        K 1 Reply Last reply
                        0
                        • D DaveyM69

                          Fair enough, but it should at least be logged for investigation. If it's unimportant and not worth logging then it should be handled in code so the exception never arises.

                          Dave
                          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                          K Offline
                          K Offline
                          Keith Barrow
                          wrote on last edited by
                          #13

                          I was being a bit (OK, very) toungue-in-cheek. 99.99% of the time I really wouldn't do this. In this case he exception is very unimportant, for various reasons I'd have to write my own logging infrastructure (which would take longer than the rest of the change, and my code is slated for replacement by an MVC app very soon) and I can't handle in code as the try-block is calling out to an asmx service and I'm guarding against technical errors. I could, I suppose tighten which errors are being caught.

                          Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                          -Or-
                          A Dead ringer for Kate Winslett[^]

                          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