http://yann.lecun.com/exdb/mnist/ 에서 C #의 MNIST 데이터 세트를 구문 분석하고 있습니다.
Int32
바이너리 파일에서 첫 번째를 읽으려고 합니다.
FileStream fileS = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fileS);
int magicNumber = reader.ReadInt32();
하지만 말도 안되는 번호 인 50855936이 표시됩니다.
내가 사용한다면 File.ReadAllBytes()
buffer = File.ReadAllBytes(fileName);
그런 다음 바이트를 살펴보면 잘 작동합니다 (처음 4 바이트는 이제 2049를 나타냅니다). BinaryReader에서 무엇을 잘못 했습니까?
파일 형식은 다음과 같습니다 (첫 번째 매직 넘버를 읽으려고합니다).
All the integers in the files are stored in the MSB first (high endian) format used by most non-Intel processors. Users of Intel processors and other low-endian machines must flip the bytes of the header.
훈련 세트 레이블 파일 (train-labels-idx1-ubyte) :
[offset] [type] [value] [description]
0000 32 bit integer 0x00000801(2049) magic number (MSB first)
0004 32 bit integer 60000 number of items
0008 unsignebyte ?? label
0009 unsigned byte ?? label
........
xxxx unsigned byte ?? label
The labels values are 0 to 9.d
50855936 == 0x03080000. 또는 리틀 엔디안이 계란 전쟁에서 승리했기 때문에 거의 모든 컴퓨터에 필요한 바이트를 뒤집을 때 0x00000803입니다. 2049 년에 가까워서 2의 오프셋을 설명하는 것이 무엇인지 잘 모릅니다. 다음은이를 읽는 데 도움이되는 확장 방법입니다.
public static class BigEndianUtils {
public static int ReadBigInt32(this BinaryReader br) {
var bytes = br.ReadBytes(sizeof(Int32));
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
return BitConverter.ToInt32(bytes, 0);
}
}
파일에 더 많은 필드 유형이 포함 된 경우 추가 메서드를 추가하고 코드 조각에서 Int32로 대체하면됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다