Click here to Skip to main content
12,761,078 members (29,617 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ C VisualC++
Hi All

The pdf file was convert to BYTE array data and stored in database.
The data was retrived in CString object (HexaDecimal data) and converted to binary.
I tried to save this data in pdf file.
The file was not opening.It was giving error "Unable to Open or Doc correpted error";
The same procedure was happening correctly for RTF document
Is there any procedure to convert pdf Hexa Decimal to binary data.
Please suggest regarding this.

My Conversion code is like this.
void HexatoByte(CString DocData, CString Number)
	int size=0;
	char* array;
	std::string str= DocData;
	int length = str.length();
	CStdioFile file;
	CString FileName;
	// make sure the input string has an even digit numbers
	if(length%2 == 1)
		str = "0" + str;
	// allocate memory for the output array
	array = new  char[length/2];
	size = length/2;
	std::stringstream sstr(str);
	for(int i=0; i < size; i++)
		char ch1, ch2;
		sstr >> ch1 >> ch2;
		int dig1, dig2;
			dig1 = ch1 - '0';
		else if(ch1>='A' && ch1<='F')
			dig1 = ch1 - 'A' + 10;
		else if(ch1>='a' && ch1<='f') 
			dig1 = ch1 - 'a' + 10;
			dig2 = ch2 - '0';
		else if(ch2>='A' && ch2<='F')
			dig2 = ch2 - 'A' + 10;
		else if(ch2>='a' && ch2<='f') 
			dig2 = ch2 - 'a' + 10;

		array[i] = (dig1*16) + dig2;	
	delete [] array;


please check this code,if there are any changes required.
From the database data retriving in only char * format.
From vc++ we r not retriving directly ByTe array.
Posted 11-Jul-12 4:31am
Updated 11-Jul-12 21:46pm
Jochen Arndt167.2K
Jochen Arndt 12-Jul-12 3:08am
I don't believe that your data are provided as string containing hex codes like 'AB03F5'. When storing to the database, the PDF content is written as binary data without any modification. When reading from the database, data are still binary. Just save them as they are.

You may be confused by the 'char *' type provided by your database read function (this code or function declaration would be interesting). The 'char' type is also often used for binary data (C/C++ representation would be like '\xAB\x03\xF5'). So if you got a pointer to such data and it length, just write that buffer to a file. Don't create a CString or string object from binary data. Copying will stop at the first NULL character.

Richard already gave you the answer. And the comments should have it make more understandable.
Richard MacCutchan 12-Jul-12 5:43am
Following on from Jochen's comment I have to repeat what I said yesterday, that you do not seem to understand how information is stored in a computer. If you want to store some data in a file and later retrieve it, then store it just as it is, do not try to change it into something else; all you are doing is corrupting the original so it is no longer readable.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You seem to be confused about what binary data actually is. If you read a PDF as a byte array and store it in the database, then you must retrieve it in exactly the same way (and write it as such to any new file). Converting it into a string just destroys the structure so it becomes unreadable by any PDF processor.
pwasser 11-Jul-12 11:00am
Absolutely correct but methinks cryptic for OP.
RTF is actually a special case. If RTF is opened and read as text file things work OK.
PDF is actually more like any file of unknown format - if opened and read as binary to byte array and then written as binary all should work fine. This same method should also work for RTF and is thus the generic approach.
Albert Holguin 11-Jul-12 11:11am
Yep... +5
Richard MacCutchan 11-Jul-12 11:15am
That's what I just said.
Richard MacCutchan 11-Jul-12 11:17am
Thanks, it just baffles me how so many people can be writing commercial applications, and yet seem to have no idea of the basic concepts of computers and how data is stored and manipulated.
pwasser 11-Jul-12 11:18am
Yes I know.
Albert Holguin 11-Jul-12 11:18am
Yeah, kind of scary isn't it?
Richard MacCutchan 11-Jul-12 11:24am
Sorry if I seem a bit dim but what was your point and why were you telling me what I already know?
pwasser 11-Jul-12 11:32am
I think the OP's problem is that he is opening an RTF as a text file for reading and or writing. This works fine for RTF but won't work for PDF as you say.
If however he opens file as binary for reading and writing this will work fine for RTF and PDF. So this was not exactly what you said but somewhat paraphrased it while giving you credit.
Richard MacCutchan 11-Jul-12 12:00pm
Ah, I guess I should have made the point that any file can potentially be corrupted by doing what OP described. Thanks.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You are a programmer so creating your own code is your job.
This will work for any file. Try to understand it and adapt to your own needs.
Must work with binary file for reading and writing. CStdioFile is text by default.

// reading a complete binary file
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

int main () {

	ifstream::pos_type fSize = 0;
	char * memblock = NULL;

	 // Input file open as binary.
	ifstream fileIn ("c:\\Temp\\example.pdf", ios::in|ios::binary|ios::ate);

	if (fileIn.is_open())
		fSize = fileIn.tellg();
		memblock = new char [fSize];		// Allocate memory for file contents.
		fileIn.seekg (0, ios::beg); (memblock, fSize);

		cout << "The input file is in memory.\n";

		 // Output file open as binary.
		ofstream fileOut ("c:\\Temp\\example2.pdf", ios::out|ios::binary|ios::ate);

		if (fileOut.is_open())
			fileOut.write(memblock, fSize);
			cout << "The new file has been created.\n";
		else cout << "Unable to open output file.\n";
	else cout << "Unable to open input file.\n";


		delete[] memblock;

	return 0;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 12 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100