PHP implements string conversion method without using built-in functions

小云云
Release: 2023-03-20 08:06:01
Original
1350 people have browsed it

If we want to convert php string type numbers into integer numbers, we usually use the system's built-in API to do the conversion. However, if the regulations do not allow us to use the system's built-in API to convert, we will let ourselves What should I do to implement a function conversion? Here we look at how to achieve it.

System built-in API method

$num = '345432123';

 //(一)
$num = (int)$num;
//输出:
//int(345432123)

//(二)
$num = intval($num);
//输出:
//int(345432123)
Copy after login

Use ASCII code method

Below we use ascii code method to do conversion, because each Each character corresponds to an ASCII code. When adding, subtracting, multiplying, and dividing this character, it is actually adding, subtracting, multiplying, and dividing the ASCII code, that is, an integer operation, which will eventually return an integer number.


-The picture is transferred from the Internet-

You can see from the picture above that the ascii codes of the characters '0' ~ '9' are 48~57. When converting, we use each Character minus '0', for example: '1' - '0' = 1, '2' - '0' = 2 The return value is an Int type, see the code implementation below.

function convertInt($strInt = ''){ 
 $len = strlen($strInt); 
 $int = 0;

 for($i=0;$i<$len;$i++){
  $int *= 10;   
  $num = $strInt{$i} - '0';   
  $int += $num;  
 }

 return $int;  
}

 $num = '345432123'; 
 var_dump(convertInt($num)); //输出: int(345432123)
Copy after login

In Redis There is also a function for converting string to integer, which is also done through ASCII code. The implementation is relatively complete and rigorous. For details, please refer to

string2ll function

#include 
#include 
#include 

/* Convert a string into a long long. Returns 1 if the string could be parsed
 * into a (non-overflowing) long long, 0 otherwise. The value will be 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;
 unsigned 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 be 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 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;
}

//-------- 执行 ---------
int main(){
 long long num;
 string2ll("345432123",strlen("345432123"),&num);
 printf("%d\n",num); //输出 345432123
 retunr 0;
}
Copy after login

Related recommendations :

sql CONVERT() Convert string to integer date character type

The above is the detailed content of PHP implements string conversion method without using built-in functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!