2009-08-17 17 views
5

मेरे पास एक बाइट है [] और मैं इंट्स (और अन्य डेटा) की एक सूची के माध्यम से पुनरावृत्ति कर रहा हूं और मैं int को अपने बाइटएरे [इंडेक्स * 4] में कॉपी करना चाहता हूं। यह करो?int से बाइट कॉपी करने के लिए सबसे आसान तरीका []

उत्तर

17

BitConverter काफी संभवतः आपका मित्र है।

हालांकि, आमतौर पर आपको नया बाइट सरणी देता है। यह आपको अंतहीनता निर्दिष्ट करने नहीं देता है। मेरे पास EndianBitConverter कक्षा MiscUtil में है जिसमें डेटा को सीधे मौजूदा बाइट सरणी में कॉपी करके आदिम प्रकारों को परिवर्तित करने के तरीके हैं।

उदाहरण के लिए:

// Copy the bytes from the integer "value" into a byte array 
// (buffer) starting at index 5 
EndianBitConverter.Little.CopyBytes(value, buffer, 5); 
+0

वाह, आप हर दिन कुछ नया सीखते हैं :) यह भी पता नहीं था कि अस्तित्व में था! – Gareth

+1

स्थानांतरण, स्थानांतरण दृष्टिकोण से बेहतर, क्योंकि आपको एंडियन-नेस के बारे में सोचने की आवश्यकता नहीं है। (बेशक, उसे बदलने की जरूरत नहीं है)। –

8

कर कैसे BinaryWriter यह करता है:

buffer[0] = (byte) value; 
buffer[1] = (byte) (value >> 8); 
buffer[2] = (byte) (value >> 0x10); 
buffer[3] = (byte) (value >> 0x18); 

0
byte[] bytes = new byte[listOfInts.Count * sizeof(int)]; 
int pos = 0; 
foreach(int i in listOfInts) 
{ 
    byte[] b = BitConverter.GetBytes(i); 
    b.CopyTo(bytes, pos); 
    pos += b.Length; 
} 
(जाहिर है इस सरणी के पहले 4 बाइट्स में पूर्णांक कॉपी जाएगा)
1

बफर। ब्लॉककॉपी (इंट्रायरे, 0, बाइटएरे, 0, 4 * इंटरेरे। लम्बाई)

दो सरणी के बीच डेटा कॉपी करता है। आखिरी तर्क बाइट्स में कॉपी करने के लिए डेटा की मात्रा है।

6

ऐसा करने के कई अलग-अलग तरीके हैं, लेकिन इसे बेहतर बनाने के लिए, C#: एक्सटेंशन की एक नई सुविधा का उपयोग करने दें।

32 बिट विविधता का एक पूर्णांक 4 बाइट लेता है, इसलिए यह बाइट [4] में 4 स्पॉट्स पर कब्जा करेगा। आप अपने 4 घटक बाइट्स में एक पूर्णांक कैसे तोड़ते हैं? आप बिट मैनिपुलेशन ऑपरेटर >> का उपयोग करके इसे कर सकते हैं। वह ऑपरेटर बिट्स को निर्दिष्ट बिट्स द्वारा पूर्णांक में बदल देता है। उदाहरण के लिए:

integer = 10399 
binary = 00101000 10011111 
10399 >> 1 == 0010100 01001111 each bit shifted by 1 

के बाद से एक बाइट में 8 बिट है अगर हम 8 बिट द्वारा पूर्णांक बदलाव हम सही-सबसे बिट्स स्थिति

10399 >> 8 = 00000000 00101000 

सूचना में पूर्णांक के नए दूसरी बाइट मूल्य होगा दूसरा बाइट पहला बाइट नहीं है और बाकी बिट्स को 0

हम बाइट्स को पहली स्थिति में स्थानांतरित करने के लिए इस चाल का उपयोग कर सकते हैं, फिर बाइट को एक कलाकार को मजबूर कर सकते हैं, जो त्याग देगा 3 अन्य बाइट्स और हमें अंतिम बाइट्स मान छोड़ देंगे:

(byte)(10399 >> 8) == 0010100 

तो, 4 बाइट के लिए इस तकनीक का उपयोग करके हमसे हर एक के लिए उपयोग दे देंगे और हम एक गंतव्य सरणी कि हमारे नए CopyToByteArray विधि करने के लिए पारित किया गया था में उन्हें कॉपी जाएगा:

