Feed on
Posts
Comments

Tag Archive 'C/C++'

前言 最近在整合公司內部 Atheros(被高通買下) 晶片的 Router,從原本 2.6.15 升級到 2.6.34.7,升級過程遇到很多困難,其中一項升級 Wireless Driver 部份,發現在 Kernel Socket 與 User Space 溝通之間出了問題,利用 Ioctl 來取得目前在 AP 上面所有 Client 資料(包含 mac address, 處於 N or G mode…等),在 User Space 上會掉資料,後來利用 /proc 底下檔案來跟 User 之間溝通,才沒有發生任何問題,由於輸出的檔案比較多,就偏向用 2.6 Kernel 提供的 seq_file 介面( interface )建立虛擬檔案 (virtual file) 與 User Space 溝通(此方法為 Alexander Viro 所設計),此功能其實在 2.4.15 已經實做了,只是在 [...]

Read Full Post »

在 PHP 函式裡面,有直接 file_exists 可以使用,相當方便: <?php if(file_exists("files/appleboy.c")) {     echo "File found!"; } ?> 在 C 裡面該如何實做?有兩種方式如下: 1. 直接開檔 bool file_exists(const char * filename) {     if (FILE * file = fopen(filename, "r"))     {         fclose(file);         return true;     }     return [...]

Read Full Post »

[C/C++] 將字串轉成 16 進位

最近在碰嵌入式系統遇到一個還蠻常見的問題,我要將16進位的字串(例如 AAC2) test 轉成16進位的 unsigned int,讓我可以進行 & | not 一些二進位運算,底下是轉換程式,大家參考看看 int power(int x,int n) {     int i;     int num = 1;     for(i=1;i<=n;i++)         num*=x;     return num; } int transfer_string_to_hex(unsigned char *str_name) {     char string[]="0123456789ABCDEF";     int number[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};     int i [...]

Read Full Post »

串接函式 strcat strcat 此函式用來連接兩字串合併成單一字串,直接看底下範例: /* strcat example */ #include <stdio.h> #include <string.h> int main () {   char str[80];   strcpy (str,"these ");   strcat (str,"strings ");   strcat (str,"are ");   strcat (str,"concatenated.");   puts (str);   return 0; } output: these strings are concatenated. 看一下 strcat 原始碼: char * strcat(char * __restrict s, [...]

Read Full Post »

strstr 這次介紹 C 語言常用 string 函式:strstr,主要是針對兩個輸入參數做比對,Parameters 1 是輸入字串,Parameters 2 是找尋字串,strstr 會先將頭一次比對成功的 pointer 回傳,也就是如果要找尋 appleboyappleboy 字串中的 boy,函式會回傳第一次比對成功的 boy pointer,而並非回傳最後一個比對到的,底下是一個參考範例: /* strstr example */ #include <stdio.h> #include <string.h> int main () {   char str[] ="This is a simple string";   char * pch;   /* 找尋 simple 字串 */   pch = strstr (str,"simple");   /* [...]

Read Full Post »

Windows C# C# 比較字串 MSDN 比較字串 Request.Form Collection Request Query String / Form Parametrs ASP.NET QueryString Usage Using include files with ASP.NET html 將所有 <body/> 的內容包到一個 <div/> 中 apache Fixing mod_rewrite and .htaccess on GoDaddy Hosting javascript jQuery Week Calendar Javascript: reference the parent window from a popup How to get and set form element [...]

Read Full Post »

之前寫了一篇:『[C/C++] 計算二進位任意數含有多少個位元為1?』,裡面用 n &= (n – 1); 的方式來計算二進位數字總共會得到多少 bit,這次來紀錄利用 shift 方式也可以得到總共含有多少 bit 數目,函式如下: #include <stdio.h> #include <stdlib.h> int count_1_bit_count(unsigned int); int main(){     int count = 0, a;     a = 1023;     count = count_1_bit_count(a);     printf("%d有%d個位元為1\n\n", a, count);     system("pause");     return 0; } int count_1_bit_count(unsigned int [...]

Read Full Post »

今天來簡介 UNIX 內建的 strsep 函式,這在 Windows Dev-C++ 是沒有支援的,在寫 UNIX 分析字串常常需要利用到此函式,大家可以 man strsep 來看如何使用 strsep,假設我們要分析 URL Get 字串:user_command=appleboy&test=1&test2=2,就可以利用兩次 strsep 函式,將字串全部分離,取的個別的 name, value。strsep(stringp, delim) 第一個參數傳入需要分析的字串,第二個參數傳入 delim 符號,假設 stringp 為 NULL 字串,則函式會回傳 NULL,換句話說,strsep 會找到 stringp 字串第一個出現 delim 符號,並將其取代為 \0 符號,然後將 stringp 更新指向到 \0 符號的下一個字串,strsep() function 回傳原來的 stringp 指標。看上面文字敘述,好像不太瞭解,沒關係,底下是 UNIX strsep.c 的原始碼: /*  * Get next token from string [...]

Read Full Post »

繼上一篇:『[C/C++] 切割字串函數:strtok, Network mac address 分割』,內容寫到 Microsoft 用到 strpbrk 來找尋字串中特定符號,並且回傳該符號的位址,用法如下: #include "string.h" #include "stdlib.h" #include "stdio.h" int main () {   char str[] = "This is a sample string";   char key[] = "aeiou";   char * pch;   printf ("Vowels in ‘%s’: ",str);   pch = strpbrk (str, key);   while (pch != NULL) [...]

Read Full Post »

今天寫了 strtok 的範例:『如何分離網路 mac address』程式碼如下,大家一定會有疑問 strtok 第一次呼叫,第一參數輸入愈分離的字串,在 while 迴圈,則是輸入 NULL 呢?底下就來解析 strtok.c 的程式碼。 /* * * Author      : appleboy * Date        : 2010.04.01 * Filename    : strtok.c * */ #include "string.h" #include "stdlib.h" #include "stdio.h" int main() {   char str[]="00:22:33:4B:55:5A";   char *delim = ":";   char * [...]

Read Full Post »

Older Posts »