Converting ASCII text to a string
-
Hey Guys, bit of a C# newby, so if you could point me to a relevant article, or give a few tips would me much appreciated. I've got a file, which I load and decompress to ASCII text in a C++ dll (would have no idea how to do it in C#, unless there's a zip library for C#) and can import that into C# like: byte * pBuffer=0; int bytes; if(DodgyImportFunction(sourceFile,&pBuffer,&bytes)) { if(bytes>0) { //at this point have a pointer to a bytes byte*, but I need it as //byte[] and C# won't be nice and let me cast it, so I can use it as //ASCII text. } } Any ideas of a better solution so I can get the decompressed data to be treated as ASCII text in C#? Thanks, Brian.
-
Hey Guys, bit of a C# newby, so if you could point me to a relevant article, or give a few tips would me much appreciated. I've got a file, which I load and decompress to ASCII text in a C++ dll (would have no idea how to do it in C#, unless there's a zip library for C#) and can import that into C# like: byte * pBuffer=0; int bytes; if(DodgyImportFunction(sourceFile,&pBuffer,&bytes)) { if(bytes>0) { //at this point have a pointer to a bytes byte*, but I need it as //byte[] and C# won't be nice and let me cast it, so I can use it as //ASCII text. } } Any ideas of a better solution so I can get the decompressed data to be treated as ASCII text in C#? Thanks, Brian.
Copy it into a string using Marshal.PtrToStringAnsi().
//declare your function like this:
private static extern bool DodgyImportFunction(
string sourcefile, //don't know what datatype this was
ref IntPtr buffer,
ref int bytes
);IntPtr pBuffer=IntPtr.Zero;
int bytes;if(DodgyImportFunction(sourceFile,ref pBuffer,ref bytes))
{
if(bytes>0)
{
string str=Marshal.PtrToStringAnsi(pBuffer,bytes);
//'str' should now be a managed string containing
//the text from pBuffer
}
}"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
Copy it into a string using Marshal.PtrToStringAnsi().
//declare your function like this:
private static extern bool DodgyImportFunction(
string sourcefile, //don't know what datatype this was
ref IntPtr buffer,
ref int bytes
);IntPtr pBuffer=IntPtr.Zero;
int bytes;if(DodgyImportFunction(sourceFile,ref pBuffer,ref bytes))
{
if(bytes>0)
{
string str=Marshal.PtrToStringAnsi(pBuffer,bytes);
//'str' should now be a managed string containing
//the text from pBuffer
}
}"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma GandhiThanks heaps, that worked a treat!:) You ripper, would've taken me ages to work that out. You beauty!