PHP不使用內(nèi)置函數(shù)實(shí)現(xiàn)字符串轉(zhuǎn)整型的方法示例
介紹
php字符串類型的數(shù)字如果想轉(zhuǎn)成整型的數(shù)字,一般我們都是采用系統(tǒng)內(nèi)置的API去做轉(zhuǎn)換,但如果規(guī)定就不讓我們?nèi)ビ孟到y(tǒng)內(nèi)置的API轉(zhuǎn)換,而是讓自己去實(shí)現(xiàn)一個(gè)函數(shù)轉(zhuǎn)換該怎么辦?這里我們看下如何去實(shí)現(xiàn)。
系統(tǒng)內(nèi)置 API 方式
$num = '345432123'; //(一) $num = (int)$num; //輸出: //int(345432123) //(二) $num = intval($num); //輸出: //int(345432123)
采用 ASCII 碼方式
下面我們利用 ascii 碼的方式去做轉(zhuǎn)換,因?yàn)槊總€(gè)字符都對(duì)應(yīng)一個(gè) ascii 碼,當(dāng)對(duì)這個(gè)字符做加減乘除的時(shí)候,實(shí)際上就是對(duì) ascii 碼做加減乘除操作,也就是整型操作,最終會(huì)返回一個(gè)整型數(shù)字.

-圖片轉(zhuǎn)自網(wǎng)絡(luò)-
通過上圖可以看到字符 '0' ~ '9' 的 ascii 碼是 48~57 我們?cè)谵D(zhuǎn)換的時(shí)候就是用每一個(gè)字符減去 '0' 例如: '1' - '0' = 1、'2' - '0' = 2 返回值就是一個(gè)Int類型,下面具體看代碼實(shí)現(xiàn).
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)
在 Redis 里面也有提供一個(gè)字符串轉(zhuǎn)整型的函數(shù),也是通過ascii碼方式去做的,實(shí)現(xiàn)的比較完善嚴(yán)謹(jǐn),具體可以參考下
string2ll 函數(shù)
#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] <= '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;
}
//-------- 執(zhí)行 ---------
int main(){
long long num;
string2ll("345432123",strlen("345432123"),&num);
printf("%d\n",num); //輸出 345432123
retunr 0;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- MySQL模糊查詢用法大全(正則、通配符、內(nèi)置函數(shù))
- 自學(xué)MySql內(nèi)置函數(shù)知識(shí)點(diǎn)總結(jié)
- MySQL高效模糊搜索之內(nèi)置函數(shù)locate instr position find_in_set使用詳解
- PHP內(nèi)置函數(shù)生成隨機(jī)數(shù)實(shí)例
- PHP使用內(nèi)置函數(shù)生成圖片的方法詳解
- 幾個(gè)實(shí)用的PHP內(nèi)置函數(shù)使用指南
- PHP通過內(nèi)置函數(shù)memory_get_usage()獲取內(nèi)存使用情況
- MySQL與PHP的基礎(chǔ)與應(yīng)用專題之內(nèi)置函數(shù)
相關(guān)文章
php strrpos()與strripos()函數(shù)
以下是對(duì)php中的strrpos函數(shù)與strripos函數(shù)的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下2013-08-08
php使用APC實(shí)現(xiàn)實(shí)時(shí)上傳進(jìn)度條功能
這篇文章主要介紹了php使用APC實(shí)現(xiàn)實(shí)時(shí)上傳進(jìn)度條功能,php本身不具備可以帶有實(shí)時(shí)上傳進(jìn)度條功能,但是php提供了一個(gè)apc,它可以與php配置實(shí)現(xiàn)上傳進(jìn)度條,感興趣的小伙伴們可以參考一下2015-10-10
php安全之直接用$獲取值而不$_GET 字符轉(zhuǎn)義
php安全之直接用$獲取值而不$_GET 字符轉(zhuǎn)義,需要的朋友可以參考下2012-06-06
linux使用crontab實(shí)現(xiàn)PHP執(zhí)行計(jì)劃定時(shí)任務(wù)
前幾天寫過一篇文章,利用單純的php實(shí)現(xiàn)定時(shí)執(zhí)行任務(wù),但是效率不佳,對(duì)于linux來說用crontab實(shí)現(xiàn)更加合理2014-05-05
PHP substr 截取字符串出現(xiàn)亂碼問題解決方法[utf8與gb2312]
在PHP中,使substr函數(shù)截取字符串末位會(huì)出現(xiàn)亂碼,因?yàn)橹形腢TF-8編碼,每個(gè)漢字占3字節(jié),而GB2312占2字節(jié),英文占1字節(jié),截取位不準(zhǔn)確,造成斷開的字符會(huì)把其后的..拉過來一起做一個(gè)字,所以出現(xiàn)了亂碼。2011-12-12

