免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 20988 | 回复: 11
打印 上一主题 下一主题

如何将16进制字符串转换成10进制? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-08 14:39 |只看该作者 |倒序浏览
我现在碰到一个很棘手的问题,我想把一个8位的16进制字符串转换成10位的10进制字符串,用strtol函数时发现只要16进制字符串大于7FFFFFFF以后,转换后的值就都变成了2147483647

例如:要转换95A7B387成10进制,正确的值应该是2510795655,可用strtol转换的结果却是2147483647

我已经查出这是由于long型的取值范围的局限,改用strtoul函数时发现结果的值是-1784171641

请问,有高人知道如何才能解决这样的问题????

万分感谢!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-03-08 14:47 |只看该作者

如何将16进制字符串转换成10进制?

用strtoull或strtoll。

论坛徽章:
0
3 [报告]
发表于 2004-03-08 15:00 |只看该作者

如何将16进制字符串转换成10进制?

我是hp-unix环境,好像没有strtoull和strtoll这样的函数吧?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2004-03-08 15:01 |只看该作者

如何将16进制字符串转换成10进制?

你man strtol试试。
我的是aix5l,也man不到它们两个,
不过man strtol就可以看到它们两个。

论坛徽章:
0
5 [报告]
发表于 2004-03-08 15:06 |只看该作者

如何将16进制字符串转换成10进制?

好像还是看不到啊,lenovo,你能介绍一下strtoll或strtoull的用法吗?

非常感谢!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2004-03-08 15:08 |只看该作者

如何将16进制字符串转换成10进制?


  1. #include <stdlib.h>;

  2. long strtol ( String, EndPointer, Base)

  3. const char *String;

  4. char **EndPointer;

  5. int Base;

  6. unsigned long strtoul (String, EndPointer, Base)

  7. const char *String;

  8. char **EndPointer;

  9. int Base;

  10. extern long long int strtoll (String, EndPointer, Base)

  11. char *String, **EndPointer;

  12. int Base;

  13. extern long long int strtoull (String, EndPointer, Base)

  14. char *String, **EndPointer;

  15. int Base;

  16. long atol (String)

  17. const char *String;

  18. int atoi (String)

  19. const char *String;

  20. Description

  21. The strtol subroutine returns a long integer whose value is represented by the
  22. character string to which the String parameter points. The strtol subroutine
  23. scans the string up to the first character that is inconsistent with the Base
  24. parameter. Leading white-space characters are ignored, and an optional sign may
  25. precede the digits.

  26. The strtoul subroutine provides the same functions but returns an unsigned long
  27. integer.

  28. The strtoll and strtoull subroutines provide the same functions but return long
  29. long integers.

  30. The atol subroutine is equivalent to the strtol subroutine where the value of
  31. the EndPointer parameter is a null pointer and the Base parameter is a value of
  32. 10.

  33. The atoi subroutine is equivalent to the strtol subroutine where the value of
  34. the EndPointer parameter is a null pointer and the Base parameter is a value of
  35. 10.

  36. If the value of the EndPointer parameter is not null, then a pointer to the
  37. character that ended the scan is stored in EndPointer. If an integer cannot be
  38. formed, the value of the EndPointer parameter is set to that of the String
  39. parameter.

  40. If the Base parameter is a value between 2 and 36, the subject sequence's
  41. expected form is a sequence of letters and digits representing an integer whose
  42. radix is specified by the Base parameter. This sequence is optionally preceded
  43. by a + (positive) or - (negative) sign. Letters from a (or A) to z (or Z)
  44. inclusive are ascribed the values 10 to 35; only letters whose ascribed values
  45. are less than that of the Base parameter are permitted. If the Base parameter
  46. has a value of 16, the characters 0x or 0X optionally precede the sequence of
  47. letters and digits, following the + (positive) or - (negative) sign if present.

  48. If the value of the Base parameter is 0, the string determines the base. Thus,
  49. after an optional leading sign, a leading 0 indicates octal conversion, and a
  50. leading 0x or 0X indicates hexadecimal conversion. The default is to use decimal
  51. conversion.

  52. Parameters

  53. String Points to the character string to be converted.

  54. EndPointer Points to a character string that contains the first character not
  55. converted.

  56. Base Specifies the base to use for the conversion.

  57. Return Values

  58. Upon successful completion, the strtol, strtoul, strtoll, and strtoull
  59. subroutines return the converted value. If no conversion could be performed, 0
  60. is returned, and the errno global variable is set to indicate the error. If the
  61. correct value is outside the range of representable values, the strtol
  62. subroutine returns a value of LONG_MAX or LONG_MIN according to the sign of the
  63. value, while the strtoul subroutine returns a value of ULONG_MAX.

  64. Error Codes

  65. The strtol and strtoul subroutines return the following error codes:

  66. ERANGE The correct value of the converted number causes underflow or overflow.

  67. EINVAL The value of the Base parameter is not valid.
复制代码

论坛徽章:
0
7 [报告]
发表于 2004-03-08 15:12 |只看该作者

如何将16进制字符串转换成10进制?

苦啊,hp-unix没有strtoll的函数,:(

有没有将16进制直接转换成10进制的double或float型函数?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
8 [报告]
发表于 2004-03-08 15:12 |只看该作者

如何将16进制字符串转换成10进制?

益出了。
自己编函数来解决。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
9 [报告]
发表于 2004-03-08 15:14 |只看该作者

如何将16进制字符串转换成10进制?

没有。既然溢出了,就想想别的办法吧。

论坛徽章:
0
10 [报告]
发表于 2004-03-08 15:17 |只看该作者

如何将16进制字符串转换成10进制?

:(

谢谢俩位!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP