如何解释代码部分中的以下代码 [英] How to interpret the below code in code section

查看:55
本文介绍了如何解释代码部分中的以下代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的代码中有一个方法,它取一个void *作为参数。

传递六进制十进制值,它使用以下代码。

我无法理解为什么会这样写。

我试图打印值(FinalValue)它正在打印一些符号,比如



0x006a4ba0Ì? Ì?



您能否解释一下以下代码的任何建议/想法?

下面的代码是否适用于大的六进制十进制字符串,如0xFFFFFFFFFFFFFFFF

,没有任何代码更改?

我无法理解为什么它占用原始字符串长度的一半。



基本上我无法理解它的输出/意向。

感谢您的帮助。



Hi All,

I have a method in my code which takes a void* as parameter.
to pass a hexa decimal value it is using the following code.
I am not able to understand why it was written like that.
I tried to print the value (FinalValue) it is printing some symbols like

0x006a4ba0 "Ì" "Ì"

Could you please explain me any suggestion/ideas on this below code ?
Will the below code work for big hexa decimal string like "0xFFFFFFFFFFFFFFFF"
without any code change ??
I am not able to understand why it is taking half of the length of original string.

Basically i could not understand it's output/intention.
Appreciate your help .

void main() 
{ 
	//std::string seedStr = "0xFFFFFFFFFFFFFFFF"; 
	std::string strValue = "0x12"; 
	int strValuelen = 0; 
	unsigned char* FinalValue= new unsigned char[(strValue.length()/2)+1]; 
	memset(FinalValue,0, (strValue.length()/2)+1); 

	//convert the strValue to hex 
	char* strValueHex = (char*)strValue.c_str(); 
	int temp; 

    for(unsigned int i = 0; i < strValue.length()/2; ++i )
    { 
       sscanf( strValueHex + 2 * i, "%2x", &temp );
       FinalValue[i] = temp; 
    } 

   std::cout << FinalValue);
   getch(); 
}

推荐答案

你似乎在混淆和/或不理解变量值的概念,它是怎么回事存储,以及如何以十六进制,十进制或其他形式的表示在屏幕上表示。我强烈建议你阅读数字基础,例如。 G。在 http://en.wikipedia.org/wiki/Radix [ ^ ]。



现在你的代码:

1.输入字符串定义为0x12。这是数字18的十六进制表示。前导0x不是数字的实际部分,它仅表示字符串是十六进制数字。因此,在尝试解释该值时应该跳过它!这就是Carlo的意思,他说你应该只为你的算法提供12,而不是0x12。



2.你已定义 FinalValue 是tpyeunsigned char数组。如果您的目的是紧凑地存储任何类型或任何长度,这是很好的。但是,如果您的目的是解释十六进制数并以可读(即十进制)形式打印它,那就没有意义了。但是让我们暂时忽略后者:



十六进制数通常用于表示任意长度的内存块的内容。原因是内存以字节为单位组织,每个字节可以保存0到255之间的值,而的十六进制数字可以完美地表示该范围。因此,当您使用工具查看某个内存块的内容时,您可能会看到它以成对的十六进制数排列,其中每对代表一个字节的内存值。



你的算法看起来正好相反,我。即读取十六进制数字对,并将它们转换回它们代表的字节值。然后该值存储在 FinalValue 中。



3.当您尝试打印时,您的代码出现故障重新解释的字节序列的内容: cout 知道如何打印出任何基本类型,例如单个 int float ,或( unsigned char 。然而,你传递了一个unsigend char数组,这被cout解释为一个0终止的C风格的字符串! I. e。它将继续读取每个字节,将其重新解释为可打印字符,打印该字符,然后重复,直到遇到值为0的字节。现在,打印一个字节的任意值作为可打印字符是你绝对不想要的东西,而且,它并不总是可能的:某些字节值与任何可打印字符都不对应!阅读 http://en.wikipedia.org/wiki/Character_encoding [ ^ ]了解更多信息。



4。还有一个问题是 FinalValue 足够长,可以保存重新解释的字节序列 - 在这种情况下为1个字节。它没有足够的空间来保存 cout 期望的终止0字符,因此 cout 继续阅读超出 Finalvalue 结尾的内存,直到最终在某个任意位置遇到0字节。当然,结果输出同样是任意的,如果你反复运行程序,甚至可能会有所不同。



经验教训:

a)如果你将一个char数组传递给cout,它将被解释为一个C样式的0终止字符串,并且应该以0字节终止。

