Base64 from file

Discussion in 'C++' started by Chunekit Pong, Nov 20, 2008.

  1. 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
    );
     
    Chunekit Pong, Nov 20, 2008
    #1
    1. Advertising

  2. Chunekit Pong

    anon Guest

    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();
     
    anon, Nov 20, 2008
    #2
    1. Advertising

  3. 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
     
    Chunekit Pong, Nov 20, 2008
    #3
  4. 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 );
     
    Chunekit Pong, Nov 20, 2008
    #4
  5. Chunekit Pong

    anon Guest

    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?
     
    anon, Nov 20, 2008
    #5
  6. 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
     
    Thomas J. Gritzan, Nov 20, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chunekit Pong

    Base64 string save to binary file

    Chunekit Pong, Nov 19, 2008, in forum: C++
    Replies:
    1
    Views:
    927
    Thomas J. Gritzan
    Nov 19, 2008
  2. Cristiano
    Replies:
    1
    Views:
    4,238
  3. Replies:
    2
    Views:
    193
    Egbert Nierop \(MVP for IIS\)
    Aug 18, 2005
  4. Eddy Velasquez

    Can't encode base64 for a jpg file

    Eddy Velasquez, Sep 24, 2010, in forum: Ruby
    Replies:
    4
    Views:
    359
    Eddy Velasquez
    Sep 24, 2010
  5. me at
    Replies:
    9
    Views:
    347
    me at
    Dec 29, 2008
Loading...

Share This Page