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. Reading Null values in a byte array

Reading Null values in a byte array

Scheduled Pinned Locked Moved C#
helpdatabasedata-structurestutorialquestion
3 Posts 3 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.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi All I am trying to loop through a byte array (sample below) reading the values to then insert those into a database I loop through keeping track of the position

    byte[] data;
    long position=0;
    while (position < data.Length)
    {
    position = Conversion.Extract(data, out mydata, position);
    position = Conversion.Extract(data, out nextitem, position);
    }

    public static long Extract(byte\[\] message, out int variable, long position) 
    { 
        const int length = 4; // the length of an int 
    
        if (message.Length >= position + length) 
        { 
            variable = BitConverter.ToInt32(message, (int)position); 
            return position + length; 
        } 
    
        // there was a problem 
        variable = 0; 
        return -1; 
    } 
    

    The data i have contains some null values, how can i extract these and successfully move onto the next value. At present if i come across a null value i dont know how to detect this and move onto the next item. Can anyone help at all Thanks Simon

    6F 72 67 61 6E 69 73 61 74 69 6F 6E 49 64 3D 33
    26 10 00 00 00 50 65 6F 70 6C 65 50 65 6F 70 6C
    65 4C 69 6E 6B 6A 08 00 00 09 00 00 00 0E 00 00
    00 53 65 6E 69 6F 72 50 65 72 73 6F 6E 49 64 0E
    00 00 00 4A 75 6E 69 6F 72 50 65 72 73 6F 6E 49
    64 11 00 00 00 53 65 6E 69 6F 72 50 65 72 73 6F
    6E 4D 69 73 49 64 11 00 00 00 4A 75 6E 69 6F 72
    50 65 72 73 6F 6E 4D 69 73 49 64 08 00 00 00 4C
    69 6E 6B 54 79 70 65 16 00 00 00 50 61 72 65 6E
    74 61 6C 52 65 73 70 6F 6E 73 69 62 69 6C 69 74
    79 08 00 00 00 50 72 69 6F 72 69 74 79 0B 00 00
    00 4C 61 73 74 55 70 64 61 74 65 64 07 00 00 00
    44 65 6C 65 74 65 64 01 00 00 00 D7 0A 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 01 80 C3 29
    C6 85 BF CE 08 00 02 00 00 00 3C 0C 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 F0 83 76 C6
    85 BF CE 08 00 03 00 00 00 3C 0C 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 C0 0E 76 C6 85
    BF CE 08 00 04 00 00 00 67 0B 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 01 70 CA 4B C6 85 BF
    CE 08 00 05 00 00 00 67 0B 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 02 70 CA 4B C6 85 BF CE
    08 00 06 00 00 00 9B 08 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 02 C0 BE B2 C5 85 BF CE 08
    00 06 00 00 00 AD 09 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 02 10 6D F5 C5 85 BF CE 08 00
    06 00 00 00 3F 0A 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 02 80 59 11 C6 85 BF CE 08 00 07
    00 00 00 E3 0A 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 01 C0 D0 2C

    D M 2 Replies Last reply
    0
    • S si_69

      Hi All I am trying to loop through a byte array (sample below) reading the values to then insert those into a database I loop through keeping track of the position

      byte[] data;
      long position=0;
      while (position < data.Length)
      {
      position = Conversion.Extract(data, out mydata, position);
      position = Conversion.Extract(data, out nextitem, position);
      }

      public static long Extract(byte\[\] message, out int variable, long position) 
      { 
          const int length = 4; // the length of an int 
      
          if (message.Length >= position + length) 
          { 
              variable = BitConverter.ToInt32(message, (int)position); 
              return position + length; 
          } 
      
          // there was a problem 
          variable = 0; 
          return -1; 
      } 
      

      The data i have contains some null values, how can i extract these and successfully move onto the next value. At present if i come across a null value i dont know how to detect this and move onto the next item. Can anyone help at all Thanks Simon

      6F 72 67 61 6E 69 73 61 74 69 6F 6E 49 64 3D 33
      26 10 00 00 00 50 65 6F 70 6C 65 50 65 6F 70 6C
      65 4C 69 6E 6B 6A 08 00 00 09 00 00 00 0E 00 00
      00 53 65 6E 69 6F 72 50 65 72 73 6F 6E 49 64 0E
      00 00 00 4A 75 6E 69 6F 72 50 65 72 73 6F 6E 49
      64 11 00 00 00 53 65 6E 69 6F 72 50 65 72 73 6F
      6E 4D 69 73 49 64 11 00 00 00 4A 75 6E 69 6F 72
      50 65 72 73 6F 6E 4D 69 73 49 64 08 00 00 00 4C
      69 6E 6B 54 79 70 65 16 00 00 00 50 61 72 65 6E
      74 61 6C 52 65 73 70 6F 6E 73 69 62 69 6C 69 74
      79 08 00 00 00 50 72 69 6F 72 69 74 79 0B 00 00
      00 4C 61 73 74 55 70 64 61 74 65 64 07 00 00 00
      44 65 6C 65 74 65 64 01 00 00 00 D7 0A 00 00 00
      00 00 00 00 00 00 00 00 00 00 00 00 01 80 C3 29
      C6 85 BF CE 08 00 02 00 00 00 3C 0C 00 00 00 00
      00 00 00 00 00 00 00 00 00 00 00 00 F0 83 76 C6
      85 BF CE 08 00 03 00 00 00 3C 0C 00 00 00 00 00
      00 00 00 00 00 00 00 00 00 00 00 C0 0E 76 C6 85
      BF CE 08 00 04 00 00 00 67 0B 00 00 00 00 00 00
      00 00 00 00 00 00 00 00 00 01 70 CA 4B C6 85 BF
      CE 08 00 05 00 00 00 67 0B 00 00 00 00 00 00 00
      00 00 00 00 00 00 00 00 02 70 CA 4B C6 85 BF CE
      08 00 06 00 00 00 9B 08 00 00 00 00 00 00 00 00
      00 00 00 00 00 00 00 02 C0 BE B2 C5 85 BF CE 08
      00 06 00 00 00 AD 09 00 00 00 00 00 00 00 00 00
      00 00 00 00 00 00 02 10 6D F5 C5 85 BF CE 08 00
      06 00 00 00 3F 0A 00 00 00 00 00 00 00 00 00 00
      00 00 00 00 00 02 80 59 11 C6 85 BF CE 08 00 07
      00 00 00 E3 0A 00 00 00 00 00 00 00 00 00 00 00
      00 00 00 00 01 C0 D0 2C

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      I may be misunderstanding this, but wouldn't you just check it see if variable is 0 and get the next Int32 if it was??

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • S si_69

        Hi All I am trying to loop through a byte array (sample below) reading the values to then insert those into a database I loop through keeping track of the position

        byte[] data;
        long position=0;
        while (position < data.Length)
        {
        position = Conversion.Extract(data, out mydata, position);
        position = Conversion.Extract(data, out nextitem, position);
        }

        public static long Extract(byte\[\] message, out int variable, long position) 
        { 
            const int length = 4; // the length of an int 
        
            if (message.Length >= position + length) 
            { 
                variable = BitConverter.ToInt32(message, (int)position); 
                return position + length; 
            } 
        
            // there was a problem 
            variable = 0; 
            return -1; 
        } 
        

        The data i have contains some null values, how can i extract these and successfully move onto the next value. At present if i come across a null value i dont know how to detect this and move onto the next item. Can anyone help at all Thanks Simon

        6F 72 67 61 6E 69 73 61 74 69 6F 6E 49 64 3D 33
        26 10 00 00 00 50 65 6F 70 6C 65 50 65 6F 70 6C
        65 4C 69 6E 6B 6A 08 00 00 09 00 00 00 0E 00 00
        00 53 65 6E 69 6F 72 50 65 72 73 6F 6E 49 64 0E
        00 00 00 4A 75 6E 69 6F 72 50 65 72 73 6F 6E 49
        64 11 00 00 00 53 65 6E 69 6F 72 50 65 72 73 6F
        6E 4D 69 73 49 64 11 00 00 00 4A 75 6E 69 6F 72
        50 65 72 73 6F 6E 4D 69 73 49 64 08 00 00 00 4C
        69 6E 6B 54 79 70 65 16 00 00 00 50 61 72 65 6E
        74 61 6C 52 65 73 70 6F 6E 73 69 62 69 6C 69 74
        79 08 00 00 00 50 72 69 6F 72 69 74 79 0B 00 00
        00 4C 61 73 74 55 70 64 61 74 65 64 07 00 00 00
        44 65 6C 65 74 65 64 01 00 00 00 D7 0A 00 00 00
        00 00 00 00 00 00 00 00 00 00 00 00 01 80 C3 29
        C6 85 BF CE 08 00 02 00 00 00 3C 0C 00 00 00 00
        00 00 00 00 00 00 00 00 00 00 00 00 F0 83 76 C6
        85 BF CE 08 00 03 00 00 00 3C 0C 00 00 00 00 00
        00 00 00 00 00 00 00 00 00 00 00 C0 0E 76 C6 85
        BF CE 08 00 04 00 00 00 67 0B 00 00 00 00 00 00
        00 00 00 00 00 00 00 00 00 01 70 CA 4B C6 85 BF
        CE 08 00 05 00 00 00 67 0B 00 00 00 00 00 00 00
        00 00 00 00 00 00 00 00 02 70 CA 4B C6 85 BF CE
        08 00 06 00 00 00 9B 08 00 00 00 00 00 00 00 00
        00 00 00 00 00 00 00 02 C0 BE B2 C5 85 BF CE 08
        00 06 00 00 00 AD 09 00 00 00 00 00 00 00 00 00
        00 00 00 00 00 00 02 10 6D F5 C5 85 BF CE 08 00
        06 00 00 00 3F 0A 00 00 00 00 00 00 00 00 00 00
        00 00 00 00 00 02 80 59 11 C6 85 BF CE 08 00 07
        00 00 00 E3 0A 00 00 00 00 00 00 00 00 00 00 00
        00 00 00 00 01 C0 D0 2C

        M Offline
        M Offline
        Matt Meyer
        wrote on last edited by
        #3

        When thinking in terms of bytes (or any integral types), null and 0 are the same. In your example, when this line

        variable = BitConverter.ToInt32(message, (int)position);

        returns a zero, that would correspond to a null value.

        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