PMunch / binaryparse

Binary parser for Nim
MIT License
71 stars 6 forks source link

parsing binary incorrectly #7

Closed mhessler97 closed 3 years ago

mhessler97 commented 3 years ago

when reading in uint16s it appear to be reading them in backwards as all of the numbers I am getting seem to be bit shifted 8 to the left instead of where they should be. Maybe I am doing something wrong. Here is the code I am testing it on.

import streams
import binaryparse

createParser(las):
    s4:sig = "LASF"
    u16:id
    u16:encoding
    u32:data1
    u16:data2
    u16:data3
    s8:data4
    s1:major
    s1:minor
    s32:sysID
    s32:software
    u16:daycreated
    u16:yearcreated
    u16:headersize

var strm = newFileStream("NEONDSSampleLiDARPointCloud.las", fmread)

https://www.asprs.org/wp-content/uploads/2010/12/LAS_1_4_r13.pdf is the specifications for parsing. and the dataset I am testing it on can be found here https://figshare.com/articles/dataset/NEON_Teaching_Data_LiDAR_Point_Cloud_las_Data/4307750

when I try manually parsing the code I am able to get correct numbers but the numbers I am getting from the generated parser are wrong. for ID the value should be 101 but i am getting 25856 instead. I can share the manual parser if that is needed. Thank you

PMunch commented 3 years ago

This is because binaryparse is strictly big endian, and that specification says numbers are in little endian. Simply do an endianess swap with the endians module and you should get the values you expect.