mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2025-12-25 00:07:49 -05:00
41 lines
907 B
C++
41 lines
907 B
C++
#include "StringUtils.h"
|
|
#include <string>
|
|
|
|
const char* StringUtils::wchar_to_char(const wchar_t* pwchar)
|
|
{
|
|
if (pwchar == nullptr)
|
|
{
|
|
return "";
|
|
}
|
|
// get the number of characters in the string.
|
|
int currentCharIndex = 0;
|
|
char currentChar = pwchar[currentCharIndex];
|
|
|
|
while (currentChar != '\0')
|
|
{
|
|
currentCharIndex++;
|
|
currentChar = pwchar[currentCharIndex];
|
|
}
|
|
|
|
const int charCount = currentCharIndex + 1;
|
|
|
|
// allocate a new block of memory size char (1 byte) instead of wide char (2 bytes)
|
|
char* filePathC = (char*)malloc(sizeof(char) * charCount);
|
|
|
|
for (int i = 0; i < charCount; i++)
|
|
{
|
|
// convert to char (1 byte)
|
|
char character = pwchar[i];
|
|
|
|
*filePathC = character;
|
|
|
|
filePathC += sizeof(char);
|
|
|
|
}
|
|
filePathC += '\0';
|
|
|
|
filePathC -= (sizeof(char) * charCount);
|
|
|
|
return filePathC;
|
|
}
|