[Gambas-user] pb loading an picture into a string

Doriano Blengino doriano.blengino at ...1909...
Thu Nov 26 14:03:20 CET 2009


Jean-Yves F. Barbier ha scritto:
> Hi,
>
> Here is a (very big) PB:
>
> ...
>   ' Get picture /path/name.ext
>   INfile = FileChooser1.SelectedPath
>   
>   FileSize = Stat(FileChooser1.SelectedPath).Size
>   
>   ' Read file into a string
>   INstream = OPEN INfile FOR INPUT 
> '  READ #INstream, INstring, Lof(INstream)	' NA: Returns 238 bytes
>   READ #INstream, INstring, FileSize		' Erk: same thing :(
>   CLOSE #INstream
> PRINT "INstring length (Len, String.Len) = " & Len(INstring) & " - " & String.Len(INstring)
>
> Returns "298 - 234" when I read a small picture.png file!??
> How could I correctly read this image wothout any loss?
>
> Purpose is to read it correctly, convert it to base64 and send it to a postgresql
> procedure (and, of course, the opposite.)
>
> JY
>   
The old, normal operator len() returns the length of the string, which 
also *was* the number of characters.
The String.len() returns the number of character in an UTF-8 string - 
which can be less if some non-ascii char is present, because UTF-8 uses 
two or more *bytes* for them. On the doc's pages of String there is a 
nice example (I can not do the same example - my name is only formed by 
ascii chars...)

So, if you are dealing with UTF *text*, use String.XXXX. Otherwise, and 
especially if you use strings as memory buffer, use normal functions. I 
don't know is gambas strings deal correctly with NULLS - if not, then it 
is dangerous to use them as binary buffers. But the doc's page about 
Streams there is also an example on how to read a binary file. So I also 
deduce that gambas strings deal correctly with nulls.

Regards,
Doriano





More information about the User mailing list