diff --git a/CMakeLists.txt b/CMakeLists.txt
index ac6be3436773316758c21c07781f89c64297f2ea..9eb3b2d2f62d5e838f5905e1cd666620ab5b8ddb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -879,7 +879,7 @@ ENDIF()
 
 IF(KWSYS_USE_Encoding)
   # Set default 8 bit encoding in "EndcodingC.c".
-  SET_PROPERTY(SOURCE EncodingC.c APPEND PROPERTY COMPILE_DEFINITIONS
+  SET_PROPERTY(SOURCE EncodingC.c EncodingCXX.cxx APPEND PROPERTY COMPILE_DEFINITIONS
     KWSYS_ENCODING_DEFAULT_CODEPAGE=${KWSYS_ENCODING_DEFAULT_CODEPAGE})
 ENDIF()
 
diff --git a/EncodingCXX.cxx b/EncodingCXX.cxx
index 5c58bcb3daa18f2a8decc60ab3cd7267499a4035..e904c1af17c3dc64fa92450f639c7732175c2a11 100644
--- a/EncodingCXX.cxx
+++ b/EncodingCXX.cxx
@@ -125,12 +125,68 @@ char const* const* Encoding::CommandLineArguments::argv() const
 
 std::wstring Encoding::ToWide(const std::string& str)
 {
-  return ToWide(str.c_str());
+  std::wstring wstr;
+#if defined(_WIN32)
+  const int wlength = MultiByteToWideChar(
+    KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(), int(str.size()), NULL, 0);
+  if (wlength > 0) {
+    wchar_t* wdata = new wchar_t[wlength];
+    int r = MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(),
+                                int(str.size()), wdata, wlength);
+    if (r > 0) {
+      wstr = std::wstring(wdata, wlength);
+    }
+    delete[] wdata;
+  }
+#else
+  size_t pos = 0;
+  size_t nullPos = 0;
+  do {
+    if (pos < str.size() && str.at(pos) != '\0') {
+      wstr += ToWide(str.c_str() + pos);
+    }
+    nullPos = str.find('\0', pos);
+    if (nullPos != str.npos) {
+      pos = nullPos + 1;
+      wstr += wchar_t('\0');
+    }
+  } while (nullPos != str.npos);
+#endif
+  return wstr;
 }
 
 std::string Encoding::ToNarrow(const std::wstring& str)
 {
-  return ToNarrow(str.c_str());
+  std::string nstr;
+#if defined(_WIN32)
+  int length =
+    WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
+                        int(str.size()), NULL, 0, NULL, NULL);
+  if (length > 0) {
+    char* data = new char[length];
+    int r =
+      WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
+                          int(str.size()), data, length, NULL, NULL);
+    if (r > 0) {
+      nstr = std::string(data, length);
+    }
+    delete[] data;
+  }
+#else
+  size_t pos = 0;
+  size_t nullPos = 0;
+  do {
+    if (pos < str.size() && str.at(pos) != '\0') {
+      nstr += ToNarrow(str.c_str() + pos);
+    }
+    nullPos = str.find(wchar_t('\0'), pos);
+    if (nullPos != str.npos) {
+      pos = nullPos + 1;
+      nstr += '\0';
+    }
+  } while (nullPos != str.npos);
+#endif
+  return nstr;
 }
 
 std::wstring Encoding::ToWide(const char* cstr)
diff --git a/testEncoding.cxx b/testEncoding.cxx
index 996976f92fe8508742e9ad2b5484157d7a6abc19..03f2ec9e33dc8f72cc6f1d342a9711f21383b070 100644
--- a/testEncoding.cxx
+++ b/testEncoding.cxx
@@ -9,6 +9,7 @@
 #include KWSYS_HEADER(Encoding.hxx)
 #include KWSYS_HEADER(Encoding.h)
 
+#include <algorithm>
 #include <iostream>
 #include <locale.h>
 #include <stdlib.h>
@@ -124,6 +125,35 @@ static int testRobustEncoding()
   return ret;
 }
 
+static int testWithNulls()
+{
+  int ret = 0;
+  std::vector<std::string> strings;
+  strings.push_back(std::string("ab") + '\0' + 'c');
+  strings.push_back(std::string("d") + '\0' + '\0' + 'e');
+  strings.push_back(std::string() + '\0' + 'f');
+  strings.push_back(std::string() + '\0' + '\0' + "gh");
+  strings.push_back(std::string("ij") + '\0');
+  strings.push_back(std::string("k") + '\0' + '\0');
+  strings.push_back(std::string("\0\0\0\0", 4) + "lmn" +
+                    std::string("\0\0\0\0", 4));
+  for (std::vector<std::string>::iterator it = strings.begin();
+       it != strings.end(); ++it) {
+    std::wstring wstr = kwsys::Encoding::ToWide(*it);
+    std::string str = kwsys::Encoding::ToNarrow(wstr);
+    std::string s(*it);
+    std::replace(s.begin(), s.end(), '\0', ' ');
+    std::cout << "'" << s << "' (" << it->size() << ")" << std::endl;
+    if (str != *it) {
+      std::replace(str.begin(), str.end(), '\0', ' ');
+      std::cout << "string with null was different: '" << str << "' ("
+                << str.size() << ")" << std::endl;
+      ret++;
+    }
+  }
+  return ret;
+}
+
 static int testCommandLineArguments()
 {
   int status = 0;
@@ -165,6 +195,7 @@ int testEncoding(int, char* [])
   ret |= testHelloWorldEncoding();
   ret |= testRobustEncoding();
   ret |= testCommandLineArguments();
+  ret |= testWithNulls();
 
   return ret;
 }