VC中 十六進製制字串轉換為十進位制數字

2021-06-21 10:57:41 字數 4264 閱讀 4766

在程式中,我們有時需要將乙個十六進製制字串轉換為十進位制數字。比如:

char *ptr="0x11";

int n=0;

//我們想讓n等於0x11,即17

通常我們在c中,想將乙個字串轉換為一整形數字,通常會使用下面的方法:

view plaincopy to clipboardprint?

char *ptr="123"; 

int n=0; 

n=atoi(ptr); 

printf("%d\n",n); 

//輸出:123 

char *ptr="123"; 

int n=0;

n=atoi(ptr);

printf("%d\n",n);

//輸出:123

但是atoi庫函式只能將十進位制字串轉化為int整形,比如下面的例子:

view plaincopy to clipboardprint?

#include

#include //atoi標頭檔案 

int main(void)   

#include

#include //atoi標頭檔案

int main(void)

所以要用atoi函式將"0x11"轉化為十進位制整形17是不行的。如果用的話,會輸出下面的結果:

view plaincopy to clipboardprint?

int n; 

char *str = "0x11"; 

n = atoi(str); //返回值n等於0 (顯然不是我們想要的結果) 

int n;

char *str = "0x11";

n = atoi(str); //返回值n等於0 (顯然不是我們想要的結果)

那怎麼辦呢?這時有人會想,那好辦,我們自己寫個函式轉化下就行了,比如用下面的方法:

注意:我們用vc 6.0建了乙個win32控制台程式,為了方便,程式中使用了cstring型別變數,需要做一點修改。

(1)包含afx.h標頭檔案

(2)在project->settings->general->mircosoft foundation classes中,選擇use mfc in a shared dll

然後我們就可以在win32控制台下使用cstring變數了,否則會出現編譯錯誤。

view plaincopy to clipboardprint?

#include

#include

int changenum(cstring str,int length)  ; 

//根據十六進製制字串的長度,這裡注意陣列不要越界 

int 

num[16]=; 

int 

count=1; 

int 

result=0; 

strcpy(revstr,str); 

for 

(int i=length-1;i>=0;i--)   

return result;  } 

int main()   

#include

#include

int changenum(cstring str,int length)  ; 

//根據十六進製制字串的長度,這裡注意陣列不要越界

int 

num[16]=; 

int 

count=1; 

int 

result=0; 

strcpy(revstr,str); 

for 

(int i=length-1;i>=0;i--)   

return result;  }

int main()

是的,上面方法可以得到我們想要的值。還有更簡單的方法嗎?當然有!

方法1:

view plaincopy to clipboardprint?

#include

int main()   

#include

int main()

主要用到sscanf這個庫函式:

函式名: sscanf 功 

能: 執行從字串中的格式化輸入 用 

法: int sscanf(char *string, char *format[,argument,...]); //%x就是我們要格式化的型別,即輸出十六進製制

方法2:

view plaincopy to clipboardprint?

#include

#include //strtol標頭檔案 

int main()   

#include

#include //strtol標頭檔案

int main()

主要用到strtol這個庫函式,它的使用方法是:

函式名: strtol 功 

能: 將串轉換為長整數 用 

法: long strtol(char *str, char **endptr, int base);//base指明我們要轉換為幾進製數

程式例:

view plaincopy to clipboardprint?

#include

#include

int main(void)   

#include

#include

int main(void)

在vc 6.0的mfc程式中,我們有時要轉換控制代碼就可以用strtol這個函式,比如:

view plaincopy to clipboardprint?

handle handle = (handle)strtol(str,null, 16); 

handle handle = (handle)strtol(str,null, 16);

ps:控制代碼是什麼?

控制代碼就是用來區分各種記憶體物件的唯一識別符號號,是個32位整數。

有些是整個系統唯一(如視窗控制代碼),有些是當前程序或執行緒中唯一(如執行緒控制代碼,全域性的有另乙個識別符號)。

詳細的可分為許多種,都是以h開頭的。在vb中使用時全部都用long。

常見的有視窗控制代碼(hwnd),裝置描述表控制代碼(hdc),記憶體控制代碼(hmem),檔案控制代碼,程序控制代碼,執行緒控制代碼,筆的型別控制代碼(hpen),字型控制代碼(hfont),區域控制代碼(hrgn)等等。

在申請控制代碼時是要占用資源的,分三類system、user、gdi。而windows的資源是固定的,並不隨記憶體的擴大而擴大,所以使用完以後一定要釋放。

方法3:

在網上,還看到一位朋友提出一種方法,就是讀寫ini檔案(我認為這種方法效率太低,畢竟要讀寫檔案的),也將其摘錄如下:

view plaincopy to clipboardprint?

//儲存handle 

struct tag_struct 

; struct tag_struct struct; 

struct.hwnd = m_hwnd; 

//把包含控制代碼的結構體寫入ini檔案 

writeprivateprofilestruc

t("section","key",&struct,sizeof(struct),"c:\\1.ini"); 

//讀取handle 

struct tag_struct 

; struct tag_struct struct; 

//從包含控制代碼的結構體的ini檔案讀取handle 

getprivateprofilestruct("section","key",&struct,sizeof(struct),"c:\\1.ini"); 

//儲存handle

struct tag_struct

;struct tag_struct struct;

struct.hwnd = m_hwnd;

//把包含控制代碼的結構體寫入ini檔案

writeprivateprofilestruc

t("section","key",&struct,sizeof(struct),"c:\\1.ini");

//讀取handle

struct tag_struct

;struct tag_struct struct;

//從包含控制代碼的結構體的ini檔案讀取handle

getprivateprofilestruct("section","key",&struct,sizeof(struct),"c:\\1.ini");

以上是我目前所知道的可以將十六進製制的字串轉化為整形數值的方法,可能還有更合適的方法,

轉貼 VC中 十六進製制字串轉換為十進位制數字

在程式中,我們有時需要將乙個十六進製制字串轉換為十進位制數字。比如 char ptr 0x11 int n 0 我們想讓n等於0x11,即17 通常我們在c中,想將乙個字串轉換為一整形數字,通常會使用下面的方法 view plaincopy to clipboardprint?char ptr 12...

VC中 十六進製制字串轉換為十進位制數字

在程式中,我們有時需要將乙個十六進製制字串轉換為十進位制數字。比如 char ptr 0x11 int n 0 我們想讓n等於0x11,即17 通常我們在c中,想將乙個字串轉換為一整形數字,通常會使用下面的方法 view plaincopy to clipboardprint?char ptr 12...

字串轉換為十六進製制

一般的程式對字串操作,串列埠程式卻時常採用十六進製制串。我在本在的社群中看到一位大俠的乙個轉換演算法不錯,貼出來和大家貢獻。char converthexchar char ch int string2hex cstring str,cbytearray senddata,char tag 函式功能...