public static class Int32Extension 
{ 
    public static void CopyToByteArray(this int source, byte[] destination, int offset) 
    { 
     if (destination == null) 
      throw new ArgumentException("Destination array cannot be null"); 

     // check if there is enough space for all the 4 bytes we will copy 
     if (destination.Length < offset + 4) 
      throw new ArgumentException("Not enough room in the destination array"); 

     destination[offset] = (byte)(source >> 24); // fourth byte 
     destination[offset+1] = (byte)(source >> 16); // third byte 
     destination[offset+2] = (byte)(source >> 8); // second byte 
     destination[offset+3] = (byte)source; // last byte is already in proper position 
    } 
} 

ध्यान दें कि हम बाइट्स को रिवर्स ऑर्डर में कॉपी कर सकते थे, जो आपके कार्यान्वयन पर निर्भर है।

विस्तार समारोह आप पूर्णांक वर्ग के एक सदस्य के रूप में नए समारोह का उपयोग करने की अनुमति देगा:

int something = 20; 
byte[] array = new byte[4]; 
something.CopyToByteArray(array,0); 
6

मैं कुछ नया बनाने के लिए पिछले जवाब में से कुछ को संक्षेप में प्रस्तुत करने की कोशिश करेंगे

चरण 1.Jon Skeet के रूप में कहा से पहले:

BitConverter काफी संभवतः अपने दोस्त है।

हालांकि, आमतौर पर आपको नया बाइट सरणी देता है।

चरण 2. आप BitConverter.GetBytes के स्रोत कोड (पूर्णांक मूल्य) विधि पा सकते हैं:

public static unsafe byte[] GetBytes(int value) 
{ 
    byte[] numArray = new byte[4]; 
    fixed (byte* numPtr = numArray) 
     *(int*) numPtr = value; 
    return numArray; 
} 

चरण 3. कल्पना का प्रयोग और से कोड की कुछ पंक्तियों में बदल रहा है

public static unsafe byte[] GetBytes(int value, int buffer[], int offset) 
{ 
    // Here should be a range check. For example: 
    // if (offset > buffer.Length + sizeof(int)) throw new IndexOutOfRangeException(); 

    fixed (byte* numPtr = &buffer[offset]) 
     *(int*) numPtr = value; 
} 
0

सामान्यीकरण एडीबी के exquisit जवाब:

चरण 2 तो यह एक मौजूदा सरणी के लिए डेटा को बचा सकता है
public static class Int32Extension 
{ 

    /// <summary> 
    /// Copies an int to a byte array: Byte order and sift order are inverted. 
    /// </summary> 
    /// <param name="source">The integer to convert.</param> 
    /// <param name="destination">An arbitrary array of bytes.</param> 
    /// <param name="offset">Offset into the desination array.</param> 
    public static void CopyToByteArray(this int source, byte[] destination, int offset) 
    { 
     if (destination == null) 
      throw new ArgumentException("Destination array cannot be null"); 

     // check if there is enough space for all the 4 bytes we will copy 
     if (destination.Length < offset + sizeof(int)) 
      throw new ArgumentException("Not enough room in the destination array"); 

     for (int i = 0, j = sizeof(int) - 1; i < sizeof(int); i++, --j) { 
      destination[offset + i] = (byte) (source >> (8 * j)); 
     } 
    } 

    /// <summary> 
    /// Copies an int to a to byte array Little Endian: Byte order and sift order are the same. 
    /// </summary> 
    /// <param name="source">The integer to convert.</param> 
    /// <param name="destination">An arbitrary array of bytes.</param> 
    /// <param name="offset">Offset into the desination array.</param> 
    public static void CopyToByteArrayLE(this int source, byte[] destination, int offset) 
    { 
     if (destination == null) 
      throw new ArgumentException("Destination array cannot be null"); 

     // check if there is enough space for all the 4 bytes we will copy 
     if (destination.Length < offset + sizeof(int)) 
      throw new ArgumentException("Not enough room in the destination array"); 

     for (int i = 0, j = 0; i < sizeof(int); i++, j++) { 
      destination[offset + i] = (byte) (source >> (8 * j)); 
     } 
    } 
} 
संबंधित मुद्दे