十六进制大于4字节的strtol? [英] strtol with hex greater than 4 byte ?
问题描述
我会检查一个应该包含十六进制值的字符串。
我知道strtol是这项工作的正确功能。
但是当我检查一个8字节的十六进制字符串会发生什么?
我可以检查字符串是否正确我知道。
长值;
char src [] =" 0x1234567812345678"
char * err;
value = strtol (src,& err,16);
if(* err!= 0x00)
返回-1; / * FEHLER * /
值不是0x12345678但是我知道它拼写正确。
是C89标准还是只在我身上编译器/操作系统(VC6,Windows)?
有没有办法获得字符串开头的正确值,
到sizeof(很长)?
Thx
彼得
Hi,
I will check a String which should contain a HEX value.
I know that strtol is the right function for this job.
But what happens when I will check a hex string with 8 bytes?
That I can check that the string is correct i know.
long value;
char src[]="0x1234567812345678"
char *err;
value = strtol(src,&err, 16);
if (*err != 0x00)
return -1; /*FEHLER*/
The value is not 0x12345678 but I know it is right spelled.
Is that C89 standard or only on my compiler/OS (VC6, Windows) ?
Is there a way to get the right value of the beginning of the string,
up to the sizeof(long) ?
Thx
Peter
推荐答案
Peter Dunker写道:
Peter Dunker wrote:
我将检查一个应该包含HEX值的字符串。
我知道strtol是这项工作的正确功能。
但是当我检查一个8字节的十六进制字符串会发生什么呢?
我知道可以检查字符串是否正确。
值很长;
char src [] =" 0x1234567812345678"
char * err;
value = strtol(src,& err,16);
请注意,在这种情况下(字符串以0x开头),您可以将
base设置为0并让strtol()计算出来。只是一个评论;
显式通常是一种很好的编程习惯。 :-)
if(* err!= 0x00)
return -1; / * FEHLER * /
值不是0x12345678但我知道它拼写正确。
是C89标准还是只在我的编译器/操作系统(VC6,Windows)上?
除非您的平台上有64位长(我认为不是,但是
而不是32位),strtol()处理完整的src []和溢出。
根据K& R,第252页返回LONG_MAX,确实
不等于0x12345678。另外errno设置为ERANGE。
有没有办法获得字符串开头的正确值,
最大尺寸(长)?
我不知道,使用标准库函数。在这种情况下,你必须自己做b
$ b。例如,您可以将字符串
复制到sizeof长字节,并将该副本传递给strtol()。
Thx
Peter
Hi,
I will check a String which should contain a HEX value.
I know that strtol is the right function for this job.
But what happens when I will check a hex string with 8 bytes?
That I can check that the string is correct i know.
long value;
char src[]="0x1234567812345678"
char *err;
value = strtol(src,&err, 16);
Note that in this case (string starts with "0x") you could set
base to 0 and let strtol() figure it out. Just a remark; being
explicit is usually a good programming habit. :-)
if (*err != 0x00)
return -1; /*FEHLER*/
The value is not 0x12345678 but I know it is right spelled.
Is that C89 standard or only on my compiler/OS (VC6, Windows) ?
Unless long is 64-bit on your platform (which I assume is not, but
rather 32-bit), strtol() processes the full src[] and overflows.
According to K&R, page 252 LONG_MAX is returned, which is indeed
not equal to 0x12345678. Also errno is set to ERANGE.
Is there a way to get the right value of the beginning of the string,
up to the sizeof(long) ?
Not that I know of, using standard library functions. In that case you
must do it yourself. You could for example make a copy of the string
up to sizeof long bytes and pass that copy to strtol().
Thx
Peter
" Case" < no@no.no> schrieb im Newsbeitrag
news:40 ********************* @ news.xs4all.nl ...
"Case" <no@no.no> schrieb im Newsbeitrag
news:40*********************@news.xs4all.nl...
Peter Dunker写道:
Peter Dunker wrote:
我将检查一个应该包含HEX值的字符串。
我知道strtol是正确的函数工作。
但是当我检查一个8字节的十六进制字符串会发生什么?
我可以检查字符串是否正确我知道。
值很长;
char src [] =" 0x1234567812345678"
char * err;
value = strtol(src,& err,16);
请注意,在这种情况下(字符串)以0x开头)你可以将
base设置为0并让strtol()将其弄清楚。只是一个评论;显而易见通常是一种很好的编程习惯。 : - )
Hi,
I will check a String which should contain a HEX value.
I know that strtol is the right function for this job.
But what happens when I will check a hex string with 8 bytes?
That I can check that the string is correct i know.
long value;
char src[]="0x1234567812345678"
char *err;
value = strtol(src,&err, 16);
Note that in this case (string starts with "0x") you could set
base to 0 and let strtol() figure it out. Just a remark; being
explicit is usually a good programming habit. :-)
我知道0的可能性,但字符串应该是/必须是十六进制
值,如果有可能得到使用在strtol之后完成基础,这将是创造,但我不这么认为。
我写了一个大的if用str [0]和str [1]在字符串的开头检查ax X 0x 0X
然后执行strtol。
I know the possibility with 0, but the string should/must be a hex
value, if there is a possibilty to get the used base after strtol is
done, that will be create, but I don''t think so.
I have written a big "if" with str[0] and str[1] to check a x X 0x 0X
at the beginning of the string and do then the strtol.
if(* err!= 0x00)
return -1; / * FEHLER * /
值不是0x12345678但我知道它拼写正确。
是C89标准还是只在我的编译器/操作系统(VC6,Windows)上?
if (*err != 0x00)
return -1; /*FEHLER*/
The value is not 0x12345678 but I know it is right spelled.
Is that C89 standard or only on my compiler/OS (VC6, Windows) ?
除非您的平台上有64位长(我认为不是,但是相当于32位),strtol()会处理完整的src []和溢出。
根据K& R,第252页返回LONG_MAX,这确实不等于0x12345678。另外errno设置为ERANGE。
Unless long is 64-bit on your platform (which I assume is not, but
rather 32-bit), strtol() processes the full src[] and overflows.
According to K&R, page 252 LONG_MAX is returned, which is indeed
not equal to 0x12345678. Also errno is set to ERANGE.
Thx,听起来很有趣。
Thx, that sounds interessting.
有没有办法得到
字符串开头的正确值,直到sizeof(long)?
Is there a way to get the right value of the beginning of the string, up to the sizeof(long) ?
不是我所知道的,使用标准库函数。在那种情况下
Not that I know of, using standard library functions. In that case
你必须自己做。例如,您可以制作一个字符串
的副本,直到sizeof长字节并将该副本传递给strtol()。
you must do it yourself. You could for example make a copy of the string
up to sizeof long bytes and pass that copy to strtol().
好的我会做这样的事情,当我需要价值。
Thx
彼得
OK I will do something like this, when I need the value.
Thx
Peter
Peter Dunker写道:
Peter Dunker wrote:
案例 < no@no.no> schrieb im Newsbeitrag
新闻:40 ********************* @ news.xs4all.nl ...
"Case" <no@no.no> schrieb im Newsbeitrag
news:40*********************@news.xs4all.nl...
Peter Dunker写道:
Peter Dunker wrote:
我会检查一个应该包含HEX值的字符串。
我知道strtol是这项工作的正确功能。
但是当我检查一个8字节的十六进制字符串时会发生什么?
我知道可以检查字符串是否正确。
long值;
char src [] =" 0x1234567812345678"
char * err;
value = strtol(src,& err,16);
Hi,
I will check a String which should contain a HEX value.
I know that strtol is the right function for this job.
But what happens when I will check a hex string with 8 bytes?
That I can check that the string is correct i know.
long value;
char src[]="0x1234567812345678"
char *err;
value = strtol(src,&err, 16);
>
请注意,在这种情况下(字符串以0x开头),您可以将
base设置为0并让strtol()将其计算出来。只是一个评论;显而易见通常是一种很好的编程习惯。 : - )
Note that in this case (string starts with "0x") you could set
base to 0 and let strtol() figure it out. Just a remark; being
explicit is usually a good programming habit. :-)
我知道0的可能性,但字符串应该/必须是hex值,如果在strtol之后有可能得到使用的基数
完成了,这将是创造,但我不这么认为。
我写了一个大的if用str [0]和str [1]在字符串的开头检查ax X 0x 0X
然后执行strtol。
I know the possibility with 0, but the string should/must be a hex
value, if there is a possibilty to get the used base after strtol is
done, that will be create, but I don''t think so.
I have written a big "if" with str[0] and str[1] to check a x X 0x 0X
at the beginning of the string and do then the strtol.
根据K& R x和X不使字符串为十六进制。只有0x或
0X才有效。所以看这个! ---(你究竟想要用这个测试来获得
?我的意思是,你想要的唯一的东西是0x或0X
可能会出错吗?这个字符串来自用户输入?)---注意
According to K&R x and X do not make the string hexadecimal. Only 0x or
0X are valid. So watch this! ---(What precisely do you want to catch
with this test? I means, is the 0x or 0X the only thing you expect to
go wrong possibly? Does this string come from user input?)---note
这篇关于十六进制大于4字节的strtol?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!