Base64 from file

C

Chunekit Pong

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
);
 
A

anon

Chunekit said:
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();
 
C

Chunekit Pong

Chunekit said:
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
 
C

Chunekit Pong

Chunekit said:
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 );
 
A

anon

Chunekit said:
Chunekit said:
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?
 
T

Thomas J. Gritzan

Chunekit said:
=================
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top