none
Extracting C++ WCHAR bytes from C# string RRS feed

  • Question

  • C# and C++ appear to store wide characters differently. How do I get the byte values of a C# string that is identical to the way it is stored in C++ ? Thanks.

    C++ string and sequence of bytes stored for it:

    WCHAR Str[] = L"Calculator.exe";
    92, 0, 67, 0, 97, 0, 108, 0, 99, 0, 117, 0, 108, 0, 97, 0, 116, 0, 111, 0, 114, 0, 46, 0, 101, 0, 120, 0
    

    C# string and the bytes extracted from it:

    byte[] StrBytes = Encoding.Unicode.GetBytes ( "Calculator.exe" );
    67, 0, 97, 0, 108, 0, 99, 0, 117, 0, 108, 0, 97, 0, 116, 0, 111, 0, 114, 0, 46, 0, 101, 0, 120, 0, 101, 0
    

    How the byte sequence differ:

    92, 0, 67, 0, 97, 0, 108, 0, 99, 0, 117, 0, 108, 0, 97, 0, 116, 0, 111, 0, 114, 0, 46, 0, 101, 0, 120, 0
           67, 0, 97, 0, 108, 0, 99, 0, 117, 0, 108, 0, 97, 0, 116, 0, 111, 0, 114, 0, 46, 0, 101, 0, 120, 0, 101, 0
    

    Thursday, February 15, 2018 1:07 AM

Answers

  • C++ string and sequence of bytes stored for it:
    WCHAR Str[] = L"Calculator.exe";
    92, 0, 67, 0, 97, 0, 108, 0, 99, 0, 117, 0, 108, 0, 97, 0, 116, 0, 111, 0, 114, 0, 46, 0, 101, 0, 120, 0


    Are these the bytes you're seeing when you write that string to a file,
    or are you seeing them in memory using a debugger?

    If you're getting these bytes from a file, what file format was used?

    - Wayne

    • Marked as answer by Dev10110110 Thursday, February 15, 2018 2:12 AM
    Thursday, February 15, 2018 2:04 AM

All replies

  • C++ string and sequence of bytes stored for it:
    WCHAR Str[] = L"Calculator.exe";
    92, 0, 67, 0, 97, 0, 108, 0, 99, 0, 117, 0, 108, 0, 97, 0, 116, 0, 111, 0, 114, 0, 46, 0, 101, 0, 120, 0


    Are these the bytes you're seeing when you write that string to a file,
    or are you seeing them in memory using a debugger?

    If you're getting these bytes from a file, what file format was used?

    - Wayne

    • Marked as answer by Dev10110110 Thursday, February 15, 2018 2:12 AM
    Thursday, February 15, 2018 2:04 AM
  • Sorry bad programming. Got my C++ pointer shifted by physical exhaustion. But thanks for taking a look anyway.

    It did turn out the byte sequence was identical.

    Thursday, February 15, 2018 2:12 AM