Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is it safe to cast from LPBYTE to LPSTR to LPBYTE ?

Is it safe to cast from LPBYTE to LPSTR to LPBYTE ?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike the Red
    wrote on last edited by
    #1

    I have a BYTE array that contains a mix of ANSI and binary data. I'm wanting to convert the LPBYTE to LPSTR so that I can use the CString functions to pull substrings of either ANSI or binary data out of the BYTE 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 char

    cout << "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 = 255

    I'm not 100% that this little test tells me it's safe to convert binary data in a BYTE array to a CHAR array and back... Can you gurus help me out, again, please? :confused:

    S 1 Reply Last reply
    0
    • M Mike the Red

      I have a BYTE array that contains a mix of ANSI and binary data. I'm wanting to convert the LPBYTE to LPSTR so that I can use the CString functions to pull substrings of either ANSI or binary data out of the BYTE 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 char

      cout << "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 = 255

      I'm not 100% that this little test tells me it's safe to convert binary data in a BYTE array to a CHAR array and back... Can you gurus help me out, again, please? :confused:

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        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

        M Offline
        M Offline
        Mike the Red
        wrote on last edited by
        #3

        -nt-

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups