当前位置:Gxlcms > PHP教程 > PHP不使用内置函数实现字符串转整型的方法详解

PHP不使用内置函数实现字符串转整型的方法详解

时间:2021-07-01 10:21:17 帮助过:25人阅读

一般php字符串类型的数字如果想转成整型的数字,我们都是采用系统内置的API去做转换,但下面这篇文章主要给大家介绍了关于PHP不使用内置函数实现字符串转整型的方法示例,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。

介绍

php字符串类型的数字如果想转成整型的数字,一般我们都是采用系统内置的API去做转换,但如果规定就不让我们去用系统内置的API转换,而是让自己去实现一个函数转换该怎么办?这里我们看下如何去实现。

系统内置 API 方式

  1. $num = '345432123';
  2. //(一)
  3. $num = (int)$num;
  4. //
输出: //int(345432123) //(二) $num = intval($num); //输出: //int(345432123)

采用 ASCII 码方式

下面我们利用 ascii 码的方式去做转换,因为每个字符都对应一个 ascii 码,当对这个字符做加减乘除的时候,实际上就是对 ascii 码做加减乘除操作,也就是整型操作,最终会返回一个整型数字.


-图片转自网络-

通过上图可以看到字符 '0' ~ '9' 的 ascii 码是 48~57 我们在转换的时候就是用每一个字符减去 '0' 例如: '1' - '0' = 1、'2' - '0' = 2 返回值就是一个Int类型,下面具体看代码实现.

  1. function convertInt($strInt = ''){
  2. $len = strlen($strInt);
  3. $int = 0;
  4. for($i=0;$i<$len;$i++){
  5. $int *= 10;
  6. $num = $strInt{$i} - '0';
  7. $int += $num;
  8. }
  9. return $int;
  10. }
  11. $num = '345432123';
  12. var_dump(convertInt($num)); //
输出: int(345432123)

在 Redis 里面也有提供一个字符串转整型的函数,也是通过ascii码方式去做的,实现的比较完善严谨,具体可以参考下

string2ll 函数

  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <string.h>
  4. /* Convert a string into a long long. Returns 1 if the string could be parsed
  5. * into a (non-overflowing) long long, 0 otherwise. The value will be set to
  6. * the parsed value when appropriate. */
  7. int string2ll(const char *s, size_t slen, long long *value) {
  8. const char *p = s;
  9. size_t plen = 0;
  10. int negative = 0;
  11. unsigned long long v;
  12. if (plen == slen)
  13. return 0;
  14. /* Special case: first and only digit is 0. */
  15. if (slen == 1 && p[0] == '0') {
  16. if (value != NULL) *value = 0;
  17. return 1;
  18. }
  19. if (p[0] == '-') {
  20. negative = 1;
  21. p++; plen++;
  22. /* Abort on only a negative sign. */
  23. if (plen == slen)
  24. return 0;
  25. }
  26. /* First digit should be 1-9, otherwise the string should just be 0. */
  27. if (p[0] >= '1' && p[0] <= '9') {
  28. v = p[0]-'0';
  29. p++; plen++;
  30. } else if (p[0] == '0' && slen == 1) {
  31. *value = 0;
  32. return 1;
  33. } else {
  34. return 0;
  35. }
  36. while (plen < slen && p[0] >= '0' && p[0] <= '9') {
  37. if (v > (ULLONG_MAX / 10)) /* Overflow. */
  38. return 0;
  39. v *= 10;
  40. if (v > (ULLONG_MAX - (p[0]-'0'))) /* Overflow. */
  41. return 0;
  42. v += p[0]-'0';
  43. p++; plen++;
  44. }
  45. /* Return if not all bytes were used. */
  46. if (plen < slen)
  47. return 0;
  48. if (negative) {
  49. if (v > ((unsigned long long)(-(LLONG_MIN+1))+1)) /* Overflow. */
  50. return 0;
  51. if (value != NULL) *value = -v;
  52. } else {
  53. if (v > LLONG_MAX) /* Overflow. */
  54. return 0;
  55. if (value != NULL) *value = v;
  56. }
  57. return 1;
  58. }
  59. //-------- 执行 ---------
  60. int main(){
  61. long long num;
  62. string2ll("345432123",strlen("345432123"),&num);
  63. printf("%d\n",num); //
输出 345432123 retunr 0; }

相关推荐:

PHP实现汉字转整型数字,如:一百零一 转成101

sql CONVERT() 字符串转整型 日期 字符型

PHP实现不使用内置函数实现字符串转整型方法

以上就是PHP不使用内置函数实现字符串转整型的方法详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行