Alaska Software Inc. - File2binary string
Username: Password:
AuthorTopic: File2binary string
Thomas PoolFile2binary string
on Tue, 24 Jan 2017 22:07:41 +0100
Hi All,
How do I read a file from disk and turn it into a binary string? 
Like LoadFromUrl() does with a webserver-file...
Thanx & regards, Thomas
Boris BorzicRe: File2binary string
on Tue, 24 Jan 2017 23:22:15 +0100
Thomas Pool wrote in news:3sviy9r4lngp$.wztkcmf2h3sh.dlg@40tude.net:

> How do I read a file from disk and turn it into a binary string? 
> Like LoadFromUrl() does with a webserver-file...

Assuming the file is not to big:

cSring := memoread( cFileName )

Best regards,
Boris Borzic

http://xb2.net
http://sqlexpress.net
industrial strength Xbase++ development tools
Thomas PoolRe: File2binary string
on Wed, 25 Jan 2017 15:14:36 +0100
Hi Boris,
Memoread() returns a readable text string, not a a binary string, which
should look like 3JlYXRpb25EYXRlIDw0NDNBMzIzMDMxMzQzMDM5MzIzOTMxMzMzN 
Any other solution?
Thanx & regards, Thomas

Op Tue, 24 Jan 2017 23:22:15 +0100 schreef Boris Borzic:

> Thomas Pool wrote in news:3sviy9r4lngp$.wztkcmf2h3sh.dlg@40tude.net:
> 
>> How do I read a file from disk and turn it into a binary string? 
>> Like LoadFromUrl() does with a webserver-file...
> 
> Assuming the file is not to big:
> 
> cSring := memoread( cFileName )
Boris BorzicRe: File2binary string
on Wed, 25 Jan 2017 18:35:04 +0100
Thomas Pool wrote in news:jgpxairpkecf$.1bkf32ffgur2p.dlg@40tude.net:

> Memoread() returns a readable text string, not a a binary string, which
> should look like 3JlYXRpb25EYXRlIDw0NDNBMzIzMDMxMzQzMDM5MzIzOTMxMzMzN 

MemoRead returns the exact contents of the file. What do you mean by 
binary?

Maybe you are thinking of base64 encoding, which is NOT binary but you can 
convert binary data to base64 encoded format (which makes it easier to 
transport). If that is what you are after, then you can use the following 
Xb2.NET functions:

xbBase64Encode
xbBase64Decode

see docs here: http://xb2.net/xb2net/xb2net.htm

Best regards,
Boris Borzic

http://xb2.net
http://sqlexpress.net
industrial strength Xbase++ development tools
Hubert BrandelRe: File2binary string
on Thu, 02 Feb 2017 11:17:35 +0100
> Memoread() returns a readable text string, not a a binary string, which

in the Alaska docus they say, that memoread() - MAY change bytes, but I 
never had that, everytime the string was same as the file.

BUT memowrit() does change the string because it adds a eof marker.

To be of the secure side, you can use fopen, fread, fwrite() etc.

I wrote this functions:

*--------------------------------- read a file from disk
function File2Str(cFile,nStartPos,nLen,nError)   like memoread()
    local nHandle, cBuffer, nReadBytes,nSeekPos   call @nError !!
    cBuffer    := ""
    nReadBytes := 0
    DEFAULT nStartPos TO 1     normaly 1
    DEFAULT nLen      TO 0     0 = read all bytes
    if nStartPos < 1
       nStartPos := 1
    endif
    nError  := 0               0 = no error
    nHandle := FOpen(cFile)
    if nHandle = -1
       nError := FError()
    else
       if nLen=0
          nLen := FSize( nHandle )
       endif
       if nStartPos > 1
          nSeekPos := FSeek( nHandle, nStartPos-1 )
          if nSeekPos <> nStartPos-1
             nError := -2
          endif
       endif
       cBuffer    := space(nLen)
       nReadBytes := FRead( nHandle, @cBuffer, nLen)
       if nReadBytes < nLen     Error or EOF
          nError := FError()
       endif
    endif
    FClose(nHandle)
return left(cBuffer,nReadBytes)
*--------------------------------- write a file to disk
function Str2Disk(cFile,cTxt)    don use memowrit() !
    local nResult, nHandle, nByte
    nResult := 0
    nHandle := FCreate(cFile)
    if nHandle = -1
       nResult := FError()
    else
       nByte := FWrite( nHandle, cTxt )
       if len(cTxt) # nByte
          nResult := FError()
       endif
    endif
    FClose(nHandle)
return nResult
Pascal BoivinRe: File2binary string
on Wed, 25 Jan 2017 17:27:16 +0100
Thomas Pool wrote:

> Hi All,
> How do I read a file from disk and turn it into a binary string? 
> Like LoadFromUrl() does with a webserver-file...
> Thanx & regards, Thomas

What do you mean by "Binary string like LoadFromUrl" ? 

Memoread is easy to use but could translate some caracters.

FOpen/Fread/Fclose will give you the same data that is written on disk.

Either way you will get a caracter variable (type "C").
Boris BorzicRe: File2binary string
on Wed, 25 Jan 2017 18:25:39 +0100
Pascal Boivin wrote in news:xn0kleiqv8gevl000@news.alaska-software.com:

> Memoread is easy to use but could translate some caracters.
> FOpen/Fread/Fclose will give you the same data that is written on disk.

I'm pretty sure that MemoRead does not translate any characters. It should 
give the exact same result as FOpen/FRead/FClose.


Best regards,
Boris Borzic

http://xb2.net
http://sqlexpress.net
industrial strength Xbase++ development tools
Pascal BoivinRe: File2binary string
on Thu, 26 Jan 2017 14:09:03 +0100
I had something in mind about Chr(141)/Chr(13) but I don't see it in
the manual. Maybe it was in Clipper days...
Boris BorzicRe: File2binary string
on Thu, 26 Jan 2017 15:28:56 +0100
Pascal Boivin wrote in news:xn0klfs4d1dgqf000@news.alaska-software.com:

> I had something in mind about Chr(141)/Chr(13) but I don't see it in
> the manual. Maybe it was in Clipper days...

You may be thinking about wordwrap done by MemoEdit()

Best regards,
Boris Borzic

http://xb2.net
http://sqlexpress.net
industrial strength Xbase++ development tools
Jose Adriano BaltieriRe: File2binary string
on Tue, 14 Feb 2017 13:21:20 -0200
On 24/1/2017 19:07, Thomas Pool wrote:
> Hi All,
> How do I read a file from disk and turn it into a binary string?
> Like LoadFromUrl() does with a webserver-file...
> Thanx & regards, Thomas
>

Hi !

If you have access to XBTOOLS, that is, if you're a PRO subscriber, then 
you can simply use

mybinstr:=FileStr("complete-file-path")

That will bring you the raw file string.

You can also use StrFile("a file string") to do the opposite.

Be sure to check the return of those functions on error situations.

There's also another function called HexStr , which will return the 
hexadecimal representation of the string, that is, for each byte of the 
string you'll have two hex letters. Say that the string contains "ABC" 
you'll have this output : "414243" . This one is useful if you need to 
store that string somewhere else where "weird" chars are not allowed. 
This is the case of some primitive MemoFields (or other file structure). 
In my case for instance I use this to send the file via TCP/IP protocol 
(as a hex string) to put it on a mainframe. But this is just a curiosity.

Anyway, take a look at FileStr, will U ?

See ya !