字节流和结构体的转换[转]

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Management;

namespace Util
{
    
public class NetStreamUtil
    {
        
private static byte _fillChar = 0//the fill character
        
        
//convert string to byte array in Ascii with length is len        
        public static byte[] StringToBytes(string str, int len)
        {
            
if (string.IsNullOrEmpty(str))
            {
                str 
= string.Empty;
            }
 
            
byte[] result = new byte[len];
            
byte[] strBytes = Encoding.Default.GetBytes(str);

            
//copy the array converted into result, and fill the remaining bytes with 0
            for (int i = 0; i < len; i++)
                result[i] 
= ((i < strBytes.Length) ? strBytes[i] : _fillChar);
            
            
return result;
        }

        
/// <summary>
        
/// struct转换为byte[]
        
/// </summary>
        
/// <param name="structObj">结构体对象</param>
        
/// <returns>字节数组</returns>
        public static byte[] StructToBytes(object structObj)
        {
            
int size = Marshal.SizeOf(structObj);
            IntPtr buffer 
= Marshal.AllocHGlobal(size);
            
try
            {
                Marshal.StructureToPtr(structObj, buffer, 
false);
                
byte[] bytes = new byte[size];
                Marshal.Copy(buffer, bytes, 
0, size);
                
return bytes;
            }
            
finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }

       
/// <summary>
       
///  byte[]转换为struct
       
/// </summary>
       
/// <param name="bytes"></param>
       
/// <param name="strcutType"></param>
       
/// <returns></returns>
       public static object BytesToStruct(byte[] bytes, Type strcutType)   
       {   
             
int size = Marshal.SizeOf(strcutType);   
             IntPtr buffer 
= Marshal.AllocHGlobal(size);   
             
try   
             {   
                Marshal.Copy(bytes, 
0, buffer, size);   
                
return Marshal.PtrToStructure(buffer, strcutType);   
             }   
             
finally   
             {   
                Marshal.FreeHGlobal(buffer);   
             }   
        } 

        
/// <summary>
        
/// IntPtr转换成byte[]
        
/// </summary>
        
/// <param name="dataBuf"></param>
        
/// <param name="length"></param>
        
/// <returns></returns>
        public byte[] ConvertToBytes(IntPtr dataBuf, int length)
        {
            
byte[] byteBuf = new byte[length];
            Marshal.Copy(dataBuf, byteBuf, 
0, length);
            
return byteBuf;
        }

        
/// <summary>
        
/// 获得网卡号
        
/// </summary>
        
/// <returns></returns>
        public static string GetNetCardMacAddress()
        {
            ManagementClass mc 
= new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc 
= mc.GetInstances();
            
string str = "";
            
foreach (ManagementObject mo in moc)
            {
                
if ((bool)mo["IPEnabled"== true)
                    str 
= mo["MacAddress"].ToString();
            }
            
return str;
        }
    }
}
posted @ 2009-09-18 00:06  因是因非  阅读(791)  评论(0编辑  收藏  举报