Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
18 captures
08 Jan 2008 - 09 Sep 2012
NovDECSep
Previous capture08Next capture
201020112012
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20111208194302/http://www.codeproject.com:80/KB/cpp/base64coding.aspx
Click here to Skip to main content
8,318,091 members and growing!
EmailPassword Lost password?
Home
Search within:




Licence CPOL
First Posted 5 Jan 2008
Views 34,560
Downloads 1,567
Bookmarked 20 times

Base64 Encoding and Decoding

ByMladen Jankovic | 6 Jan 2008
Base64 coding/decoding for native C++ and customized coding for C++/CLI.
 
See Also
Print Article
add
Add to your CodeProject bookmarks
Discuss
Discuss this article
11
  3.12 (15 votes)
5 votes, 33.3%
1

2
1 vote, 6.7%
3
3 votes, 20.0%
4
6 votes, 40.0%
5
3.12/5 - 15 votes
μ 3.12, σa 3.15 [?]
Sponsored Links

Introduction

It is a common thing to pass binary data through text-based protocols. One way to manage this is by using Base64 coding.

Background

Base64 is an encoding which uses characters from 'A'-'Z', 'a'-'z', '0'-'9', and the '+', '/' characters. The '=' character is used for padding data.

Value Encoding  Value Encoding  Value Encoding  Value Encoding   0 A            17 R            34 i            51 z   1 B            18 S            35 j            52 0   2 C            19 T            36 k            53 1   3 D            20 U            37 l            54 2   4 E            21 V            38 m            55 3   5 F            22 W            39 n            56 4   6 G            23 X            40 o            57 5   7 H            24 Y            41 p            58 6   8 I            25 Z            42 q            59 7   9 J            26 a            43 r            60 8  10 K            27 b            44 s            61 9  11 L            28 c            45 t            62 +  12 M            29 d            46 u            63 /  13 N            30 e            47 v  14 O            31 f            48 w         (pad) =  15 P            32 g            49 x  16 Q            33 h            50 y

The last two characters may change on different systems. One of the reasons could be that you cannot use pure Base64 to send binary data embedded into a URL because the '+' and '/' characters must be encoded with '%2B' and '%2F'. This increases the size of the code, and there may be additional problems (the % character is used by SQL). So sometimes, '+' and '/' have to be replaced; one of thestandard replacements are '*' and '-' characters.

Using the Code

Coding is implemented in theBase64 class. It can be used in native and managed applications. The version is chosen by the conditional compilation based on the/clr project options. Macros in theManagedExt.h header are used to make a transfer from one environment to another easier.

AS_PUBLIC CLASS Base64{public:static CONSTwchar_t CHAR_63 ='*';static CONSTwchar_t CHAR_64 ='-';staticint Encode(BYTE_DATA_IN inData,int dataLength, STRING_OUT outCode);staticint Decode(STRING_IN inCode,int codeLength, BYTE_DATA_OUT outData);staticint GetDataLength(int codeLength);staticint GetCodeLength(int dataLength);};
  • Encode - Converts an array of binary data into a string which contains Base64 code. It also returns the size of the produced code (in bytes).
  • Decode - Decodes a string which contains Base64 code and stores it into an output binary data buffer. If the native C++ version is used, the buffer must be pre-allocated. It also returns the number of decoded bytes stored in the output buffer.
  • GetDataLength - Calculates the maximum size of data (in bytes) based on the Base64 code's length.
  • GetCodeLength - Calculates the size of the Base64 code (in bytes) based on the code length. It also includes padding (if it is required). That means that theDecode method can decode less bytes (one or two) than this method returns for the same code length.
  • CHAR_63 andCHAR_64 - These attributes are used to customize the Base64 code to fit our needs (described at the beginning of the article).

The managed version uses theSystem::Convert class (FromBase64String andToBase64String). The .NET framework always produces Base64 code with the '+' and '/' characters. The methods of this class replace them withCHAR_63 andCHAR_64, if they are different (when coding), and reverse the changes before decoding.

TheGetDataLength method is handy in the native version of the library because the output buffer should be pre-allocated, and this is an easy way to find out how much memory you need.

History

  • 7 November - Fixed the decode routine (miscalculation of size of decoded data).
  • 7 January - A few bug fixes. Customization of padding char added.
  • 5 January - Original version posted.

License

This article, along with any associated source code and files, is licensed underThe Code Project Open License (CPOL)

About the Author

Mladen Jankovic

Software Developer

Serbia Serbia

Member


loading...
Sign Up to vote  PoorExcellent
Add a reason or comment to your vote:x
Votes of 3 or less require a comment

Comments and Discussions

 
 RefreshFirstPrevNext
GeneralMy vote of 1membercipiripper1:28 14 Jun '11  
Questionencoding & decoding error?memberDouble Chiang0:34 1 Nov '10  
AnswerRe: encoding & decoding error? [modified]memberMladen Jankovic12:12 4 Nov '10  
GeneralRe: encoding & decoding error?memberDouble Chiang15:22 4 Nov '10  
GeneralRe: encoding & decoding error?memberMladen Jankovic23:24 4 Nov '10  
GeneralMy vote of 1memberJim_zhuo0:56 29 Sep '10  
GeneralRe: My vote of 1memberMladen Jankovic4:28 1 Oct '10  
QuestionHow would you use this to encode a file?memberkencocomputers22:27 4 Mar '10  
NewsI've converted your code into a pure C version. I think some people may like it.memberkingsimba051119:49 30 Dec '09  
GeneralBug in Decoding routinememberMember 125140014:21 6 Nov '08  
Last Visit: 19:00 31 Dec '99     Last Update: 9:43 8 Dec '1112Next »

General General   News News   Suggestion Suggestion   Question Question   Bug Bug   Answer Answer   Joke Joke   Rant Rant   Admin Admin   

Permalink |Advertise |Privacy |Mobile
Web24 |2.5.111206.1 |Last Updated 6 Jan 2008
Article Copyright 2008 by Mladen Jankovic
Everything elseCopyright ©CodeProject, 1999-2011
Terms of Use
Layout:fixed|fluid

The Daily Insider

[8]ページ先頭

©2009-2025 Movatter.jp