I think this question has popped before. I have used 
[http://nim-lang.org/docs/endians.html](http://forum.nim-lang.org///nim-lang.org/docs/endians.html).
 Just get the generic pointers to source and target and pass them to the 
standard lib, that will do. Something along these lines:
    
    
    import endians
    
    proc loadHigh*[T: SomeUnsignedInt](buff: openArray[byte], value: var T, 
offset: int = 0): bool =
      
      var i: pointer = addr buff[offset]
      var o: pointer = addr value
      
      result = true
      
      case sizeof(value)
            of 1:
                value = ord(buff.data[0])
            of 2:
                bigEndian16(o, i)
            of 4:
                bigEndian32(o, i)
            of 8:
                bigEndian64(o, i)
            else:
                result = false
    

Use littleEndian<NN>() if you need. Similarly, you could to a "store" function. 
You may also want to test the offset does not overflow your source.

Reply via email to