List<byte[]> help please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #1

    List<byte[]> help please

    I'm writing a program that has a certain specific type of input file. This input file contains information about the program that created it. The information is always at the same offset in the file and is always the same number of bytes (4), but I only want to allow files that were made by certain programs. An example would be:

    4D 53 88 5C

    That's one of the acceptable values, but there are multiple others I'd like to accept, and a lot more that I don't want to accept.

    I'm having trouble wrapping my head around creating a list of byte arrays, though. What I want to do is read the value from the file as a byte array then use:

    acceptList.Cont ains(byte[]);

    if the value I read from the file is contained in the array it'll return true, else it'll return false.

    Since I know the byte values of the acceptable program ids, is there a way I can put those byte arrays into the list?

    I understand that an alternative would be comparing the int32 value of the 4 bytes, would that be a better option to try than comparing byte arrays?
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by HaLo2FrEeEk
    I'm writing a program that has a certain specific type of input file. This input file contains information about the program that created it. The information is always at the same offset in the file and is always the same number of bytes (4), but I only want to allow files that were made by certain programs. An example would be:

    4D 53 88 5C

    That's one of the acceptable values, but there are multiple others I'd like to accept, and a lot more that I don't want to accept.

    I'm having trouble wrapping my head around creating a list of byte arrays, though. What I want to do is read the value from the file as a byte array then use:

    acceptList.Cont ains(byte[]);

    if the value I read from the file is contained in the array it'll return true, else it'll return false.

    Since I know the byte values of the acceptable program ids, is there a way I can put those byte arrays into the list?

    I understand that an alternative would be comparing the int32 value of the 4 bytes, would that be a better option to try than comparing byte arrays?
    you can do this List<List<byte> > that's a 2D list

    Comment

    • Monomachus
      Recognized Expert New Member
      • Apr 2008
      • 127

      #3
      Originally posted by HaLo2FrEeEk
      I'm writing a program that has a certain specific type of input file. This input file contains information about the program that created it. The information is always at the same offset in the file and is always the same number of bytes (4), but I only want to allow files that were made by certain programs. An example would be:

      4D 53 88 5C

      That's one of the acceptable values, but there are multiple others I'd like to accept, and a lot more that I don't want to accept.

      I'm having trouble wrapping my head around creating a list of byte arrays, though. What I want to do is read the value from the file as a byte array then use:

      acceptList.Cont ains(byte[]);

      if the value I read from the file is contained in the array it'll return true, else it'll return false.

      Since I know the byte values of the acceptable program ids, is there a way I can put those byte arrays into the list?

      I understand that an alternative would be comparing the int32 value of the 4 bytes, would that be a better option to try than comparing byte arrays?
      Since I know the byte values of the acceptable program ids, is there a way I can put those byte arrays into the list?
      Yes why not? Also you may consider transforming bytes in string representation using BitConverter, and than search whose strings in List<string>.
      But really why don't you just compare these.

      Ok so this is what I wrote:
      Code:
      public static void Main()
              {
                  byte[] anotherInput = new byte[4];
      
                  anotherInput[0] = 0x4A;
                  anotherInput[1] = 0x53;
                  anotherInput[2] = 0x88;
                  anotherInput[3] = 0x5C;
      
                  Console.WriteLine(string.Format("Is {0} accepted ? {1}", BitConverter.ToString(anotherInput),
                      AcceptProgramInput(anotherInput)));
                  
                  Console.ReadKey();
      }
      
              private static bool AcceptProgramInput(byte[] programInfo)
              {
                  byte[] allowedInput = new byte[4];
      
                  allowedInput[0] = 0x4D;
                  allowedInput[1] = 0x53;
                  allowedInput[2] = 0x88;
                  allowedInput[3] = 0x5C;
      
                  List<string> acceptedProgramsList = new List<string>();
      
                  string strVersion = BitConverter.ToString(allowedInput);
      
                  acceptedProgramsList.Add(strVersion);
      
                  return acceptedProgramsList.Contains(BitConverter.ToString(programInfo));
              }
      BitConverter converts your array of bytes in something like this "XX-YY-ZZ-WW", so you should store your values in List<string> in the same format.

      And the output
      Code:
      Is 4A-53-88-5C accepted ? False

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        Eh, I just went ahead and used the int32 value instead, so my example value of 4D 53 88 5C translated to 1297320028 in Big Endian, and I compare my value I'm reading from the file to the list with that in it. So here's my List:

        List<int> allow = new List<int>() { 1297287142, 1297287287, 1297320028 };

        And here's the code I use to test it:

        Code:
        if (!allow.Contains(programID))
                    {
                        errorSet("Sorry, this program isn't supported.", "Program not supported");
                        return false; // This isn't one of the 3 supported programs, exit the function
                    }
        That worked out nicely.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          If going that route, I would be use to use UInt32, it probably shouldn't matter, but you never know.

          Also a List<byte[]> should have worked just fine?
          Well, actually the contains() function would not work, as it behaves differently on native-y type objects (ints, doubles, strings, etc) As byte[] is really just an instance of the Array class tuned to a specific type.

          Comment

          Working...