DDK提供了UNICODE_STRING字符串与整数相互转换的内核函数。
(1). UNICODE_STRING 字符串转换成整数。
这个函数是 RtlUnicodeStringToInteger, 其声明是:
NTSTATUS RtlUnicodeStringToInteger (
IN PCUNICODE_STRING String,
IN ULONG Base,
OUT PULONG Value
);
// String: 需要转换的字符串
// Base: 转换的数的进制(如2、8、10、16)
// Value: 需要转换的数字
// 返回值: 指明是否转换成功
(1). 整数转换成 UNICODE_STRING 字符串。
这个函数RtlIntegerToUnicodeString,其声明是:
NTSTATUS RtlIntegerToUnicodeString (
IN ULONG Value,
IN ULONG Base,
IN OUT PUNICODE_STRING String
);
// 需要转换的数字
// Base: 转换的数的进制(如2、8、10、16)
// String: 需要转换的字符串
// 返回值: 指明是否转换成功
以下是字符串与整数型数字相互转换的实例。
#define BUFFER_SIZE 256
// (1). 字符串转换成数字
// 初始化 UnicodeStr1
UNICODE_STRING UnicodeStr1;
RtlInitUnicodeString(&UnicodeStr1, L"-100");
ULONG lNumber;
NTSTATUS nStatus = RtlUnicodeStringToInteger(&UnicodeStr1, 10, &lNumber);
if (NT_SUCCESS(nStatus))
{
KdPrint(("Counter to integer successfully!\n"));
KdPrint(("Result: %d!\n", lNumber));
}
else
{
KdPrint(("Conver to integer unsuccessfully\n"));
}
// (1). 数字转换成字符串
UNICODE_STRING UnicodeStr2 = {0};
UnicodeStr2.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
UnicodeStr2.MaximumLength = BUFFER_SIZE;
nStatus = RtlIntegerToUnicodeString(200, 10, &UnicodeStr2);
if (NT_SUCCESS(nStatus))
{
KdPrint(("Counter to string successfully!\n"));
KdPrint(("Result: %wZ!\n", &UnicodeStr2));
}
else
{
KdPrint(("Conver to string unsuccessfully\n"));
}
// 销毁 UnicodeStr2
// 注意: UnicodeStr1不用销毁
RtlFreeUnicodeString(&UnicodeStr2);