Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Base64 from file

Reply
Thread Tools

Base64 from file

 
 
Chunekit Pong
Guest
Posts: n/a
 
      11-20-2008
What's the problem with this code?

the purpose is to convert binary file into a Base64 string

I seem to have problem in the following line
BYTE const* pbBinary = &bytes[0];
int size = sizeof(pbBinary);

=================
typedef unsigned char BYTE;
std::ifstream file1("c:/test2.png");

// read from test2.png into BYTE array
std::vector<BYTE> bytes(
(std::istreambuf_iterator<char>(file1))
, (std::istreambuf_iterator<char>())
);
if(bytes.empty())
; // no bytes have been read

BYTE const* pbBinary = &bytes[0];
int size = sizeof(pbBinary);

unsigned long ulEncLen = 0;
char *pEncOut = NULL;

BOOL fRet = ::CryptBinaryToString( pbBinary, size,
CRYPT_STRING_BASE64, pEncOut, &ulEncLen );
====================
BOOL WINAPI CryptBinaryToString(
__in const BYTE *pbBinary,
__in DWORD cbBinary,
__in DWORD dwFlags,
__out_opt LPTSTR pszString,
__inout DWORD *pcchString
);
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      11-20-2008
Chunekit Pong wrote:
> What's the problem with this code?
>


It doesn't compile

> the purpose is to convert binary file into a Base64 string
>


What's Base64 string?

> I seem to have problem in the following line
> BYTE const* pbBinary = &bytes[0];
> int size = sizeof(pbBinary);
>


Most likely you will get 4 (depends on the platform you use)

> =================
> typedef unsigned char BYTE;
> std::ifstream file1("c:/test2.png");
>
> // read from test2.png into BYTE array
> std::vector<BYTE> bytes(
> (std::istreambuf_iterator<char>(file1))
> , (std::istreambuf_iterator<char>())
> );


Is this working?

> if(bytes.empty())
> ; // no bytes have been read
>
> BYTE const* pbBinary = &bytes[0];
> int size = sizeof(pbBinary);
>


I would do this:
const int size = bytes.size();
 
Reply With Quote
 
 
 
 
Chunekit Pong
Guest
Posts: n/a
 
      11-20-2008
On Thu, 20 Nov 2008 10:13:34 +0100, anon <> wrote:

>Chunekit Pong wrote:
>> What's the problem with this code?
>>

>
>It doesn't compile
>
>> the purpose is to convert binary file into a Base64 string
>>

>
>What's Base64 string?
>
>> I seem to have problem in the following line
>> BYTE const* pbBinary = &bytes[0];
>> int size = sizeof(pbBinary);
>>

>
>Most likely you will get 4 (depends on the platform you use)
>
>> =================
>> typedef unsigned char BYTE;
>> std::ifstream file1("c:/test2.png");
>>
>> // read from test2.png into BYTE array
>> std::vector<BYTE> bytes(
>> (std::istreambuf_iterator<char>(file1))
>> , (std::istreambuf_iterator<char>())
>> );

>
>Is this working?
>
>> if(bytes.empty())
>> ; // no bytes have been read
>>
>> BYTE const* pbBinary = &bytes[0];
>> int size = sizeof(pbBinary);
>>

>
>I would do this:
>const int size = bytes.size();


the full C++ file is like this - should compile
http://www.oniva.com/upload/1356/c3.cpp
 
Reply With Quote
 
Chunekit Pong
Guest
Posts: n/a
 
      11-20-2008
On Thu, 20 Nov 2008 10:13:34 +0100, anon <> wrote:

>Chunekit Pong wrote:
>> What's the problem with this code?
>>

>
>It doesn't compile
>
>> the purpose is to convert binary file into a Base64 string
>>

>
>What's Base64 string?
>
>> I seem to have problem in the following line
>> BYTE const* pbBinary = &bytes[0];
>> int size = sizeof(pbBinary);
>>

>
>Most likely you will get 4 (depends on the platform you use)
>
>> =================
>> typedef unsigned char BYTE;
>> std::ifstream file1("c:/test2.png");
>>
>> // read from test2.png into BYTE array
>> std::vector<BYTE> bytes(
>> (std::istreambuf_iterator<char>(file1))
>> , (std::istreambuf_iterator<char>())
>> );

