[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

Converting from byte arrays to objects

I'm reading a stream of bytes into an application, and need to convert that data to an object. Deserialization is not an option here as the stream does not hold any metadata on the object.

Rather than crafting a method to read the stream into an array of bytes and then allocate each byte by hand to where it should go (assuming I can even do this), is there some magical class I don't know about that can take in a byte array and a Struct, and will do the transformation for me?

Please note...the stream holds data of varying types (ints, longs, strings) - it is not just a text stream unfortunately, so I can't use text decoding.

Many thanks for your help
0
gamesmeister
Asked:
gamesmeister
1 Solution
 
Tony McCreathTechnical SEO ConsultantCommented:
We would need to know how the data was created before we can suggest how to read it.
0
 
gamesmeisterAuthor Commented:
In this particular case it's a LOGFONT object stored in the windows registry
0
 
OBonioCommented:
Interesting idea. I think you could do this with reflection.  Not all the types covered here and string would need a bit more calculation doing on it (I guess it's null terminated?).

using System.Reflection;
 
public struct MyStruct
{
    int myInt;
    uint myUint;
    Int16 myInt16;
    UInt16 myUint16;
    Int64 myInt64;
    UInt64 myUint64;
}
 
byte[] data = { 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6 };
MyStruct str = (MyStruct)ConvertToStruct(typeof(MyStruct), data);
 
private static object ConvertToStruct(Type type, byte[] data)
{
    FieldInfo[] fis = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
    object retVal = Activator.CreateInstance(type);
    int streamPointer = 0;
    foreach (FieldInfo fi in fis)
    {
        object val = null;
        byte fieldSize = 0;
        #region Get type and size
        switch (fi.FieldType.Name.ToUpper())
        {
            case "STRING":
                //tricky one this
                break;
            case "UINT16":
            case "INT16":
                fieldSize = 2;
                break;
            case "UINT32":
            case "INT32":
                fieldSize = 4;
                break;
            case "UINT64":
            case "INT64":
                fieldSize = 8;
                break;
        }
        #endregion 
 
        if (fieldSize > 0)
        {
            byte[] tmpArr = new byte[fieldSize];
            Array.Copy(data, streamPointer, tmpArr, 0, fieldSize);
            streamPointer += fieldSize;
 
            #region Convert data
            switch (fi.FieldType.Name.ToUpper())
            {
                case "STRING":
                    //tricky one this
                    break;
                case "INT16":
                    val = BitConverter.ToInt16(tmpArr, 0);
                    break;
                case "INT32":
                    val = BitConverter.ToInt32(tmpArr, 0);
                    break;
                case "INT64":
                    val = BitConverter.ToInt64(tmpArr, 0);
                    break;
                case "UINT16":
                    val = BitConverter.ToUInt16(tmpArr, 0);
                    break;
                case "UINT32":
                    val = BitConverter.ToUInt32(tmpArr, 0);
                    break;
                case "UINT64":
                    val = BitConverter.ToUInt64(tmpArr, 0);
                    break;
            }
            #endregion
        }
 
        if (val!=null)
            fi.SetValue(retVal, val);
    }
    return retVal;
}

Open in new window

0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now