* @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. */
| 418 | * @return Converted string in ASCII. |
| 419 | */ |
| 420 | std::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. |
no test coverage detected