상세 컨텐츠

본문 제목

[C#] Buffer.BlockCopy 으로 byte[] 데이터를 memmove, memcpy()처럼 이용 관련

본문

반응형

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

출처: https://netmaid.tistory.com/60

 

 

C# 에서 C의 memcpy() 역할과 동일한 함수가 Buffer.BlockCopy() 함수이다.

그러면, memmove() 를 대신할 수 있는 함수는 무엇일까?

아무리 찾아봐도 없는데, Buffer.BlockCopy() 로 대응가능하지 않을까?

 

다음은 이에 대한 테스트 코드이다.

	class Program 	{ 		static void Main(string[] args) 		{ 			byte[] tmp = new byte [20]; 			string ss = "012__________";  			System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); 			Buffer.BlockCopy(enc.GetBytes(ss), 0, tmp, 0, enc.GetByteCount(ss)); 			Console.WriteLine(BitConverter.ToString(tmp));  			Buffer.BlockCopy(tmp, 3, tmp, 0, 10); 			Console.WriteLine(BitConverter.ToString(tmp)); 		} 	} 

 

위를 실행한 결과는 다음과 같다.

30-31-32-5F-5F-5F-5F-5F-5F-5F-5F-5F-5F-00-00-00-00-00-00-00 5F-5F-5F-5F-5F-5F-5F-5F-5F-5F-5F-5F-5F-00-00-00-00-00-00-00 

 

tmp 의 3바이트 뒤에 있는 데이터를 앞으로 memmove() 한 형태이다.

Buffer.BlockCopy() 를 memcpy() 이든, memmove() 이든 자유롭게 쓰면 된다.

 

나중에 메뉴얼을 잘 읽어보니까 이 부분을 상세히 설명하고 있다.

참고:



출처: https://netmaid.tistory.com/60 [Service for Every Master]

 

 

=================================

=================================

=================================

 

 

 

반응형

 

728x90

 

 

 

 

출처: https://learder.tistory.com/827192

 

 

바이트 단위 바이너리 연산이 많아서 문제다...
C 에서는 memcpy 했는데, 이놈의 java 나 c# 에서는 어케 하나 했었다.
 
- MSDN 내용입니다. 참고하시기 바랍니다.

Buffer.BlockCopy
특정 오프셋에서 시작하는 소스 배열에서 특정 오프셋에서 시작하는 대상 배열로 지정된 바이트 수를 복사합니다.
public static void BlockCopy (
Array src,
int srcOffset,
Array dst,
int dstOffset,
int count
)
 
- 사용 예
Buffer.BlockCopy(src, srcOffset, dst, dstOffset, count)
 
매개 변수
src       : 소스 버퍼입니다.
srcOffset : src의 바이트 오프셋입니다.
dst   : 대상 버퍼입니다.
dstOffset : dst의 바이트 오프셋입니다.
count    : 복사할 바이트 수입니다.
 
 
public static void arraycopy(
           Object source,
   int srcIndex, 
           Object dest,
           int destIndex,
           int length
)
 
- 사용 예
public class ArrayCopyDemo {
 
    public static void main(String[] args) {
        char[] Src = { 'd', 'e', 'c', 'a', 'f', 'f', 'e','i', 'n', 'a', 't', 'e', 'd' };
        char[] dst = new char[7];
        System.arraycopy(src, 2, dst, 0, 7);
        System.out.println(new String(dst));
    }
}
 



출처: https://learder.tistory.com/827192 [Peaceful Lake]

 

 

=================================

=================================

=================================

 

 

 

Based on: .NET (2019)  C# program that uses BlockCopy on bytes  using System;  class Program {     static void Main()     {         byte[] arr1 = new byte[] { 1, 2, 3, 4, 5 };         byte[] arr2 = new byte[10];          // Copy the first five bytes from arr1 to arr2         Buffer.BlockCopy(arr1, 0, arr2, 0, 5);          Display(arr2);     }      static void Display(byte[] arr)     {         for (int i = 0; i < arr.Length; i++)         {             Console.Write(arr[i]);         }         Console.WriteLine();     } }

------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 static byte[] GetSourceArray() {     // [Note] You can populate data here     return new byte[_len]; }  Code that copies byte arrays: C#  const int _len = 1000;  Buffer.BlockCopy(source, 0, target, 0, _len);  Array.Copy(source, target, _len);  Benchmark results:  Buffer.BlockCopy: 1113 ms [faster] Array.Copy:       1343 ms

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

using System;  class Program {     static void Main()     {         // Use an array of three integers for testing.         // ... Loop through the bytes in the array and write their values.         int[] array1 = { 1, 1, 256 };         for (int i = 0; i < Buffer.ByteLength(array1); i++)         {             Console.WriteLine(Buffer.GetByte(array1, i));         }          // Set certain byte values at indexes in the array.         Buffer.SetByte(array1, 0, 55);         Buffer.SetByte(array1, 4, 55);         Buffer.SetByte(array1, 8, 55);          // Render the modified array.         Console.WriteLine("---");         for (int i = 0; i < Buffer.ByteLength(array1); i++)         {             Console.WriteLine(Buffer.GetByte(array1, i));         }     } }  Output  1 0 0 0 1 0 0 0 0 1 0 0 --- 55 0 0 0 55 0 0 0 55 1 0 0

 

=================================

=================================

=================================

 

 

반응형


관련글 더보기

댓글 영역