如何将字节数组转换为任何类型

时间:2022-01-15 16:10:12

okay guys I'm seeing question from persons asking how to convert byte arrays to int, string, Stream, etc... and the answers to which are all varying and I have personally not found any satisfactory answers.

好吧伙计们,我看到有人询问如何将字节数组转换为int,string,Stream等等的问题......而且答案都各不相同,我个人没有找到任何满意的答案。

So here are some types that we want to convert an array of bytes to.

所以这里有一些我们想要将字节数组转换为的类型。

UnityEngine.Font which can take in ttf data.

UnityEngine.Font可以接收ttf数据。

UnityEngine.Testure2D which h can take in data from image files like .png, .jpg, etc...

UnityEngine.Testure2D,它可以从图像文件中获取数据,如.png,.jpg等...

How would we convert a byte array to a String, UnityEngine.Testure2D,UnityEngine.Font, Bitmap, etc...

我们如何将字节数组转换为String,UnityEngine.Testure2D,UnityEngine.Font,Bitmap等...

The data that populates the byte array must be from a file type whose data can by managed by the type we want to convert the byte array to?

填充字节数组的数据必须来自一个文件类型,其数据可以通过我们要将字节数组转换为的类型来管理?

Is this currently possible?

这目前可能吗?

Any help would be appreciated.

任何帮助,将不胜感激。

1 个解决方案

#1


19  

Primitive types are easy because they have a defined representation as a byte array. Other objects are not because they may contain things that cannot be persisted, like file handles, references to other objects, etc.

原始类型很容易,因为它们具有定义的表示形式作为字节数组。其他对象不是因为它们可能包含无法持久化的内容,例如文件句柄,对其他对象的引用等。

You can try persisting an object to a byte array using BinaryFormatter:

您可以尝试使用BinaryFormatter将对象持久化到字节数组:

public byte[] ToByteArray<T>(T obj)
{
    if(obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    using(MemoryStream ms = new MemoryStream())
    {
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }
}

public T FromByteArray<T>(byte[] data)
{
    if(data == null)
         return default(T);
    BinaryFormatter bf = new BinaryFormatter();
    using(MemoryStream ms = new MemoryStream(data))
    {
        object obj = bf.Deserialize(ms);
        return (T)obj;
    }
}

But not all types are serializable. There's no way to "store" a connection to a database, for example. You can store the information that's used to create the connection (like the connection string) but you can't store the actual connection object.

但并非所有类型都可序列化。例如,没有办法“存储”与数据库的连接。您可以存储用于创建连接的信息(如连接字符串),但不能存储实际的连接对象。

#1


19  

Primitive types are easy because they have a defined representation as a byte array. Other objects are not because they may contain things that cannot be persisted, like file handles, references to other objects, etc.

原始类型很容易,因为它们具有定义的表示形式作为字节数组。其他对象不是因为它们可能包含无法持久化的内容,例如文件句柄,对其他对象的引用等。

You can try persisting an object to a byte array using BinaryFormatter:

您可以尝试使用BinaryFormatter将对象持久化到字节数组:

public byte[] ToByteArray<T>(T obj)
{
    if(obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    using(MemoryStream ms = new MemoryStream())
    {
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }
}

public T FromByteArray<T>(byte[] data)
{
    if(data == null)
         return default(T);
    BinaryFormatter bf = new BinaryFormatter();
    using(MemoryStream ms = new MemoryStream(data))
    {
        object obj = bf.Deserialize(ms);
        return (T)obj;
    }
}

But not all types are serializable. There's no way to "store" a connection to a database, for example. You can store the information that's used to create the connection (like the connection string) but you can't store the actual connection object.

但并非所有类型都可序列化。例如,没有办法“存储”与数据库的连接。您可以存储用于创建连接的信息(如连接字符串),但不能存储实际的连接对象。