How to convert byte array to any type

后端 未结 2 816
陌清茗
陌清茗 2020-12-06 04:50

okay guys I\'m seeing question from persons asking how to convert byte arrays to int, string, Stream, etc... and the answers to which

2条回答
  •  無奈伤痛
    2020-12-06 05:04

    c++ template version :

    template
    void fromByteArray(T& t, byte *bytes)
    {
      t = *(T*)bytes;
    };
    
    template
    byte * toByteArray(T& t) 
    {
      return (byte*)&t;
    };
    

提交回复
热议问题