b)一个C风格的字符串必须分配在存储的字符串长度之上的一个附加字节,用于保存终止的0字节。

c)您必须确保存储在C样式字符串中的实际字符串的结尾后跟一个0字节。

d)如果你的char数组的目的是来存储实际的字符,你不需要终止0,但你将它传递给cout将输出垃圾。



5.我无法回答的一件事是如何正确打印 FinalValue :它取决于你想看到什么。如果您的输入只是一个任意长度的十六进制数字的任意序列,那么在屏幕上打印出来的最佳方法是十六进制数字序列 - i。即输入字符串!



但是如果输入意味着代表一个数字,那么你的算法做错了!在这种情况下,FinalValue应为 int long 类型,而不是 unsigned char数组
You seem to be mixing up and/or not understand the concepts of the value of variable, how it is stored, and how it is represented on screen in a hexadecimal, decimal, or other form of representation. I strongly suggest you should read up on number bases, e. g. at http://en.wikipedia.org/wiki/Radix[^].

Now on to your code:
1. the input string is defined as "0x12". This is the hexadecimal representation of the number 18. The leading "0x" is not an actual part of the number, it is only an indication that the string is a hexadecimal number. As such, it should be skipped when trying to interpret the value! That is what Carlo meant when he said you should feed only "12" to your algorithm, rather than "0x12".

2. You've defined FinalValue to be of tpye "array of unsigned char". This is fine if your purpose is to compactly store a number of any kind or any length. But it is pointless if your purpose is to interpret a hexadecimal number and print it in readable (i. e. decimal) form. But let's ignore the latter for the moment:

Hexadecimal numbers are commonly used to represent the content of blocks of memory of arbitrary length. The reason is that memory is organized in bytes, and each byte can hold a value between 0 and 255, and a pair of hexadecimal digits can perfectly represent that range. So, when you use a tool to look at the contents of some memory block, you'll likely see it arranged in pairs of hexadecimal numbers, where each pair represents the value of one byte of memory.

Your algorithm appears to be doing the opposite, i. e. reading pairs of hexadecimal digits, and convert them back into the byte value they represent. And that value is then stored in FinalValue.

3. Your code breaks down when you try to print out the contents of your reinterpreted byte sequence: cout knows how to print out any basic type, such as a single int, float, or (unsigned) char. You pass an array of unsigend char however, and that is interpreted by cout as a 0-terminated C-style string! I. e. it will keep on reading each byte, reinterpret it as a printable character, print that character, and repeat until it encounters a byte with the value of 0. Now, printing an arbitrary value of a byte as a printable character is something that you definitely didn't intend her, and besides, it isn't always possible: some byte values don't correspond to any printable character! Read up on http://en.wikipedia.org/wiki/Character_encoding[^] for more information.

4. There's also the added problem that FinalValue is just long enough to hold the reinterpreted byte sequence - 1 byte in this case. It does not have sufficient space to also hold the terminating 0-character that cout expects, therefore cout keeps on reading the memory beyond the end of Finalvalue until it eventually encounters a 0-byte at some arbitrary location. Of course, the resulting output is equally arbitrary and may even vary if you repeatedly run the program.

Lessons learned:
a) If you pass an array of char to cout, it will be interpreted as a C-style 0-terminated string, and is expected to be terminated with a 0-byte.
b) A C-style string must allocate one additional byte on top of the length of the stored string to hold the terminating 0-byte.
c) You must ensure that the end of the actual string stored in a C-style string is followed by a 0-byte.
d) If your purpose of a char array is not to store actual characters, you don't need the terminating 0, but you passing it to cout will output garbage.

5. One thing I can't answer is how to print out FinalValue correctly: it depends what you wanted to see. If your input was meant to be just an arbitrary sequence of hexadecimal digits, of arbitrary length, then the best way to print this out on screen is a sequence of hexadecimal digits - i. e. the input string!

But if the input was meant to represent a single number, then your algorithm is doing the wrong thing! In that case, FinalValue should be of type int or long, not array of unsigned char!


看起来像是(有点丑陋)尝试将十六进制字符串转换为字节数组。但是,您应该使用,例如12,而不是0x12

此外你不应该以这种方式输出字节数组,而应该打印每个字节的值。

请注意,分配的内存不会被释放。
It looks like a (bit ugly) attempt to conver an hexadecimal string to a byte array. However, you should feed it with, for instance "12", instead of "0x12".
Moreover you shouldn't output the byte array that way, instead you should print the value of every individual byte.
Please note, allocated memory is not released.


这篇关于如何解释代码部分中的以下代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