C#操作內(nèi)存讀寫方法淺析
作者:佚名
C#操作內(nèi)存讀寫方法是什么呢?C#操作內(nèi)存是如何實現(xiàn)讀寫的呢?那么本文就向你介紹具體的實施實現(xiàn)。
C#操作內(nèi)存讀寫方法是什么呢?讓我們來看看具體的實例實現(xiàn):
- using System.Runtime.InteropServices;
- using System.Text;
- public class Function
- {
- //C#操作內(nèi)存讀寫方法
- public static byte PtrToByte( int Ptr )
- {
- byte b = Marshal.ReadByte( ( IntPtr ) Ptr );
- return b;
- }
- public static char PtrToChar( int Ptr )
- {
- byte b = Marshal.ReadByte( ( IntPtr ) Ptr );
- return ( char ) b;
- }
- public static short PtrToShort( int Ptr )
- {
- short b = Marshal.ReadInt16( ( IntPtr ) Ptr );
- return b;
- }
- //C#操作內(nèi)存讀寫方法
- public static ushort PtrToUShort( int Ptr )
- {
- ushort b = ( ushort ) Marshal.ReadInt16( ( IntPtr ) Ptr );
- return b;
- }
- public static int PtrToInt( int Ptr )
- {
- int b = Marshal.ReadInt32( ( IntPtr ) Ptr );
- return b;
- }
- public static uint PtrToUInt( int Ptr )
- {
- uint b = ( uint ) Marshal.ReadInt32( ( IntPtr ) Ptr );
- return b;
- }
- public static long PtrToLong( int Ptr )
- {
- long b = Marshal.ReadInt64( ( IntPtr ) Ptr );
- return b;
- } //C#操作內(nèi)存讀寫方法
- public static ulong PtrToULong( int Ptr )
- {
- ulong b = ( ulong ) Marshal.ReadInt64( ( IntPtr ) Ptr );
- return b;
- }
- // Convert an ip address stored an address to equivalent string value
- public static string GetPtrToIpAddr(int intPtr, int varlen)
- {
- int i = 0;
- StringBuilder sb = new StringBuilder(0,varlen*4);
- byte[] byx = new byte[varlen];
- // ip address cann't have zero value C#操作內(nèi)存讀寫方法
- // ip address cann't have zero length C#操作內(nèi)存讀寫方法
- if( ( intPtr == 0 ) || ( varlen == 0 ) ) return "";
- Marshal.Copy( ( IntPtr ) intPtr , byx , 0 , varlen );
- for( i = 0; i < varlen - 1; i ++ )
- {
- sb.Append(byx[i]);
- sb.Append('.');
- }
- sb.Append(byx[varlen - 1]);
- return sb.ToString();
- }
- }
C#操作內(nèi)存讀寫方法的基本內(nèi)容就向你介紹到這里,希望對你了解和學習C#操作內(nèi)存讀寫方法有所幫助。
【編輯推薦】
責任編輯:仲衡
來源:
it55.com