Using Unicode Strings in the C++ Edition


The C++ editions support Unicode text in two ways:

Headers Location String Type Platforms
Standard include char* (UTF-8 by default) All
Unicode include/unicode wchar_t* (LPWSTR/LPCWSTR) Windows

Both sets of headers declare the same classes (e.g., HTTP), and use the same library, so the choice only affects the string types used in code.

UTF-8 Strings with the Standard Headers

The headers in the include folder use char* strings for all properties, method parameters, and event parameters. By default, these strings are treated as UTF-8, so Unicode text can be used by passing UTF-8 encoded strings.

#include <stdio.h>
#include "http.h"

int main()
{
  HTTP http;
  // The source file is saved as UTF-8, and compiled with UTF-8 strings (e.g., using /utf-8 with Visual C++).
  int ret = http.Get("https://www.example.com/café");
  if (ret) {
    printf("Error %d: %s\n", ret, http.GetLastError());
    return 1;
  }
  printf("%s\n", http.GetStatusLine());
  return 0;
}

To use a different encoding for these strings, set the CodePage configuration setting to the identifier of the code page (the default is 65001, which is UTF-8).

http.Config("CodePage=1252"); // Use Windows-1252 (Western European).

Wide Character Strings with the Unicode Headers

On Windows, applications that use wide character strings (e.g., applications built with UNICODE defined) can use the headers in the <installation directory>/include/unicode folder instead. These headers use LPWSTR and LPCWSTR strings for properties, method parameters, and event parameters.

To use them, add <installation directory>/include/unicode to the include path of the project in place of <installation directory>/include. The class names and the code that uses them do not change, other than the string types.

#include <stdio.h>
#include "http.h" // Resolved from <installation directory>/include/unicode.

class MyHTTP : public HTTP {
public:
  virtual int FireHeader(HTTPHeaderEventParams* e) {
    wprintf(L"%ls: %ls\n", e->Field, e->Value);
    return 0;
  }
};

int main()
{
  MyHTTP http;
  int ret = http.Get(L"https://www.example.com/café");
  if (ret) {
    // Note that GetLastError returns a char* string in both sets of headers.
    printf("Error %d: %s\n", ret, http.GetLastError());
    return 1;
  }
  wprintf(L"%ls\n", http.GetStatusLine());
  return 0;
}

Binary data (such as the content returned by GetTransferredData) is returned as bytes in both sets of headers, and is not affected by the string type.

Note that in older versions (2016 and earlier), Unicode support was provided by separate classes with a "W" suffix (e.g., HTTPW and HTTPHeaderEventParamsW). To update code that uses these classes, add the include/unicode folder to the include path, and remove the "W" suffix from the class and type names.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.