数组和指针的困难 [英] difficulties with arrays and pointers

查看:77
本文介绍了数组和指针的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将十六进制值更改为整数.

在无符号char INBuffer [2]中,有一个0x0a等格式的十六进制代码.我想使用strtol函数将此值转换为long或int变量:

I''m having great difficulty changing a hex value into an integer.

In an unsigned char INBuffer[2], there is a hex code in the format 0x0a etc. I would like to convert this value into a long or int variable using the strtol function:

long int strtol ( const char * str, char ** endptr, int base );



为此,我假设我需要将INBuffer [2]中包含的值转换为字符串指针,但是无论我查找了多少示例,我似乎都无法理解指针.有人可以帮忙吗?

M.



For this I''m assuming I need to get the value contained within INBuffer[2] into a string pointer, but no matter how many examples I look up i just can''t seem to get my head around pointers. Could somebody please help?

M.

推荐答案

在我看来有点困惑:具有两个元素的无符号char缓冲区不能保存格式为"0x0a"的十六进制代码是四个字符.

如果您的意思是两个无符号字符中都有一个等于十六进制值0A的值,并且其范围介于0000和FFFF十六进制之间,那么您需要执行以下操作:
That seems a little confused to me: an unsigned char buffer with two elements can''t hold a hex code in the format "0x0a" since that is four characters.

If you mean that there is a value in the two unsigned chars that equates to the hex value 0A, and that it has a range between 0000 and FFFF hex, then you need to do this:
int i = ((int) INBuffer[0] & 0xFF) + (((int) INBuffer[1] << 8) & 0xFF00);


(那里有AND可以防止符号扩展,无论如何都不应发生).您可能需要根据数据格式交换数组索引:big-endian或little-little-endian

如果您的意思是INBuffer包含两个分别为"0"和"A"的字节,那么您需要将它们复制到一个足以容纳三个字节的字符数组中,并使其成为以null结尾的字符串:


(The ANDs are there to prevent sign extentions, which shouldn''t happen anyway) You may need to swap round the array indexes depending on your data format: big- or little-endian

If you mean that INBuffer contains two bytes which are ''0'' and ''A'' respectively, then you need to copy them to a character array big enough to hold three bytes, and make it a null terminated string:

unsigned char data[3];
data[0] = INBuffer[0];
data[1] = INBuffer[1];
data[2] = '\0';

然后可以使用它将其转换为int:

You can then use that to convert it to a int:

char * pEnd;
long int i = strtol(data, &pEnd, 16);


我对您的问题感到困惑,但让我们尝试一下...

您有一个未签名的char INBuffer [2].这意味着它是一个两个字符的缓冲区.可以使用INBuffer [0]和INBuffer [1]分别访问此缓冲区的两个元素.

如果要在INBuffer [0]中包含十进制值,则可以执行此操作
I''m confused by your question, but let''s try this...

You have an unsigned char INBuffer[2]. This means it''s a two character buffer. The two elements of this buffer can be accessed individually by using INBuffer[0] and INBuffer[1].

If you want the decimal value contained in INBuffer[0], you can do this
int buf0val = (int)INBuffer[0];


同样,如果您希望该缓冲区的第二个字符包含该值,则可以执行此操作


Likewise if you want the value contained in the second character of this buffer, you can do this

int buf1val = (int)INBuffer[1]



但是,如果您的意思是第一个字符包含ASCII数字0,第二个字符包含ASCII"A",则您遇到了另一个问题.

您不能对此使用strtol,因为此(两个字符)字符串不是NULL终止的-就像上面的OriginalGriff所说的那样.

希望对您有所帮助.



However if you mean that the first character contains the ASCII number 0 and the second character contains the ASCII ''A'', the you have a different problem.

You can''t use strtol on this, because this (two character) string is not NULL terminated - just as OriginalGriff said above.

Hope that helps.


我认为您可以这样做,

I think You can do like this,

char*chVal  = "0x0a";
cout<<"size:: "<<sizeof(chVal)<<endl;
long lVal = strtol(chVal, NULL, 16);


这篇关于数组和指针的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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