주어진 C# 메서드 HexToByteArray는 16진수 문자열을 바이트 배열로 변환하는 기능을 수행합니다. 입력값으로 "01101184000204000200009BAC"을 제공했을 때, 이 메서드가 어떻게 작동하는지 분석해 보겠습니다.
public static byte[] HexToByteArray(this string hex)
{
byte[] array = Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
return array;
}
Enumerable.Range(0, hex.Length):
hex.Length - 1까지의 정수 시퀀스를 생성합니다.Where(x => x % 2 == 0):
Select(x => Convert.ToByte(hex.Substring(x, 2), 16)):
Convert.ToByte를 사용하여 10진수 바이트로 변환합니다.ToArray():
이제 입력값을 사용하여 변환 과정을 살펴보겠습니다.
"01101184000204000200009BAC"짝수 인덱스에서 두 자리씩 잘라서:
각 두 자리 16진수를 10진수 바이트로 변환:
00 -> 011 -> 1701 -> 118 -> 2400 -> 020 -> 3200 -> 002 -> 200 -> 000 -> 000 -> 09B -> 155AC -> 172따라서, 최종적으로 변환된 바이트 배열은 다음과 같습니다:
byte[] result = { 0, 17, 1, 24, 0, 32, 0, 2, 0, 0, 0, 155, 172 };
입력값 "01101184000204000200009BAC"를 바이트 배열로 변환한 결과는:
{ 0, 17, 1, 24, 0, 32, 0, 2, 0, 0, 0, 155, 172 }
이 배열이 최종 결과입니다. 추가적인 질문이나 더 알고 싶은 내용이 있으시면 말씀해 주세요!
이런 자료를 참고했어요.
[1] Steemit - 해시(Hash)와 해시 함수의 의미 (https://steemit.com/kr/@ohrak22/hash)
[2] 티스토리 - [ 정보처리기사 ] 테스트 오라클 - 쉿 코딩중 (https://itmoon.tistory.com/86)
[3] velog - [Java] 정숫값을 읽어 부호(양/음/0) 판정하기 (https://velog.io/@1109_haeun/Java-%EC%A0%95%EC%88%AB%EA%B0%92%EC%9D%84-%EC%9D%BD%EC%96%B4-%EB%B6%80%ED%98%B8%EC%96%91%EC%9D%8C0-%ED%8C%90%EC%A0%95%ED%95%98%EA%B8%B0)
[4] 티스토리 - [Python] 기초 함수(Function) - 우 주 신 - 티스토리 (https://ordo.tistory.com/28)
[5] IBM - DAY 스칼라 함수 (https://www.ibm.com/docs/ko/db2/11.5?topic=functions-day)
[6] Microsoft Support - 목표값 찾기를 통해 입력 값을 조정하여 원하는 결과 찾기 (https://support.microsoft.com/ko-kr/office/%EB%AA%A9%ED%91%9C%EA%B0%92-%EC%B0%BE%EA%B8%B0%EB%A5%BC-%ED%86%B5%ED%95%B4-%EC%9E%85%EB%A0%A5-%EA%B0%92%EC%9D%84-%EC%A1%B0%EC%A0%95%ED%95%98%EC%97%AC-%EC%9B%90%ED%95%98%EB%8A%94-%EA%B2%B0%EA%B3%BC-%EC%B0%BE%EA%B8%B0-320cb99e-f4a4-417f-b1c3-4f369d6e66c7)
[7] 티스토리 - 파이썬 함수 - 기본적인 4가지 형태 (https://white-polarbear.tistory.com/109)
[8] Slash libraries - mapValues (https://www.slash.page/ko/libraries/common/utils/src/mapValues.i18n)
[9] 티스토리 - 해시함수의 특징 및 정의 (https://withbabybird.tistory.com/6)
[10] 티스토리 - BadRequest 로 응답해서 입력 값 제한하기 - Code - 티스토리 (https://fors.tistory.com/456)
뤼튼 사용하러 가기 > https://agent.wrtn.ai/5xb91l