有时候我们需要将 ASCII(CHAR8)和Unicode(CHAR16)的字符串相互转化。之前可以使用 UnicodeValueToString() 和 AsciiValueToString() 函数,但是最新的 EDK2 中已经移除了这两个函数。经过研究可以使用 PrintLib 中的
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/ShellCEntryLib.h>
#include <Library/PrintLib.h>
/***
Print a welcoming message.
Establishes the main structure of the application.
@retval 0 The application exited normally.
@retval Other An error occurred.
***/
INTN
EFIAPI
ShellAppMain (
IN UINTN Argc,
IN CHAR16 **Argv
)
{
CHAR8 *AsciiStr="www.lab-z.com Ascii->Unicode";
CHAR16 UnicodeBuff[200];
CHAR16 *UnicodeStr=L"www.lab-z.com Unicode->Ascii";
CHAR8 AsciiBuff[200];
Print(L"%s\n",UnicodeStr);
Print(L"%a\n",AsciiStr);
// 将 Ascii 转为 Unicode
UnicodeSPrintAsciiFormat(
&UnicodeBuff[0],
sizeof(UnicodeBuff),
AsciiStr);
// 将 unicode 转为 Ascii
AsciiSPrintUnicodeFormat(
&AsciiBuff[0],
sizeof(AsciiBuff),
UnicodeStr);
Print(L"%s\n",UnicodeBuff);
Print(L"%a\n",AsciiBuff);
return(0);
}
运行结果:
函数原型定义在PrintLib.c:
/**
Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
Unicode format string and variable argument list.
This function is similar as snprintf_s defined in C11.
Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
and BufferSize.
The ASCII string is produced by parsing the format string specified by FormatString.
Arguments are pulled from the variable argument list based on the contents of the
format string.
The number of ASCII characters in the produced output buffer is returned not including
the Null-terminator.
If FormatString is not aligned on a 16-bit boundary, then ASSERT().
If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
unmodified and 0 is returned.
If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
unmodified and 0 is returned.
If PcdMaximumAsciiStringLength is not zero, and BufferSize >
(PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
is unmodified and 0 is returned.
If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
ASSERT(). Also, the output buffer is unmodified and 0 is returned.
If BufferSize is 0, then no output buffer is produced and 0 is returned.
@param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
ASCII string.
@param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
@param FormatString A Null-terminated Unicode format string.
@param ... Variable argument list whose contents are accessed based on the
format string specified by FormatString.
@return The number of ASCII characters in the produced output buffer not including the
Null-terminator.
**/
UINTN
EFIAPI
AsciiSPrintUnicodeFormat (
OUT CHAR8 *StartOfBuffer,
IN UINTN BufferSize,
IN CONST CHAR16 *FormatString,
...
)
/**
Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
ASCII format string and variable argument list.
This function is similar as snprintf_s defined in C11.
Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
and BufferSize.
The Unicode string is produced by parsing the format string specified by FormatString.
Arguments are pulled from the variable argument list based on the contents of the
format string.
The number of Unicode characters in the produced output buffer is returned not including
the Null-terminator.
If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
unmodified and 0 is returned.
If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
unmodified and 0 is returned.
If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
(PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
buffer is unmodified and 0 is returned.
If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
ASSERT(). Also, the output buffer is unmodified and 0 is returned.
If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
@param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
Unicode string.
@param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
@param FormatString A Null-terminated ASCII format string.
@param ... Variable argument list whose contents are accessed based on the
format string specified by FormatString.
@return The number of Unicode characters in the produced output buffer not including the
Null-terminator.
**/
UINTN
EFIAPI
UnicodeSPrintAsciiFormat (
OUT CHAR16 *StartOfBuffer,
IN UINTN BufferSize,
IN CONST CHAR8 *FormatString,
...
)