Connecting Tech Pros Worldwide Help | Site Map

Base64 from file

  #1  
Old November 20th, 2008, 08:35 AM
Chunekit Pong
Guest
 
Posts: n/a
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<BYTEbytes(
(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
);
  #2  
Old November 20th, 2008, 09:15 AM
anon
Guest
 
Posts: n/a

re: Base64 from file


Chunekit Pong wrote:
Quote:
What's the problem with this code?
>
It doesn't compile
Quote:
the purpose is to convert binary file into a Base64 string
>
What's Base64 string?
Quote:
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)
Quote:
=================
typedef unsigned char BYTE;
std::ifstream file1("c:/test2.png");
>
// read from test2.png into BYTE array
std::vector<BYTEbytes(
(std::istreambuf_iterator<char>(file1))
, (std::istreambuf_iterator<char>())
);
Is this working?
Quote:
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();
  #3  
Old November 20th, 2008, 09:35 AM
Chunekit Pong
Guest
 
Posts: n/a

re: Base64 from file


On Thu, 20 Nov 2008 10:13:34 +0100, anon <anon@no.invalidwrote:
Quote:
>Chunekit Pong wrote:
Quote:
>What's the problem with this code?
>>
>
>It doesn't compile
>
Quote:
>the purpose is to convert binary file into a Base64 string
>>
>
>What's Base64 string?
>
Quote:
>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)
>
Quote:
>=================
> typedef unsigned char BYTE;
> std::ifstream file1("c:/test2.png");
>>
> // read from test2.png into BYTE array
> std::vector<BYTEbytes(
> (std::istreambuf_iterator<char>(file1))
> , (std::istreambuf_iterator<char>())
> );
>
>Is this working?
>
Quote:
> 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
  #4  
Old November 20th, 2008, 10:25 AM
Chunekit Pong
Guest
 
Posts: n/a

re: Base64 from file


On Thu, 20 Nov 2008 10:13:34 +0100, anon <anon@no.invalidwrote:
Quote:
>Chunekit Pong wrote:
Quote:
>What's the problem with this code?
>>
>
>It doesn't compile
>
Quote:
>the purpose is to convert binary file into a Base64 string
>>
>
>What's Base64 string?
>
Quote:
>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)
>
Quote:
>=================
> typedef unsigned char BYTE;
> std::ifstream file1("c:/test2.png");
>>
> // read from test2.png into BYTE array
> std::vector<BYTEbytes(
> (std::istreambuf_iterator<char>(file1))
> , (std::istreambuf_iterator<char>())
> );
>
>Is this working?
>
Quote:
> 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 );
  #5  
Old November 20th, 2008, 11:35 AM
anon
Guest
 
Posts: n/a

re: Base64 from file


Chunekit Pong wrote:
Quote:
On Thu, 20 Nov 2008 10:13:34 +0100, anon <anon@no.invalidwrote:
>
Quote:
>Chunekit Pong wrote:
Quote:
>>What's the problem with this code?
>>>
>It doesn't compile
>>
Quote:
>>the purpose is to convert binary file into a Base64 string
>>>
>What's Base64 string?
>>
Quote:
>>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)
>>
Quote:
>>=================
>> typedef unsigned char BYTE;
>> std::ifstream file1("c:/test2.png");
>>>
>> // read from test2.png into BYTE array
>> std::vector<BYTEbytes(
>> (std::istreambuf_iterator<char>(file1))
>> , (std::istreambuf_iterator<char>())
>> );
>Is this working?
>>
Quote:
>> 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?
  #6  
Old November 20th, 2008, 01:25 PM
Thomas J. Gritzan
Guest
 
Posts: n/a

re: Base64 from file


Chunekit Pong schrieb:
Quote:
Quote:
Quote:
>>=================
>> typedef unsigned char BYTE;
>> std::ifstream file1("c:/test2.png");
>>>
>> // read from test2.png into BYTE array
>> std::vector<BYTEbytes(
>> (std::istreambuf_iterator<char>(file1))
>> , (std::istreambuf_iterator<char>())
>> );
>Is this working?
>>
Quote:
>> 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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to retrieve mails & attachment from mbox Watermark answers 6 March 13th, 2007 01:09 PM
How can I get the specific file handle information from a System.Diagnostics.Process.Start call? forest demon answers 3 November 19th, 2006 01:55 AM
base64 Jay answers 5 April 13th, 2006 08:25 AM
reading strings from file Giff answers 6 January 3rd, 2006 12:05 AM