


PHP implements string conversion method without using built-in functions
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)
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<p>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 </p><p>string2ll function<br></p><pre class="brush:php;toolbar:false">#include <stdio.h> #include <limits.h> #include <string.h> /* 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] = '0' && p[0] (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 ((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; }</string.h></limits.h></stdio.h>
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!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