>
>Is this working?
>
>> if(bytes.empty())
>> ; // no bytes have been read
>>
>> BYTE const* pbBinary = &bytes[0];
>> int size = sizeof(pbBinary);
>>

>
>I would do this:
>const int size = bytes.size();


I used the bytes.size();

but i found that - pEncOut - is not holding any string after calling
CryptBinaryToString function, when I debug it says bad Prt something.

After calling the function CryptBinaryToString - pEncOut shoul have
the Base64 string generated.

===================
char *pEncOut = NULL;

BOOL fRet = ::CryptBinaryToString( pbBinary, size,
CRYPT_STRING_BASE64, pEncOut, &ulEncLen );
 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      11-20-2008
Chunekit Pong wrote:
> On Thu, 20 Nov 2008 10:13:34 +0100, anon <> wrote:
>
>> Chunekit Pong wrote:
>>> What's the problem with this code?
>>>

>> It doesn't compile
>>
>>> the purpose is to convert binary file into a Base64 string
>>>

>> What's Base64 string?
>>
>>> I seem to have problem in the following line
>>> BYTE const* pbBinary = &bytes[0];
>>> int size = sizeof(pbBinary);
>>>

>> Most likely you will get 4 (depends on the platform you use)
>>
>>> =================
>>> typedef unsigned char BYTE;
>>> std::ifstream file1("c:/test2.png");
>>>
>>> // read from test2.png into BYTE array
>>> std::vector<BYTE> bytes(
>>> (std::istreambuf_iterator<char>(file1))
>>> , (std::istreambuf_iterator<char>())
>>> );

>> Is this working?
>>
>>> if(bytes.empty())
>>> ; // no bytes have been read
>>>
>>> BYTE const* pbBinary = &bytes[0];
>>> int size = sizeof(pbBinary);
>>>

>> I would do this:
>> const int size = bytes.size();

>
> the full C++ file is like this - should compile
> http://www.oniva.com/upload/1356/c3.cpp


To open a file for binary read, you need to create ifstream object like
this:
std::ifstream file1( "c:/test2.png",
std::ios_base::binary |
std::ios_base::in );

Are you sure CryptBinaryToString works fine?
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      11-20-2008
Chunekit Pong schrieb:
>>> =================
>>> typedef unsigned char BYTE;
>>> std::ifstream file1("c:/test2.png");
>>>
>>> // read from test2.png into BYTE array
>>> std::vector<BYTE> bytes(
>>> (std::istreambuf_iterator<char>(file1))
>>> , (std::istreambuf_iterator<char>())
>>> );

>> Is this working?
>>
>>> if(bytes.empty())
>>> ; // no bytes have been read
>>>
>>> BYTE const* pbBinary = &bytes[0];
>>> int size = sizeof(pbBinary);
>>>

>> I would do this:
>> const int size = bytes.size();

>
> I used the bytes.size();
>
> but i found that - pEncOut - is not holding any string after calling
> CryptBinaryToString function, when I debug it says bad Prt something.
>
> After calling the function CryptBinaryToString - pEncOut shoul have
> the Base64 string generated.
>
> ===================
> char *pEncOut = NULL;
>
> BOOL fRet = ::CryptBinaryToString( pbBinary, size,
> CRYPT_STRING_BASE64, pEncOut, &ulEncLen );


Of course it doesn't work. pEncOut is set to NULL. It won't magically
change to something else.

You would have to set pEncOut to some buffer and maybe ulEncLen to the
size of this buffer. Check out the documentation of this
CryptBinaryToString function.

--
Thomas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't encode base64 for a jpg file Eddy Velasquez Ruby 4 09-24-2010 09:30 PM
how to convert a binary file to base64? Cristiano C++ 1 03-28-2009 08:58 PM
Removing base64 from mbox formatted file. me at Perl Misc 9 12-29-2008 12:38 AM
Base64 string save to binary file Chunekit Pong C++ 1 11-19-2008 09:57 PM
Decoding Base64 and Saving to a file - HELP! plank@plank.com ASP General 2 08-18-2005 09:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57