-
-
[旧帖] [原创]用C++写了一个字符串操作类,包括计算字符串长度,比较字符串,字符串转换整形... 0.00雪花
-
发表于: 2012-8-31 19:23 860
-
菜鸟第一次发帖,大家多多指教
class Convert { public: Convert(){} ~Convert(){}; //多字节转化成宽字符字符串 static void atou(char *pszAssicText, wchar_t *&pszUniText, unsigned long &cchUniText); //宽字符转化成多字节字符串 static void utoa(wchar_t *pszUniText, char *&pszAssicText, unsigned long &cchAssicText); //是否小写 static bool IsLower(int c); //是否大写 static bool IsUpper(int c); //转换成小写 static int ToLower(int c); //转换成大写 static int ToUpper(int c); //计算字符串长度 static int lstrlen(const char *str); //复制字符串到缓存区中 static char *lstrcpy(char *str1, const char *str2); //复制指定大小的字符串到缓冲区中 static char *lstrcpyn(char *str1, const char *str2, int num); //连接字符串 static char *lstrcat(char *str1, const char *str2); //比较字符串 static int lstrcmp(char *str1, char *str2); //比较字符串,比较指定字节 static int lstrncmp(char *str1, char *str2, int num); //比较字符串,忽略大小写 static int lstrcmpi(char *str1, char *str2); //比较字符串,比较指定字节,,忽略大小写 static int lstrncmpi(char *str1, char *str2, int num); //32位整形转化成字符串 static int itoa(int num, char *str); //32位无符号整形转化成字符串 static int uitoa(unsigned int num, char *str); //字符串转化成32位整形 static int atoi(char *str); //64位整形转化成字符串 static int i64toa(__int64 num, char *str); //64位无符号整形转化成字符串 static int ui64toa(unsigned __int64 num, char *str); //字符串转化成64位整形 static __int64 atoi64(char *str); }; bool Convert::IsLower(int c) { if(c >= 'a' && c <= 'z') return true; return false; } bool Convert::IsUpper(int c) { if(c >= 'A' && c <= 'Z') return true; return false; } int Convert::ToLower(int c) { if(IsUpper(c)) { return c + 32; } return c; } int Convert::ToUpper(int c) { if(IsLower(c)) { return c - 32; } return c; } void Convert::atou(char *pszAssicText, wchar_t *&pszUniText, unsigned long &cchUniText) { cchUniText = MultiByteToWideChar(CP_ACP, 0, pszAssicText, -1, 0, 0); pszUniText = new wchar_t[cchUniText]; MultiByteToWideChar(CP_ACP, 0, pszAssicText, -1, pszUniText, cchUniText); } void Convert::utoa(wchar_t *pszUniText, char *&pszAssicText, unsigned long &cchAssicText) { cchAssicText = WideCharToMultiByte(CP_OEMCP, 0, pszUniText, -1, 0, 0, 0, 0); pszAssicText = new char[cchAssicText]; WideCharToMultiByte(CP_OEMCP, 0, pszUniText, -1, pszAssicText, cchAssicText, 0, 0); } int Convert::lstrlen(const char *str) { int i; if(!str) return 0; for(i = 0; str[i]; i++); return i; } char *Convert::lstrcpy(char *str1, const char *str2) { int i; if(!str1 || !str2) return NULL; for(i = 0; str2[i]; i++) { str1[i] = str2[i]; } str1[i] = str2[i]; return str1; } char *Convert::lstrcpyn(char *str1, const char *str2, int num) { int i; if(!str1 || !str2) return NULL; for(i = 0; i < num; i++) { str1[i] = str2[i]; } str1[i] = '\0'; return str1; } char *Convert::lstrcat(char *str1, const char *str2) { int i; int j; if(!str1 || !str2) return NULL; for(i = 0; str1[i]; i++); for(j = 0; str2[j]; i++, j++) { str1[i] = str2[j]; } str1[i] = str2[j]; return str1; } int Convert::lstrcmp(char *str1, char *str2) { int i; if(!str1 || !str2) return 0; for(i = 0; (str1[i] == str2[i]) && str1[i] && str2[i]; i++); if(str1[i] == str2[i]) return 0; return (str1[i] > str2[i]) ? 1 : -1; } int Convert::lstrncmp(char *str1, char *str2, int num) { int i; if(!str1 || !str2 || !num) return 0; for(i = 0; (i < num - 1) && (str1[i] == str2[i]) && str1[i] && str2[i]; i++); if(str1[i] == str2[i]) return 0; return (str1[i] > str2[i]) ? 1 : -1; } int Convert::lstrcmpi(char *str1, char *str2) { int i; if(!str1 || !str2) return 0; for(i = 0; (ToLower(str1[i]) == ToLower(str2[i])) && str1[i] && str2[i]; i++); if(ToLower(str1[i]) == ToLower(str2[i])) return 0; return (ToLower(str1[i]) > ToLower(str2[i])) ? 1 : -1; } int Convert::lstrncmpi(char *str1, char *str2, int num) { int i; if(!str1 || !str2 || !num) return 0; for(i = 0; (ToLower(str1[i]) == ToLower(str2[i])) && (i < num - 1) && str1[i] && str2[i]; i++); if(ToLower(str1[i]) == ToLower(str2[i])) return 0; return (ToLower(str1[i]) > ToLower(str2[i])) ? 1 : -1; } int Convert::itoa(int num, char *str) { char tmp[15]; int mod; int quotient; int i = 0; int j = 0; if(num == 0) { str[0] = '\0'; return 0; } else if(num < 0) { num = 0 - num; tmp[i++] = '\0'; str[j++] = '-'; } else { tmp[i++] = '\0'; } quotient = num; for(; quotient; i++) { mod = quotient % 10; quotient = quotient / 10; tmp[i] = (unsigned char)(mod + 48); } for(; i--; j++) { str[j] = tmp[i]; } return j; } int Convert::uitoa(unsigned int num, char *str) { char tmp[15]; int mod; unsigned int quotient; int i = 0; int j = 0; if(num == 0) { str[0] = '\0'; return 0; } tmp[i++] = '\0'; quotient = num; for(; quotient; i++) { mod = quotient % 10; quotient = quotient / 10; tmp[i] = (unsigned char)(mod + 48); } for(; i--; j++) { str[j] = tmp[i]; } return j; } int Convert::atoi(char *str) { int num = 0; int bit = 1; bool bSign = false; int i; int j; if(str == NULL) return 0; if(str[0] == '-' || str[0] == '+') { bSign = true; } for(i = bSign; str[i]; i++) { if(str[i] < 48 || str[i] > 57) break; } for(j = 0; j < i - 1 - bSign; j++) { bit *= 10; } for(i = bSign; str[i]; i++) { num = num + bit * (str[i] - 48); bit /= 10; } if(str[0] == '-') { num = 0 - num; } return num; } int Convert::i64toa(__int64 num, char *str) { char tmp[35]; int mod; __int64 quotient; int i = 0; int j = 0; if(str == NULL) return 0; if(num == 0) { str[0] = '\0'; return 0; } else if(num < 0) { num = 0 - num; tmp[i++] = '\0'; str[j++] = '-'; } else { tmp[i++] = '\0'; } quotient = num; for(; quotient; i++) { mod = quotient % 10; quotient = quotient / 10; tmp[i] = (unsigned char)(mod + 48); } for(; i--; j++) { str[j] = tmp[i]; } return j; } int Convert::ui64toa(unsigned __int64 num, char *str) { char tmp[35]; int mod; unsigned __int64 quotient; int i = 0; int j = 0; if(str == NULL) return 0; if(num == 0) { str[0] = '\0'; return 0; } else if(num < 0) { num = 0 - num; tmp[i++] = '\0'; str[j++] = '-'; } else { tmp[i++] = '\0'; } quotient = num; for(; quotient; i++) { mod = quotient % 10; quotient = quotient / 10; tmp[i] = (unsigned char)(mod + 48); } for(; i--; j++) { str[j] = tmp[i]; } return j; } __int64 Convert::atoi64(char *str) { __int64 num = 0; __int64 bit = 1; bool bSign = false; int i; int j; if(str == NULL) return 0; if(str[0] == '-' || str[0] == '+') { bSign = true; } for(i = bSign; str[i]; i++) { if(str[i] < 48 || str[i] > 57) break; } for(j = 0; j < i - 1 - bSign; j++) { bit *= 10; } for(i = bSign; str[i]; i++) { num = num + bit * (str[i] - 48); bit /= 10; } if(str[0] == '-') { num = 0 - num; } return num; }
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
赞赏
他的文章
- [原创]支持Win7的星号密码查看器的实现 36232
- 此贴已删除 13513
- 此贴已删除 14969
- [原创]菜鸟对QQ一个Key生成算法分析 11714
- [原创]QQ记住密码MD5读取器(源码) 49369
看原图
赞赏
雪币:
留言: