1
주어진 바이트 배열을 int로 변환하고 싶습니다. 그런 다음 프로세스를 취소하고 싶습니다. 그 int에서 원래 바이트 배열을 다시 얻고 싶습니다. 나는 이런 일이 일한 것이라고 생각 : C# byte [] to long and back
byte[] myBytes = { 0, 0, 0, 32 };
if (BitConverter.IsLittleEndian)
Array.Reverse(myBytes);
int i = BitConverter.ToInt32(myBytes, 0);
Console.WriteLine("int: {0}", i); // Output: 32
byte[] newBytes = BitConverter.GetBytes(i);
Console.WriteLine("byte array: " + BitConverter.ToString(newBytes));
// Outputs: 20-00-00-00
그래서 원래 바이트 배열을 나에게 다시 제공하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
답변 해 주셔서 감사합니다. 나는 엔디안에 전혀 관심이 없으며 Array.reverse를 제거하면 00-00-00-20을 출력합니다. 원래 배열은 00-00-00-32 였으므로 원래의 배열은 여전히 얻지 못했습니다. 나중에 EndianBitConverter를 사용해 보겠습니다. – Jonathan
@Jonathan : 아니요, 원래 배열은''00-00-00-20 "'이었습니다. 왜냐하면'BitConverter.ToString'은 16 진수로 포맷하기 때문에 배열 초기화 자에서 * decimal * 리터럴을 사용하기 때문입니다. 'Console.WriteLine ("바이트 배열 :"+ BitConverter.ToString (myBytes));'를 시작하면 내가 무슨 뜻인지 알게 될 것이다. –
예, BitConverter.ToString이 나를 혼란스럽게 만들었습니다. 도와 줘서 고마워! – Jonathan