I am working with the [freeimage](https://nimble.directory/pkg/freeimage) nim library/wrapper, and have this code: proc compressJpeg(imgData: string): string = ## Use freeimage to compress the jpeg files # Open image var mem = FreeImage_OpenMemory(cast[ptr byte](imgData.cstring), imgData.len.uint32) var image = FreeImage_LoadFromMemory(FIF_JPEG, mem, JPEG_ACCURATE) FreeImage_CloseMemory(mem) # Save image let FI_DEFAULT = 0.uint8 var outmem = FreeImage_OpenMemory(addr FI_DEFAULT, FI_DEFAULT.uint32) doAssert FreeImage_SaveToMemory(FIF_JPEG, image, outmem, JPEG_OPTIMIZE).bool # to nim var buffer: ptr byte var length: uint32 doAssert FreeImage_AcquireMemory(outmem, addr buffer, addr length).bool let cs = cast[cstring](buffer) doAssert cs.len.uint32 == length # Ensure converison was sucessful result = $cs doAssert result.len.uint32 == length # Ensure converison was sucessful # Cleanup FreeImage_Unload(image) FreeImage_CloseMemory(outmem) Run
But `doAssert cs.len.uint32 == length` always fires, with cs being 4 bytes long? I assume I am converting `buffer` to a cstring in the wrong way, but there could be another issue. Is there a better way than cast to convert between ptr byte and cstring?