FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index mod_harbour hb_base64Ex – URL-safe
Posts: 129
Joined: Mon Oct 17, 2005 03:03 AM
hb_base64Ex – URL-safe
Posted: Sat Aug 23, 2025 08:30 AM
hb_base64Ex – URL-safe Base64 encoder/decoder for mod_harbour

Hi everyone,

Here’s a practical solution to a common issue in mod_harbour web applications:
When sending Base64-encoded data (like an id) from the backend to the frontend, the ‘+’ character often gets lost when the frontend sends it back—especially via URLs or form fields. This causes decoding errors or mismatches.

To solve this, I created hb_base64Ex, a Harbour module that provides:
  • URL-safe Base64 encoding ([tt]+[/tt] → [tt]-[/tt], [tt]/[/tt] → [tt]_[/tt])
  • Optional removal of padding ([tt]=[/tt]) for JWT-style output
  • Auto-detection of standard vs URL-safe formats during decoding
  • UTF-8 compatibility via [tt]hb_cdpSelect("UTF8")[/tt]
  • Error handling for illegal characters
This makes it safe to encode IDs or tokens in the backend, send them to the frontend, and decode them reliably—even if the frontend strips or alters characters like [tt]+[/tt].

Example usage:
hb_cdpSelect( "UTF8" )  // Ensure UTF-8 encoding

cEncoded := hb_base64EncodeEx( "123+456", .T. )  // URL-safe, no padding
// → "MTIzLTQ1Ng"

cDecoded := hb_base64DecodeEx( cEncoded )
// → "123+456"
Project structure:
hb_base64Ex/
├── base64cEx.c            // Encoder module
├── base64dEx.c            // Decoder module
├── test.prg               // Sample test program
├── test.hbp               // Build configuration
└── README.md              // Documentation
GitHub link: https://github.com/WenSheng-tw/harbour-base64-extend

Hope this helps others working with mod_harbour and frontend integration.
Feedback and suggestions are welcome!

Best regards,
WenSheng
line ID: ssbbstw

WeChat ID: ssbbstw

Continue the discussion