MCPcopy Create free account
hub / github.com/avast/retdec / unicodeToAscii

Function unicodeToAscii

src/utils/string.cpp:420–453  ·  view source on GitHub ↗

* @brief Converts unicode @a bytes to ASCII string. * * @param[in] bytes Bytes for conversion. * @param[in] nBytes Number of bytes. * * @return Converted string in ASCII. */

Source from the content-addressed store, hash-verified

418* @return Converted string in ASCII.
419*/
420std::string unicodeToAscii(const std::uint8_t *bytes, std::size_t nBytes)
421{
422 std::stringstream result;
423 if (!bytes || !nBytes)
424 {
425 return {};
426 }
427 if (nBytes & 1)
428 {
429 nBytes--;
430 }
431
432 for (std::size_t i = 0; i < nBytes; i += 2)
433 {
434 if (bytes[i] == 0 && bytes[i + 1] == 0)
435 {
436 break;
437 }
438 if (bytes[i + 1] == 0 && isPrintableChar(bytes[i]))
439 {
440 result << bytes[i];
441 }
442 else
443 {
444 const std::size_t maxC = (1 << (sizeof(std::string::value_type) * CHAR_BIT)) - 1;
445 const auto val1 = intToHexString(bytes[i] & maxC);
446 const auto val2 = intToHexString(bytes[i + 1] & maxC);
447 result << "\\x" << std::setw(2) << std::setfill('0') << val1;
448 result << "\\x" << std::setw(2) << std::setfill('0') << val2;
449 }
450 }
451
452 return result.str();
453}
454
455/**
456* @brief Converts unicode @a bytes to ASCII string.

Callers 6

parseVersionInfoMethod · 0.85
parseVersionInfoChildMethod · 0.85
parseVarFileInfoChildMethod · 0.85
parseVarStringMethod · 0.85

Calls 3

isPrintableCharFunction · 0.85
intToHexStringFunction · 0.85
strMethod · 0.80

Tested by

no test coverage detected