/* 功能:將輸入的string型別的ip資訊轉換為string型別
* 輸入:string型別的ip資訊
* 輸出:dword結果,正常返回解析結果值,異常時,dwip為0
* 返回:返回解析的整型,異常時返回"0"
**:
#include #include #include "oj.h"
#include #include using namespace std;
bool effective(string strip)//檢查ip位址的有效性
if(*ip >= '0' && *ip <= '9' || *ip == '.')
else
}if(cnt != 3)
return false;
return true;
}string getvaluebyip(string strip)
; unsigned char value[4] = ;//存放ip位址的四個數字
char word[10] = ;
int cnt = 0,cntnum = 0;
char *ip = (char*)strip.c_str();
while(*ip != '\0')//拆分到value
word[cnt] = '\0';
cnt = 0;
if(atoi(word) > 255 || atoi(word) < 0)
return "0";
value[cntnum++] = atoi(word);
if(cntnum == 4)
break;
ip++;
} res = (value[0]<<24) | (value[1]<<16) | (value[2]<<8) | value[3];//將四個數字進行拼接成乙個整數
sprintf_s(resip,sizeof(resip),"%u\0",res);
string result = resip;
return result;
}
C C 程式設計題之整數轉IP位址
將輸入的整數ip資訊轉換為string型ip資訊,即x.x.x.x的格式 輸入 string型別的值 輸出 x.x.x.x的格式的ip資訊 返回 正常解析時返回非空的ip資訊,異常時strip為空值 string getipbyvalue string strvalue unsigned int v...
IP位址轉 整數互相轉換
知識點 乙個二進位制數,按位左移n位,就是把該數的值乘以2的n次方 二進位制除二即右移一位 1 ip位址轉換為整數 乙個無符號32為整數。每段數字 相對應的二進位制數 10 00001010 0 00000000 3 00000011 193 11000001 public class ip pub...
程式設計題 整數與IP位址間的轉換
程式設計題 整數與ip位址間的轉換 原理 ip位址的每段可以看成是乙個0 255的整數,把每段拆分成乙個二進位制形式組合起來,然後把這個二進位制數轉變成乙個長整數。舉例 乙個ip位址為10.0.3.193 每段數字 相對應的二進位制數 10 00001010 0 00000000 3 0000001...