Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C++
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;
		length++;
	}
	
	// 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;
		if(isdigit(ch1))
			dig1 = ch1 - '0';
		else if(ch1>='A' && ch1<='F')
			dig1 = ch1 - 'A' + 10;
		else if(ch1>='a' && ch1<='f') 
			dig1 = ch1 - 'a' + 10;
		if(isdigit(ch2)) 
			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;	
	}
	FileName.Format("%s.pdf",Number);
	
	file.Open(FileName,CFile::modeCreate|CFile::modeReadWrite);
	file.WriteString(array);
	file.Close();
	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
Updated 11-Jul-12 20:46pm
v3
Comments
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.

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.
 
Share this answer
 
Comments
[no name] 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.
Richard MacCutchan 11-Jul-12 11:15am    
That's what I just said.
[no name] 11-Jul-12 11:18am    
Yes I know.
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?
[no name] 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.
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.


C++
// 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);
		fileIn.read (memblock, fSize);
		fileIn.close();

		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);
			fileOut.close();
			cout << "The new file has been created.\n";
		}
		else cout << "Unable to open output file.\n";
	}
	else cout << "Unable to open input file.\n";

	_getch();

	if(memblock)
		delete[] memblock;

	return 0;
} 
 
Share this answer
 
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900