如何在 crystal 中读取其他大小的切片?
How can I read a slice with other size in crystal?
我想将文件的第一个 2 字节读取为“unsigned int”。
我检查了线程
“”
我可以用下面的代码得到 1st 2bytes。
File.open("./test/test_data") do |io|
buffer = Slice(UInt8).new(2)
bytes_read = io.read(buffer)
buffer = buffer[0, bytes_read]
pp buffer
end
但是,这段代码 returns "2 UInt8"
$ crystal test2.cr
buffer # => Slice[0, 6]
如何将“2 UInt8”读作“1 UInt16”?
File.open("test/test_data") do |io|
p UInt16.from_io(io, IO::ByteFormat::LittleEndian)
end
我想将文件的第一个 2 字节读取为“unsigned int”。
我检查了线程
“
File.open("./test/test_data") do |io|
buffer = Slice(UInt8).new(2)
bytes_read = io.read(buffer)
buffer = buffer[0, bytes_read]
pp buffer
end
但是,这段代码 returns "2 UInt8"
$ crystal test2.cr
buffer # => Slice[0, 6]
如何将“2 UInt8”读作“1 UInt16”?
File.open("test/test_data") do |io|
p UInt16.from_io(io, IO::ByteFormat::LittleEndian)
end