- 字符串轉(zhuǎn)換為 unsigned int 類型
/*將傳入的字符串轉(zhuǎn)換為無符號的的32位整形
*@param: str : 傳入的字符串
*retval: The converted value.
*/
static unsigned int atoui(const char *str);
unsigned int atoui(const char *str)
{
unsigned int result = 0, i = 0;
char *tmp = NULL;
for (i = 0; isspace(str[i]) && i < strlen(str); i++)//跳過空白符;
;
tmp = str+i;
while (*tmp)
{
result = result * 10 + *tmp - '0';
tmp++;
}
return result;
}
2. 將uint32 類型轉(zhuǎn)換為 C字符串, 默認采用 10進制
char* uitoa(unsigned int n, char *s)
{
int i, j;
i = 0;
char buf[20];
memset(buf, 0, sizeof(buf));
do{
buf[i++] = n % 10 + '0';//取下一個數(shù)字
} while ((n /= 10)>0);//刪除該數(shù)字
i -= 1;
for (j = 0; i >= 0; j++, i--)//生成的數(shù)字是逆序的,,所以要逆序輸出
s[j] = buf[i];
s[j] = '\0';
return s;
}
3. 自定義LOG輸出宏函數(shù),
#define myprint( x...) do {char bufMessagePut_Stdout_[1024]; sprintf(bufMessagePut_Stdout_, x); fprintf(stdout, "%s [%d], [%s]\n", bufMessagePut_Stdout_,__LINE__, __FILE__ ); }while (0)
int main()
{
myprint("The num : %d, The string : %s", 98, "nihao");
return 0;
}
//該函數(shù)宏要注意的地方: 宏中的局部參數(shù): bufMessagePut_Stdout_, 不可以與外面的傳進參數(shù)同名 ,同名時,宏替換會產(chǎn)生歧義:
sprintf(bufMessagePut_Stdout_, "%s", bufMessagePut_Stdout_);
此時: 前后bufMessagePut_Stdout_應(yīng)代表不同的參數(shù)類型及意義,而程序上并不會這樣認為.
4.將C風格 16進制的字符串 轉(zhuǎn)換為十進制(即: 默認字符串中存的內(nèi)容為 16進制數(shù)據(jù))
//獲取x的y次方; x^y
unsigned int power(int x, int y)
{
unsigned int result = 1;
while (y--)
result *= x;
return result;
}
//將字符串的內(nèi)容轉(zhuǎn)換為 16進制
unsigned int hex_conver_dec(char *src)
{
char *tmp = src;
int len = 0;
unsigned int hexad = 0;
char sub = 0;
while (1)
{
if (*tmp == '0') //去除字符串 首位0
{
tmp++;
continue;
}
else if (*tmp == 'X')
{
tmp++;
continue;
}
else if (*tmp == 'x')
{
tmp++;
continue;
}
else
break;
}
len = strlen(tmp);
for (len; len > 0; len--)
{
sub = toupper(*tmp++) - '0';
if (sub > 9)
sub -= 7;
hexad += sub * power(16, len - 1);
}
return hexad;
}
int main(){
hex_conver_dec("31"); //49
hex_conver_dec("abcd02"); //11259138
}
5.在一塊內(nèi)存中查找子串位置:該函數(shù)主要是為了在二進制文本中進行查找操作,。
//獲取字符流中的指定子串的位置
char* memstr(char* full_data, int full_data_len, char* substr)
{
if (full_data == NULL || full_data_len <= 0 || substr == NULL) {
return NULL;
}
if (*substr == '\0') {
return NULL;
}
int sublen = strlen(substr);
int i;
char* cur = full_data;
int last_possible = full_data_len - sublen + 1;
for (i = 0; i < last_possible; i++) {
if (*cur == *substr) {
//assert(full_data_len - i >= sublen);
if (memcmp(cur, substr, sublen) == 0) {
//found
return cur;
}
}
cur++;
}
return NULL;
}
6. 查看指定目錄下的文件是否存在
bool if_file_exist(const char *filePath)
{
if(filePath == NULL)
assert(0);
if(access(filePath, F_OK) == 0)
return true;
return false;
}
7. 將unsigned int 轉(zhuǎn)換為16進制C字符串
char *unsigned_int_to_hex(unsigned int number, char *hex_str)
{
char b[17] = { "0123456789ABCDEF" };
int c[8], i = 0, j = 0, d, base = 16;
do{
c[i] = number % base;
i++;
number = number / base;
} while (number != 0);
hex_str[j++] = '0';
hex_str[j++] = 'X';
for (--i ; i >= 0; --i, j++)
{
d = c[i];
hex_str[j] = b[d];
}
hex_str[j] = '\0';
return hex_str;
}
|