Error using hb_base64encode/decode

241 views
Skip to first unread message

Jayadev U

unread,
Mar 5, 2021, 5:05:57 AM3/5/21
to harbou...@googlegroups.com

Hi,

 

When executing the following code, cEncode contains the correct value but cDecode get incorrect value.

 

Tested with today’s harbour-nightly download.  Please let me know the cause of the error.

 

/* code start

 

/* compile with hbwin.hbc */

 

Procedure Main

                local cEncode :="", cDecode :=""

                local cFile := hb_cwd()+"HbBase.pdf"

                local cOutFileName := hb_cwd()+"HbDecode.pdf",hOutPut := 0

                setmode(25,80)

                cEncode := hb_base64encode(cFile)

                cDecode :=hb_base64decode(cEncode)

    hOutput := FCreate( cOutFileName )

    FWrite( hOutput, cDecode)

    FClose( hOutput )

                wapi_shellexecute(,"Open",cOutFileName)

return NIL          

 

/* code end */

 

Warm regards,

 

Jayadev

 

HbBase.pdf

Budyanto Dj.

unread,
Mar 5, 2021, 6:14:57 AM3/5/21
to harbou...@googlegroups.com
Hello,

My guess:

This line:
cEncode := hb_base64encode(cFile)

will encode the string cFile instead of the content of the file.

regards,
budyanto

Jayadev U

unread,
Mar 5, 2021, 6:39:49 AM3/5/21
to harbou...@googlegroups.com

Hi Budyanto,

 

Thanks for your immediate reply.

 

When executing the code, if you inspect, cEncode contains a converted string as under:

 

“ZTpccHJvamVjdHNcV2hhdHNhcHBcSGJCYXNlLnBkZg==”

 

But cDecode gets the value “e:\projects\Whatsapp\HbBase.pdf” which is wrong.

 

Warm regards,

 

Jayadev

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/b266157a-c9f6-9067-e15a-b90f1ad72f0f%40csacomputer.com.

Klas Engwall

unread,
Mar 5, 2021, 8:46:08 AM3/5/21
to harbou...@googlegroups.com
Hi Jayadev,

> When executing the code, if you inspect, cEncode contains a converted
> string as under:
>
> “ZTpccHJvamVjdHNcV2hhdHNhcHBcSGJCYXNlLnBkZg==”
>
> But cDecode gets the value “e:\projects\Whatsapp\HbBase.pdf” which is wrong.

It is wrong only in the sense that it is not what you wanted to see but
it is correct because that is exaxtly what the Base64 encoded string
contains. As Budyanto said, you are encoding the filename and not the
file content.

Regards,
Klas

Budyanto Dj.

unread,
Mar 5, 2021, 8:52:11 AM3/5/21
to harbou...@googlegroups.com
Hi Jayadev,

(I am sorry if I am being wrong, I don't do any test about it, just guessing from reading.)

cDecode being "e:\projects\Whatsapp\HbBase.pdf” is correct, provided hb_cwd() returns "e:\projects\Whatsapp\".

From your posted code, I can imagine that you want to do the following:
1) cEncode := encoded content of file hb_cwd()+"HbBase.pdf"
2) cDecode := decoded back cEncode
3) Write cDecode into file hb_cwd()+"HbDecode.pdf"

Problem occurs in step (1), because instead of encoding the content of the file, your code is encoding the file name.

You may want to change from this:
cEncode := hb_base64encode(cFile)
into this:
cEncode := hb_base64encode( hb_memoread(cFile) )

then the (1),(2),(3) step above will do as it's supposed to.

regards,
budyanto

Jayadev U

unread,
Mar 5, 2021, 8:57:38 AM3/5/21
to harbou...@googlegroups.com
Hi Klas and Budyanto,

Thanks so much, now I see the light, sorry for all the noise.

Warm regards,

Jayadev


-----Original Message-----
From: harbou...@googlegroups.com [mailto:harbou...@googlegroups.com] On Behalf Of Klas Engwall
Sent: 05 March 2021 07:16 PM
To: harbou...@googlegroups.com
Subject: Re: [harbour-users] Error using hb_base64encode/decode

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/d5a46431-519d-30c8-b116-0d94b21b9548%40engwall.com.

Reply all
Reply to author
Forward
0 new messages