473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

sample for base64 encoding in c language

i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
aruna.

Jun 22 '07 #1
13 14675
On Jun 22, 4:13 pm, aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
aruna.
hey im adding more,
i tried with this

#include <stdio.h>

int main(void){
long l;
l=A64L("asdbsde");
printf(%d,l);
return 0;

}
but at the A64l it says this is Compatible with UNIX System V C.
so cud u find the error there.

Jun 22 '07 #2
<ar************@gmail.comwrote:
>i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
The number 6 item on this link may help. Note that the code has been
criticized and also note that the download has a lot more code than the tiny
fragments shown in situ. I did the download and it looked like real source
code. Then I lost interest. I think it is entirely possible that you might
code it yourself and come out with comparable results, timewise. I am quite
sure base64 is documented well enough on the Web that it is just a question
of writing the code. And when you get through you *know* what you have. I
am registered for downloads on this site and it hasn't had any nasty side
effects.

It is also entirely possible there is something better already on the net.

http://www.codeproject.com/info/search.asp
Jun 22 '07 #3
ar************@gmail.com wrote:
i am currently trying to convert data into binary data.
What is your data, and what do you mean by "binary data"?

--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 22 '07 #4
On Fri, 22 Jun 2007 11:13:23 -0000, in comp.lang.c ,
ar************@gmail.com wrote:
>i am currently trying to convert data into binary data.
You need to clarify what you mean. Data is data is data.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 22 '07 #5
On Fri, 22 Jun 2007 11:56:22 -0000, in comp.lang.c ,
ar************@gmail.com wrote:
>#include <stdio.h>
int main(void){
long l;
l=A64L("asdbsde");
no definition of function "A64L" provided.
> printf(%d,l);
missing quote marks round specifier
missing \n at the end of the format string - output not guaranteed.
%d is not the specifier for a long, undefined behaviour.
>but at the A64l it says this is Compatible with UNIX System V C.
no idea what you mean by that.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 22 '07 #6
ar************@gmail.com wrote:
i am currently trying to convert data into binary data.
Assuming that your Subject: heading is an accurate indicator of what you are
looking for ("sample for base64 encoding in C language"), here's some code for
you....

/*
** MIME Base64 coding examples
**
** encode() encodes an arbitrary data block into MIME Base64 format string
** decode() decodes a MIME Base64 format string into raw data
**
** Global table base64[] carries the MIME Base64 conversion characters
*/
/*
** Global data used by both binary-to-base64 and base64-to-binary conversions
*/

static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/";

/*
** ENCODE RAW into BASE64
*/

/* Encode source from raw data into Base64 encoded string */
int encode(unsigned s_len, char *src, unsigned d_len, char *dst)
{
unsigned triad;

for (triad = 0; triad < s_len; triad += 3)
{
unsigned long int sr;
unsigned byte;

for (byte = 0; (byte<3)&&(triad+byte<s_len); ++byte)
{
sr <<= 8;
sr |= (*(src+triad+byte) & 0xff);
}

sr <<= (6-((8*byte)%6))%6; /*shift left to next 6bit alignment*/

if (d_len < 4) return 1; /* error - dest too short */

*(dst+0) = *(dst+1) = *(dst+2) = *(dst+3) = '=';
switch(byte)
{
case 3:
*(dst+3) = base64[sr&0x3f];
sr >>= 6;
case 2:
*(dst+2) = base64[sr&0x3f];
sr >>= 6;
case 1:
*(dst+1) = base64[sr&0x3f];
sr >>= 6;
*(dst+0) = base64[sr&0x3f];
}
dst += 4; d_len -= 4;
}

return 0;
}

/*
** DECODE BASE64 into RAW
*/

/* determine which sextet value a Base64 character represents */
int tlu(int byte)
{
int index;

for (index = 0; index < 64; ++index)
if (base64[index] == byte)
break;
if (index 63) index = -1;
return index;
}

/*
** Decode source from Base64 encoded string into raw data
**
** Returns: 0 - Success
** 1 - Error - Source underflow - need more base64 data
** 2 - Error - Chunk contains half a byte of data
** 3 - Error - Decoded results will overflow output buffer
*/
int decode(unsigned s_len, char *src, unsigned d_len, char *dst)
{
unsigned six, dix;

dix = 0;

for (six = 0; six < s_len; six += 4)
{
unsigned long sr;
unsigned ix;

sr = 0;
for (ix = 0; ix < 4; ++ix)
{
int sextet;

if (six+ix >= s_len)
return 1;
if ((sextet = tlu(*(src+six+ix))) < 0)
break;
sr <<= 6;
sr |= (sextet & 0x3f);
}

switch (ix)
{
case 0: /* end of data, no padding */
return 0;

case 1: /* can't happen */
return 2;

case 2: /* 1 result byte */
sr >>= 4;
if (dix d_len) return 3;
*(dst+dix) = (sr & 0xff);
++dix;
break;

case 3: /* 2 result bytes */
sr >>= 2;
if (dix+1 d_len) return 3;
*(dst+dix+1) = (sr & 0xff);
sr >>= 8;
*(dst+dix) = (sr & 0xff);
dix += 2;
break;

case 4: /* 3 result bytes */
if (dix+2 d_len) return 3;
*(dst+dix+2) = (sr & 0xff);
sr >>= 8;
*(dst+dix+1) = (sr & 0xff);
sr >>= 8;
*(dst+dix) = (sr & 0xff);
dix += 3;
break;
}
}
return 0;
}
/************************************************** ****
** Test bed program to encode and decode base64 data **
************************************************** ****/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
char Bin_array[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
char Str_array[64];
char Raw_array[64];
int binsize, bufsize;

printf("Test 1 - increasing size binary data\n");
for (binsize = 0; binsize < 7; ++binsize)
{
memset(Str_array,0,64);
if (encode(binsize,Bin_array,64,Str_array) == 0)
{
int Strsize;

printf("%d bytes becomes >%s<\n",binsize,Str_array);

Strsize = strlen(Str_array);
if (decode(Strsize,Str_array,binsize,Raw_array) == 0)
{
if (memcmp(Bin_array,Raw_array,binsize) == 0)
printf("values match\n");
else printf("values differ\n");
}
else printf("decode failed\n");
}
else printf("%d bytes failed to encode\n");
}

printf("\nTest 2 - decreasing size buffer\n");
for (bufsize = 10; bufsize 0; --bufsize)
{
printf("** %d byte buffer\n",bufsize);
for (binsize = 0; binsize < 7; ++binsize)
{
memset(Str_array,0,64);
if (encode(binsize,Bin_array,bufsize,Str_array) == 0)
{
int Strsize;

printf("%d bytes becomes >%s<\n",binsize,Str_array);

Strsize = strlen(Str_array);
if (decode(Strsize,Str_array,binsize,Raw_array) == 0)
{
if (memcmp(Bin_array,Raw_array,binsize) == 0)
printf("values match\n");
else printf("values differ\n");
}
else printf("decode failed\n");
}
else printf("%d bytes failed to encode\n",binsize);
}
}
return EXIT_SUCCESS;
}
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Jun 26 '07 #7
On Jun 22, 4:13 am, aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
aruna.
Uhh ... under the assumption that you want a base64 encoder and/or
decoder (based on your subject) you may find a fairly robust one in
the bstraux module of the Better String Library from here:

http://bstring.sf.net/

Unlike the one posted by Lew Pitcher, the one in Bstrlib actually
takes binary input, not just '\0'-free C string input (which kind of
defeats the entire point of a base64 codec). Anyways, it tracks
decoder errors and allows you to stream the conversion through IO
(because you might not have the memory to do it) or whatever.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jun 27 '07 #8
we******@gmail.com wrote:
On Jun 22, 4:13 am, aruna.eies....@gmail.com wrote:
>i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
aruna.

Uhh ... under the assumption that you want a base64 encoder and/or
decoder (based on your subject) you may find a fairly robust one in
the bstraux module of the Better String Library from here:

http://bstring.sf.net/

Unlike the one posted by Lew Pitcher, the one in Bstrlib actually
takes binary input, not just '\0'-free C string input (which kind of
defeats the entire point of a base64 codec).
Ahem!

If you examined the code, you would have seen that the encode() function does
not "just take '\0'-free C string input". It indeed takes a char buffer of
arbitrary binary data, and transforms that into a MIME base64 string.
Similarly, the decode() function takes a MIME base64 string of specified
length (possibly terminated by a non-MIME-base64 character) and transforms
that into a buffer of arbitrary binary data.

I suggest that next time you are tempted to critique some code, you should
read it first.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Jun 27 '07 #9
On Jun 26, 7:00 pm, Lew Pitcher <lpitc...@teksavvy.comwrote:
websn...@gmail.com wrote:
[...] Unlike the one posted by Lew Pitcher, the one in Bstrlib actually
takes binary input, not just '\0'-free C string input (which kind of
defeats the entire point of a base64 codec).

Ahem!

If you examined the code, you would have seen that the encode() function
does not "just take '\0'-free C string input". It indeed takes a char
buffer of arbitrary binary data, and transforms that into a MIME base64
string. [etc]
Whoops! Sorry, you are right. I did skim your code, but just way too
quickly. Indeed, I withdraw my complaint about your code; it does
appear to be better designed and more correct than I thought.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jun 27 '07 #10
On Jun 22, 5:11 pm, Chris Dollin <chris.dol...@hp.comwrote:
aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.

What is your data, and what do you mean by "binary data"?

--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
if i say you the full story, we are currently developing a project
named 'fast infoset' which has recommended ITU(International
Telecomunication Union), this is to speed up the messaging and there
is one open source implementation named fi at sun under the glass fish
projects.so we are going to do the c implementation.so we need to
encode the xml data into binary data, according to the specification
there are several encoding methods according to the character data for
an example for integers there is int encoding algorithms, which is a
built in algorithm. so i need to start my stuff some where else , that
is why i tried to have some sample codes which encode the just
character data in to base64 encoded data. for that i tried to find a
method from the library function as a result of that i got above
mentioned function. but it did nt work.so any way i saw one sample
code is given there at the below which is sent by Mark McIntyre . I
tried with that, amaizing that is working.
so any way thanks you lot.
aruna

Jul 5 '07 #11
On Jun 22, 5:11 pm, Chris Dollin <chris.dol...@hp.comwrote:
aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.

What is your data, and what do you mean by "binary data"?

--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
oh sorry that the sample code sent by Low Pithcher, that should be
revised .
aruna.
faculty of engineering
galle
srilanka

Jul 5 '07 #12
On Jun 27, 4:35 am, Lew Pitcher <lpitc...@teksavvy.comwrote:
aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.

Assuming that your Subject: heading is an accurate indicator of what you are
looking for ("sample for base64 encoding in C language"), here's some code for
you....

/*
** MIME Base64 coding examples
**
** encode() encodes an arbitrary data block into MIME Base64 format string
** decode() decodes a MIME Base64 format string into raw data
**
** Global table base64[] carries the MIME Base64 conversion characters
*/

/*
** Global data used by both binary-to-base64 and base64-to-binary conversions
*/

static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/";

/*
** ENCODE RAW into BASE64
*/

/* Encode source from raw data into Base64 encoded string */
int encode(unsigned s_len, char *src, unsigned d_len, char *dst)
{
unsigned triad;

for (triad = 0; triad < s_len; triad += 3)
{
unsigned long int sr;
unsigned byte;

for (byte = 0; (byte<3)&&(triad+byte<s_len); ++byte)
{
sr <<= 8;
sr |= (*(src+triad+byte) & 0xff);
}

sr <<= (6-((8*byte)%6))%6; /*shift left to next 6bit alignment*/

if (d_len < 4) return 1; /* error - dest too short */

*(dst+0) = *(dst+1) = *(dst+2) = *(dst+3) = '=';
switch(byte)
{
case 3:
*(dst+3) = base64[sr&0x3f];
sr >>= 6;
case 2:
*(dst+2) = base64[sr&0x3f];
sr >>= 6;
case 1:
*(dst+1) = base64[sr&0x3f];
sr >>= 6;
*(dst+0) = base64[sr&0x3f];
}
dst += 4; d_len -= 4;
}

return 0;

}

/*
** DECODE BASE64 into RAW
*/

/* determine which sextet value a Base64 character represents */
int tlu(int byte)
{
int index;

for (index = 0; index < 64; ++index)
if (base64[index] == byte)
break;
if (index 63) index = -1;
return index;

}

/*
** Decode source from Base64 encoded string into raw data
**
** Returns: 0 - Success
** 1 - Error - Source underflow - need more base64 data
** 2 - Error - Chunk contains half a byte of data
** 3 - Error - Decoded results will overflow output buffer
*/
int decode(unsigned s_len, char *src, unsigned d_len, char *dst)
{
unsigned six, dix;

dix = 0;

for (six = 0; six < s_len; six += 4)
{
unsigned long sr;
unsigned ix;

sr = 0;
for (ix = 0; ix < 4; ++ix)
{
int sextet;

if (six+ix >= s_len)
return 1;
if ((sextet = tlu(*(src+six+ix))) < 0)
break;
sr <<= 6;
sr |= (sextet & 0x3f);
}

switch (ix)
{
case 0: /* end of data, no padding */
return 0;

case 1: /* can't happen */
return 2;

case 2: /* 1 result byte */
sr >>= 4;
if (dix d_len) return 3;
*(dst+dix) = (sr & 0xff);
++dix;
break;

case 3: /* 2 result bytes */
sr >>= 2;
if (dix+1 d_len) return 3;
*(dst+dix+1) = (sr & 0xff);
sr >>= 8;
*(dst+dix) = (sr & 0xff);
dix += 2;
break;

case 4: /* 3 result bytes */
if (dix+2 d_len) return 3;
*(dst+dix+2) = (sr & 0xff);
sr >>= 8;
*(dst+dix+1) = (sr & 0xff);
sr >>= 8;
*(dst+dix) = (sr & 0xff);
dix += 3;
break;
}
}
return 0;

}

/************************************************** ****
** Test bed program to encode and decode base64 data **
************************************************** ****/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
char Bin_array[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
char Str_array[64];
char Raw_array[64];
int binsize, bufsize;

printf("Test 1 - increasing size binary data\n");
for (binsize = 0; binsize < 7; ++binsize)
{
memset(Str_array,0,64);
if (encode(binsize,Bin_array,64,Str_array) == 0)
{
int Strsize;

printf("%d bytes becomes >%s<\n",binsize,Str_array);

Strsize = strlen(Str_array);
if (decode(Strsize,Str_array,binsize,Raw_array) == 0)
{
if (memcmp(Bin_array,Raw_array,binsize) == 0)
printf("values match\n");
else printf("values differ\n");
}
else printf("decode failed\n");
}
else printf("%d bytes failed to encode\n");
}

printf("\nTest 2 - decreasing size buffer\n");
for (bufsize = 10; bufsize 0; --bufsize)
{
printf("** %d byte buffer\n",bufsize);
for (binsize = 0; binsize < 7; ++binsize)
{
memset(Str_array,0,64);
if (encode(binsize,Bin_array,bufsize,Str_array) == 0)
{
int Strsize;

printf("%d bytes becomes >%s<\n",binsize,Str_array);

Strsize = strlen(Str_array);
if (decode(Strsize,Str_array,binsize,Raw_array) == 0)
{
if (memcmp(Bin_array,Raw_array,binsize) == 0)
printf("values match\n");
else printf("values differ\n");
}
else printf("decode failed\n");
}
else printf("%d bytes failed to encode\n",binsize);
}
}
return EXIT_SUCCESS;

}

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
hi Lew ,
i m really grateful you and amazing that is really working .
so thanks lot .
aruna
faculty of engineering
galle
sri lanka
intern wso2.inc

Jul 5 '07 #13
On Jul 5, 7:48 am, aruna.eies....@gmail.com wrote:
On Jun 27, 4:35 am, Lew Pitcher <lpitc...@teksavvy.comwrote:
aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.
Assuming that your Subject: heading is an accurate indicator of what you are
looking for ("sample for base64 encoding in C language"), here's some code for
you....
/*
** MIME Base64 coding examples
**
[snip]
hi Lew ,
i m really grateful you and amazing that is really working .
so thanks lot .
You're welcome.

I wrote that code as an example for an XML development project I was
working on. From that C code, I wrote a corresponding COBOL program to
encode and decode in base64 (the C code being a template for the
webish side of the system, and the COBOL for the mainframe side of the
system).

I'm glad it worked for you.
Jul 5 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rodney Pont | last post by:
I've got the example below to set up phpOpenTracker to log exit URL's but I'm having trouble getting it to work. I have played with the quotes and changed the \\2 to $3 and got the url in there but...
3
by: wenmang | last post by:
Hi, I ma thinking whether to use Base64 encoding to encode the binary content in the XML file. I have done some simple calculations, it seems to me that the size for encoded content increases by...
4
by: Curt Fluegel | last post by:
I seem to be having a problem base64 encoding characters above 127. I can encode a sentence like "The big bad dog" without problems, but if I try to encode something like 0xFF I get different...
5
by: LP | last post by:
A web service returns base64 encoded data. The goal is to parse it and store it into binary file with .dat extension. This file is then will be used by a custom program to produce diagrams. As far...
3
by: nly | last post by:
What's the purpose of "Base64 encoding and decoding"? Thanks in advance!
2
by: kevin | last post by:
DISCLAIMER: I know what the words mean (i.e. by definition), but I in know way pretend to understand the specifics of either, therefore I may need a basic primer before I can accomplish this task,...
9
by: Miguel | last post by:
I am looking for some example of how to encode a string pair (user/password) in base64. I am using basic windows authentication... ¿Could anybody help me? Thank you in advance Miguel
1
by: Loren Dummer | last post by:
In Visual Basic 6 I could take a value from the database and create a byte array. I could then take the byte array and place it into an Xml document and specify the datatype of the node as...
1
by: maxxxxel | last post by:
Hi Can anyone help me with some asp code , I changed the code to use CDO.message instead of the old cdont.sys to send mail from a ASP webpage which works fine. Our problem is that when we send...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.