简体   繁体   中英

Reading null values in a byte array c#

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;
position = Conversion.Extract(data, out mydata, position);
position = Conversion.Extract(data, out nextitem, position);

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 don't know how to detect this and move onto the next item. Can anyone help at all

The Extract code is:

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;
    }

    variable = 0;
    return -1;
}

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 C6 85 BF CE 08 00 08 00
00 00 9B 08 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 01 C0 BE B2 C5 85 BF CE 08 00 08 00 00
00 AD 09 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 01 10 6D F5 C5 85 BF CE 08 00 08 00 00 00
C9 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 01 20 76 FA C5 85 BF CE 08 00 08 00 00 00 3F
0A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 B0 CE 11 C6 85 BF CE 08 00 09 00 00 00 E3 0A
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
90 5B 2C C6 85 BF CE 08 00 0A 00 00 00 C9 09 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 20
76 FA C5 85 BF CE 08 00 0B 00 00 00 F6 08 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 01 E0 C9
D2 C5 85 BF CE 08 00 0C 00 00 00 F6 08 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 02 F0 2C C9
C5 85 BF CE 08 00 0E 00 00 00 91 09 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 01 C0 C7 EF C5
85 BF CE 08 00 0F 00 00 00 91 09 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 02 90 52 EF C5 85
SeniorPersonId | JuniorPersonId | SeniorPersonMisId | JuniorPersonMisId | LinkType | ParentalResponsibility | Priority | LastUpdated              Deleted
1              | 2775           | {null}            | {null}            | {null}   | 0                      | 1        | 2012-02-22 14:31:18.200 | {null}
2              | 3132           | {null}            | {null}            | {null}   | 0                      | {null}   | 2012-02-22 14:31:18.703 | {null}
3              | 3132           | {null}            | {null}            | {null}   | 0                      | {null}   | 2012-02-22 14:31:18.700 | {null}
4              | 2919           | {null}            | {null}            | {null}   | 0                      | 1        | 2012-02-22 14:31:18.423 | {null}

I decoded the bytes to ASCII and got

"o", "r",  "g", "a", "n", "i", "s", "a", "t", "i", "o", "n", "I", "d", "=", "3", "&", "\x10", "\x00", "\x00", "\x00", 
"P", "e", "o", "p", "l", "e", "P", "e", "o", "p", "l", "e", "L", "i", "n", "k", "j", "\b", "\x00", "\x00", "\t", "\x00", "\x00", "\x00", "\x0E", "\x00", "\x00", "\x00", 
"S", "e", "n", "i", "o", "r", "P", "e", "r", "s", "o", "n", "I", "d", "\x0E", "\x00", "\x00", "\x00", 
"J", "u", "n", "i", "o", "r", "P", "e", "r", "s", "o", "n", "I", "d", "\x11", "\x00", "\x00", "\x00", 
"S", "e", "n", "i", "o", "r", "P", "e", "r", "s", "o", "n", "M", "i", "s", "I", "d", "\x11", "\x00", "\x00", "\x00", 
"J", "u", "n", "i", "o", "r", "P", "e", "r", "s", "o", "n", "M", "i", "s", "I", "d", "\b", "\x00", "\x00", "\x00", 
"L", "i", "n", "k", "T", "y", "p", "e", "\x16", "\x00", "\x00", "\x00", 
"P", "a", "r", "e", "n", "t", "a", "l", "R", "e", "s", "p", "o", "n", "s", "i", "b", "i", "l", "i", "t", "y", "\b", "\x00", "\x00", "\x00", 
"P", "r", "i", "o", "r", "i", "t", "y", "\v", "\x00", "\x00", "\x00", 
"L", "a", "s", "t", "U", "p", "d", "a", "t", "e", "d", "\a", "\x00", "\x00", "\x00", 
"D", "e", "l", "e", "t", "e", "d", 
"\x01", "\x00", "\x00", "\x00", "\xD7", "\x0A", "\x00", "\x00", "\x00", "\x00", ...

