=================================
=================================
=================================
출처: 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]
=================================
=================================
=================================
출처: https://learder.tistory.com/827192
Buffer.BlockCopy
출처: 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
=================================
=================================
=================================
'프로그래밍 관련 > 언어들의 코딩들 C++ JAVA C# 등..' 카테고리의 다른 글
[C#] 소켓 통신 시 구조체,바이너리, 마샬링 등등 사용하기 관련 (0) | 2019.03.13 |
---|---|
[C#] C#의 클래스 혹은 구조체(class, struct)를 byte[]로 변환하는 방법 또는 반대로 (0) | 2019.03.13 |
[C#] Sizeof, Marshal.SizeOf Method 형 크기 SizeOf 관련 (0) | 2019.03.13 |
[C#] C++ 와 C# 형 비교 참조 Converting C++ Data Types to C# (0) | 2019.03.13 |
[C#] 읽기/쓰기 속성 선언 및 사용 - get, set 사용 관련 (0) | 2019.03.12 |
댓글 영역