将 unsigned char* 转换为可读字符串 &这个函数在做什么
我在谷歌上搜索了很多,以了解如何将我的 unsigned char* 转换为可打印的十六进制字符串。到目前为止,我稍微了解了它是如何工作的&有符号和有符号之间的区别无符号字符。
你能告诉我我发现的这个函数有什么作用吗?并帮助我开发一个将 unsigned char*(这是一个散列字符串)转换为可打印字符串的函数?
以下函数是否执行此操作:
- 它迭代字符数组字符串的每隔一个字符
- 在每个循环中,它读取 string[x] 位置处的 char,将其转换为无符号数字(精度为 2 位小数),然后将转换后的 char(number?) 复制到变量 uChar。
- 最后它将 unsigned char uChar 存储在十六进制字符串中
void AppManager :: stringToHex( unsigned char* hexString, char* string, int stringLength )
{
// Post:
unsigned char uChar = 0;
for ( int x = 0; x<stringLength; x+=2 )
{
sscanf_s(&string[x], "%02x", &uChar);
hexString[x] = uChar;
}
}
所以我猜这意味着它将字符串中的字符转换为 unsigned(& 2dcp) 以确保它可以正确存储十六进制字符串。为什么要精确到小数点后两位,&从有符号(如果该字符有符号)到无符号的简单转换不会产生完全不同的字符串吗?
如果我有一个 unsigned char*,我该如何将其转换为可以让我在屏幕上打印出来的内容?
I have googled alot to learn how to convert my unsigned char* to a printable hex string. So far I am slightly understanding how it all works & the difference between signed & unsigned chars.
Can you tell me what this function I found does? And help me devlop a function that converts a unsigned char*(which is a hashed string) to a printable string?
Does the following function do this:
- it iterates over every second character of the char array string
- on each loop it reads the char at the position string[x], converts it to an unsigned number(with a precision of 2 decimal places) then copies that converted char(number?) to the variables uChar.
- finally it stores the unsigned char uChar in hexstring
void AppManager :: stringToHex( unsigned char* hexString, char* string, int stringLength )
{
// Post:
unsigned char uChar = 0;
for ( int x = 0; x<stringLength; x+=2 )
{
sscanf_s(&string[x], "%02x", &uChar);
hexString[x] = uChar;
}
}
So I guess that means that it converts the character in string to unsigned(& 2dcp) to ensure that it can be correctly stored the hexstring. Why to 2 decimal places, & wont a simple conversion from signed(if that character is signed) to unsigned result in a completely different string?
If I have a unsigned char* how can I go about converting it to something that will let me print it out on screen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些不是小数位,而是数字。你的意思是“不要给我一个短于 2 的字符串;如果它短于 2 位数字,则用零填充它。”
这样,如果您有一个十六进制序列
0x0A
,它实际上会打印0A
而不仅仅是A
。另外,这里没有签名/无符号转换。十六进制字符串是十六进制字符串 - 它们没有符号。它们是数据的二进制表示形式,并且根据它们的解释方式,可以将其读取为二进制补码有符号整数、无符号整数、字符串或其他任何内容。
Those aren't decimal places, they're digits. You're saying "don't give me a string shorter than 2; if it's shorter than 2 digits, then pad it with a zero."
This is so that if you have a hex sequence
0x0A
it'll actually print0A
and not justA
.Also, there is no signed/unsigned conversion here. Hex strings are hex strings - they don't have a sign. They're a binary representation of the data, and depending on how they're interpreted may be read as two's complement signed integers, unsigned integers, strings, or anything else.