-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtil.cpp
33 lines (26 loc) · 989 Bytes
/
StringUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cwchar>
#include <cstdlib>
#include "StringUtil.h"
#pragma warning(disable:4996)
char* convertWideToNarrow(const wchar_t* wcharArray) {
// Determine the size needed for the narrow character array
size_t size = wcstombs(NULL, wcharArray, 0);
if (size == static_cast<size_t>(-1)) {
// Handle error, wcstombs failed
std::cerr << "Conversion error" << std::endl;
return nullptr;
}
// Allocate memory for the narrow character array
char* charArray = new char[size + 1];
// Convert wide character array to narrow character array
if (wcstombs(charArray, wcharArray, size + 1) == static_cast<size_t>(-1)) {
// Handle error, conversion failed
std::cerr << "Conversion error" << std::endl;
delete[] charArray; // Free allocated memory before returning
return nullptr;
}
// Null-terminate the narrow character array
charArray[size] = '\0';
return charArray;
}