PHP does not use built-in functions to implement string-to-integer methods

Source: Internet
Author: User
Tags integer numbers
General PHP String type of numbers if you want to turn into integer numbers, we are using the system built-in API to do the conversion, but the following this article mainly introduces you about PHP does not use the built-in function to implement the string-to-integer Method example, the article describes the very detailed, the need for friends can reference, Let's take a look below.

Introduced

PHP String type of numbers if you want to turn into integer numbers, generally we are using the system built-in API to do the conversion, but if the rules do not let us go to use the system built-in API conversion, but let ourselves to implement a function transformation what to do? Here we look at how to achieve.

System built-in API mode

$num = ' 345432123 '; (i) $num = (int) $num;//output://int (345432123)//(ii) $num = Intval ($num);//output://int (345432123)

Using ASCII code mode

Here we use ASCII code to do the conversion, because each character corresponds to an ASCII code, when the character is subtraction, is actually the ASCII code to do subtraction operation, that is, the integer operation, will eventually return an integer number.


-Images transferred from the web-

The ASCII code that can be seen by the character ' 0 ' ~ ' 9 ' is 48~57 we are converting with each character minus ' 0 ' for example: ' 1 '-' 0 ' = 1, ' 2 '-' 0 ' = 2 The return value is an int type, as shown below in the code implementation.

function Convertint ($strInt = ") {  $len = strlen ($strInt);  $int = 0; for ($i =0; $i < $len; $i + +) {  $int *=;     $num = $strInt {$i}-' 0 ';     $int + = $num;   } return $int;  } $num = ' 345432123 ';  Var_dump (Convertint ($num)); Output: Int (345432123)

In the Redis also provides a string to the function of the integral type, but also through the ASCII code way to do, the implementation of the relatively perfect rigorous, specific can refer to the next

String2ll function

#include <stdio.h> #include <limits.h> #include <string.h>/* Convert a string into a long long. Returns 1 If the string could is parsed * into a (non-overflowing) long long, 0 otherwise. The value is set to * the parsed value when appropriate. */int string2ll (const char *s, size_t slen, Long long *value) {const char *p = s; size_t plen = 0; int negative = 0; unsi gned Long Long V; if (Plen = = Slen) return 0; /* Special Case:first and only digit is 0.  */if (Slen = = 1 && p[0] = = ' 0 ') {if (value! = NULL) *value = 0; return 1;  } if (p[0] = = '-') {negative = 1; p++;  plen++; /* Abort on only a negative sign. */if (Plen = = Slen) return 0; }/* First digit should be 1-9, otherwise the string should just is 0.  */if (p[0] >= ' 1 ' && p[0] <= ' 9 ') {v = p[0]-' 0 '; p++; plen++;  } else if (p[0] = = ' 0 ' && slen = = 1) {*value = 0; return 1; } else {return 0;} while (Plen < Slen && p[0] >= ' 0 ' && p[0] <= ' 9 ') {if (V > (ULLONG_MAX/10))/* Overflow. */return 0;  V *= 10; if (V > (Ullong_max-(p[0]-' 0 '))/*/Overflow.  */return 0;  V + = p[0]-' 0 '; p++; plen++; }/* Return if not all bytes were used. */if (Plen < Slen) return 0;  if (negative) {if (V > (unsigned long) (-(llong_min+1)) +1)//* Overflow. */return 0; if (value = NULL) *value =-V;  } else {if (V > Llong_max)/* Overflow. */return 0; if (value = NULL) *value = v; } return 1;} --------executes---------int main () {long long num; STRING2LL ("345432123", strlen ("345432123"), &num); printf ("%d\n", NUM); Output 345432123 Retunr 0;}

Related recommendations:

PHP implementation of the number of Chinese characters, such as: 101 Turn 101

SQL CONVERT () string to integer date character type

PHP implementation does not use built-in functions to implement string -to-integer methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.