Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Decode Base64 String in VFP6

Decode Base64 String in VFP6

I know I can encode and decode Base64 strings using STRCONV in VFP9. Unfortunately, I'm tied to VFP6 for this project.

I have an Encode function that works great. I've decoded the strings in VFP9 using STRCONV and get the image I'm expecting.

So far, though, I've not found a Decode function that I can get to work. The following code "dies" (q < 0) on a + character:

    LOCAL i,j,j,k,q,ch,s2,buf,tmpc
    tmpc = 0
    j = 0
    buf = 0
    s2 = ''
    k = LEN(s)
    FOR i = 1 TO k
        ch = ASC(SUBSTR(s,i,1))
        q = IIF((ch >= 97 AND ch <=122),25+ch-96,IIF((ch >= 65 AND ch <=90),ch-65,;
            IIF((ch >= 48 AND ch <=57),ch+4,IIF(ch = 47,63,-1))))
        IF q < 0 THEN
            RETURN IIF(ch = 61,s2,'')
        buf = BITOR(BITLSHIFT(buf,6),q)
        j = j + 6
        IF j >= 8 THEN
            j = j - 8
            tmpc = CHR(BITAND(BITRSHIFT(buf,j),255))
            buf = BITAND(buf,BITLSHIFT(1,j) -1)
            s2 = s2 + tmpc
    RETURN s2

Again, this function takes a Base64 string and decodes it to binary. Can anyone help?

RE: Decode Base64 String in VFP6

I am no expert on this, but a quick google got me this:

Which has both encode and decode functions.


Keep Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

RE: Decode Base64 String in VFP6

Actually, the decode looks very like yours!


Keep Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

RE: Decode Base64 String in VFP6

I don't know the base64 specs, but:
 + = CHR(43), so ch = 43

This falls through the nested IIFs, assigning -1 to q, which appears to be a terminator.

The function is expecting the terminator to be the '=' character (ASCII 61). Any other character just forces the function to return an empty string.

Ok, so I just checked the specs, and reviewing the link.

The implementation of the decode function does not mirror the implementation of the encode function.

The encode function has + and / as characters 62 and 63 of the 'alphabet'.
The decode function recognises '/' (ASCII 47) and assigns it 63, but does not recognise '+' (ASCII 43)

Try replacing the embedded IIFs thusly:


q = IIF((ch >= 97 AND ch <=122),25+ch-96,IIF((ch >= 65 AND ch <=90),ch-65,;
    IIF((ch >= 48 AND ch <=57),ch+4,IIF(ch = 47,63,IIF(ch=43,62,-1)))))


RE: Decode Base64 String in VFP6

That did it! Thank you, thank you, thank you!!!

Now I can continue on with my project... You're a lifesaver!

RE: Decode Base64 String in VFP6

Weird that the author would use the basechars string to encode, but not to decode (using AT())

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close