Is it safe to cast from LPBYTE to LPSTR to LPBYTE ?
-
I have a
BYTE
array that contains a mix of ANSI and binary data. I'm wanting to convert theLPBYTE
toLPSTR
so that I can use theCString
functions to pull substrings of either ANSI or binary data out of theBYTE
array. I know that explicit casts can be hazardous, so I tried this:int _tmain(int argc, _TCHAR* argv[])
{ // (BYTE = unsigned char)
unsigned char uc = 255; // 255 is max value for unsigned char, outside range for charcout << "unsigned char uc = " << (int) uc << "\\n"; char c = (char) uc; cout << "char c = (char) uc = " << (int) c << "\\n"; uc = (unsigned char) c; cout << "(unsigned char) c = " << (int) uc << "\\n";
}
OUTPUT:
unsigned char uc = 255
char c = (char) uc = -1
(unsigned char) c = 255I'm not 100% that this little test tells me it's safe to convert binary data in a
BYTE
array to aCHAR
array and back... Can you gurus help me out, again, please? :confused: -
I have a
BYTE
array that contains a mix of ANSI and binary data. I'm wanting to convert theLPBYTE
toLPSTR
so that I can use theCString
functions to pull substrings of either ANSI or binary data out of theBYTE
array. I know that explicit casts can be hazardous, so I tried this:int _tmain(int argc, _TCHAR* argv[])
{ // (BYTE = unsigned char)
unsigned char uc = 255; // 255 is max value for unsigned char, outside range for charcout << "unsigned char uc = " << (int) uc << "\\n"; char c = (char) uc; cout << "char c = (char) uc = " << (int) c << "\\n"; uc = (unsigned char) c; cout << "(unsigned char) c = " << (int) uc << "\\n";
}
OUTPUT:
unsigned char uc = 255
char c = (char) uc = -1
(unsigned char) c = 255I'm not 100% that this little test tells me it's safe to convert binary data in a
BYTE
array to aCHAR
array and back... Can you gurus help me out, again, please? :confused:Yes, you can cast LPBYTE to LPSTR without really worrying too much. Rather than use CString, though, I'd probably use either STL algorithms (because LPBYTE and LPSTR can both be treated as STL iterators) or Boost algorithms[^] (which are actually a lot easier to use than the documentation can make it appear), because they will act on your BYTE array, whereas a CString manages its own buffer. Here's an example of how to find a substring in a BYTE buffer (I've used unsigned char* because I'm testing on OS X, not Windows, so I don't have a BYTE type!):
#include <algorithm>
#include <iostream>int main()
{
unsigned char buffer[] = { 1, 2, 3, 'H', 'e', 'l', 'l', 'o', 34, 12 };
unsigned char* bufferEnd = buffer + sizeof(buffer);
char lookFor[] = "Hello";
char* lookForEnd = lookFor + strlen(lookFor);unsigned char* where = std::search(buffer, bufferEnd, (unsigned char*)lookFor, (unsigned char*)lookForEnd);
std::cout << std::distance(buffer, where) << std::endl; // Shows the offset of Hello in the buffer == 3
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Yes, you can cast LPBYTE to LPSTR without really worrying too much. Rather than use CString, though, I'd probably use either STL algorithms (because LPBYTE and LPSTR can both be treated as STL iterators) or Boost algorithms[^] (which are actually a lot easier to use than the documentation can make it appear), because they will act on your BYTE array, whereas a CString manages its own buffer. Here's an example of how to find a substring in a BYTE buffer (I've used unsigned char* because I'm testing on OS X, not Windows, so I don't have a BYTE type!):
#include <algorithm>
#include <iostream>int main()
{
unsigned char buffer[] = { 1, 2, 3, 'H', 'e', 'l', 'l', 'o', 34, 12 };
unsigned char* bufferEnd = buffer + sizeof(buffer);
char lookFor[] = "Hello";
char* lookForEnd = lookFor + strlen(lookFor);unsigned char* where = std::search(buffer, bufferEnd, (unsigned char*)lookFor, (unsigned char*)lookForEnd);
std::cout << std::distance(buffer, where) << std::endl; // Shows the offset of Hello in the buffer == 3
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-nt-