The actual data seems to start after "D", "e", "l", "e", "t", "e", "d", and SeniorPersonId seems to be a 32 bit integer. I base that on the fact that the following "\\xD7", "\\x0A", "\\x00", "\\x00" => 0x0AD7 => 2775 and that is the same as the JuniorPersonId in your example above.

"\x01", "\x00", "\x00", "\x00", // SeniorPersonId
"\xD7", "\x0A", "\x00", "\x00", // JuniorPersonId = 2775
"\x00", "\x00", "\x00", "\x00", // SeniorPersonMisId
"\x00", "\x00", "\x00", "\x00", // JuniorPersonMisId
"\x00", "\x00", "\x00", "\x00", // LinkType
"\x00", // ParentalResponsibility
"\x01", // Priority
"\x80", "\xC3", ")", "\xC6", "\x85", "\xBF", "\xCE", "\b", // LastUpdated
"\x00", // Deleted
"\x02", "\x00", "\x00", "\x00", // SeniorPersonId
"\x3C", "\x0C", "\x00", "\x00", // JuniorPersonId = 3132
"\x00", "\x00", "\x00", "\x00", // SeniorPersonMisId
"\x00", "\x00", "\x00", "\x00", // JuniorPersonMisId
"\x00", "\x00", "\x00", "\x00", // LinkType
"\x00", // ParentalResponsibility
"\x00", // Priority
"\xF0", "\x83", "v", "\xC6", "\x85", "\xBF", "\xCE", "\b", // LastUpdated
"\x00", // Deleted

An educated guess is that if all bytes of the field is 0x00 then it is null.

The fields are obviously of different length. You will have to parse them accordingly.

Here's an example on how you can go about to parse it.

public class Person
{
    public int SeniorPersonId {get;set;}
    public int JuniorPersonId {get;set;}
    public int? SeniorPersonMisId {get;set;}
    public int? JuniorPersonMisId {get;set;}
    public int? LinkType {get;set;}
    public byte ParentalResponsibility {get;set;}
    public byte? Priority {get;set;}
    public DateTime LastUpdated {get;set;}
    public byte? Deleted {get;set;}
}

void Main()
{
    var message = new byte[] 
    {
        0x01, 0x00, 0x00, 0x00, // SeniorPersonId
        0xD7, 0x0A, 0x00, 0x00, // JuniorPersonId = 2775
        0x00, 0x00, 0x00, 0x00, // SeniorPersonMisId
        0x00, 0x00, 0x00, 0x00, // JuniorPersonMisId
        0x00, 0x00, 0x00, 0x00, // LinkType
        0x01, // ParentalResponsibility
        0x01, // Priority
        0x80, 0xC3, 0x29, 0xC6, 0x85, 0xBF, 0xCE, 0x08, // LastUpdated
        0x00, // Deleted
    };

    var person = new Person();
    int pos = 0;

    person.SeniorPersonId = BitConverter.ToInt32(message, pos);
    person.JuniorPersonId = BitConverter.ToInt32(message, pos + 4);
    person.SeniorPersonMisId = BitConverter.ToInt32(message, pos + 8);
    if (person.SeniorPersonMisId == 0) person.SeniorPersonMisId = null;
    person.ParentalResponsibility = message[pos + 20];
    person.LastUpdated = DateTime.FromBinary( BitConverter.ToInt64(message, pos + 22) );
}

Creates a person object with:

SeniorPersonId 1
JuniorPersonId 2775
SeniorPersonMisId null  
JuniorPersonMisId null  
LinkType null  
ParentalResponsibility 1
Priority null  
LastUpdated 2012-02-22 14:31:18
Deleted null  

Now you just have to add the rest of the properties and create a parse method. I'll leave that to you.

Your extraction logic seems to be fine but return -1 would definitely make next call to Extract break. This might be the source of the problem. Since you're calling Extract twice before checking if you have gone over the data.Length, you probably have odd number if Int32 values in your table - first Extract returns -1 and next call to Extract fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM