将 unsigned char* 转换为可读字符串 &这个函数在做什么

发布于 2024-12-08 05:02:48 字数 931 浏览 1 评论 0原文

我在谷歌上搜索了很多,以了解如何将我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-12-15 05:02:48

这些不是小数位,而是数字。你的意思是“不要给我一个短于 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 print 0A and not just A.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文