如何将一个 long 转换成一个byte[] ?

解决方案 »

  1.   

    long i=12356;
    byte []a= System.Text.Encoding.Default.GetBytes(i.ToString());
      

  2.   

    BitConverter.GetBytes Method (Int32)See Also
    BitConverter Class | BitConverter Members | System Namespace | BitConverter.GetBytes Overload List | BitConverter Members (Visual J# Syntax) | Managed Extensions for C++ Programming 
    Requirements
    Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
    Language
    C#C++JScriptVisual BasicShow All
    Returns the specified 32-bit signed integer value as an array of bytes.[Visual Basic]
    Overloads Public Shared Function GetBytes( _
       ByVal value As Integer _
    ) As Byte()
      

  3.   

    byte[] buf = new byte[4];
    MemoryStream s = new MemoryStream(buf);
    BinaryWriter w = new BinaryWriter(s);
    long n = 10;
    w.Write(n);//buf is your answer
      

  4.   

    /// <summary>
    ///  Int32 -> byte[4]
    /// </summary>
    /// <param name="intSource"></param>
    /// <returns></returns>
    public static byte[] Int32ToBytes (Int32 intSource)
    {
    byte[] byteRet = new byte [4];
    unsafe
    {
    byte* buffer = stackalloc byte[4];
    Int32 *p = &intSource;
    buffer = (byte* )p;
    for (int i = 0; i < 4; i++)
    {
    byteRet [i] = *buffer;
    buffer ++;

    }
    return byteRet; } }----------------
    四十年来家国,三千里地山河,凤阁龙楼连霄汉,玉树琼枝作烟箩。几曾识干戈?