-
January 22nd, 2007, 08:43 AM
#1
write CString to binary file
Hi,
How to write some data contained in a CString to a binary file?
The CString data can only contain "hex values", i.e. str = "010145b4bd".
When I write using ofstream or CFile and open the file with a hex editor it shows my string hex values in the ascii part :
00000000 | 30 31 30 31 34 | 010145b4bd
any idea?
thanks in advance
Please rate if it helps!
* The second mouse gets the cheese.
* Birthdays are good for you. The more you have, the longer you live.
* Always keep your words soft and sweet, just in case you have to eat them.
* Always read stuff that will make you look good if you die in the middle of it.
-
January 22nd, 2007, 09:36 AM
#2
Re: write CString to binary file
ok, i found a solution.
1) convert the CString to unsigned char
2) write the unsigned char buffer to the file
Code:
CFile f( headerPath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
CString header = "all hex bytes here";
int sz = header.GetLength();
unsigned char Msg[5000] = "";
unsigned char* pMsg = Msg;
CStringToHex(header, pMsg); //(this is my own conversion function)
f.Write(pMsg,sz);
f.Close();
Please rate if it helps!
* The second mouse gets the cheese.
* Birthdays are good for you. The more you have, the longer you live.
* Always keep your words soft and sweet, just in case you have to eat them.
* Always read stuff that will make you look good if you die in the middle of it.
-
January 22nd, 2007, 09:39 AM
#3
Re: write CString to binary file
Sounds like you use "ofstream or CFile" not correct, but without seeing your code...
Code:
// non-unicode build
CFile file;
CString str("010145b4bd");
if(fle.Open(....))
{
file.Write((LPCSTR)str, str.GetLength());
file.Close();
}
-
January 22nd, 2007, 09:40 AM
#4
Re: write CString to binary file
step 1 can be fixed with MyString.GetBuffer (0). This gives you the internal pointer to the charbuffer of the CString.
-
January 22nd, 2007, 09:43 AM
#5
Re: write CString to binary file
Thanks!
victor, yeah, i didn't use LPCSTR.
Please rate if it helps!
* The second mouse gets the cheese.
* Birthdays are good for you. The more you have, the longer you live.
* Always keep your words soft and sweet, just in case you have to eat them.
* Always read stuff that will make you look good if you die in the middle of it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|