C語(yǔ)言實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換函數(shù)的實(shí)例詳解
C語(yǔ)言實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換函數(shù)的實(shí)例詳解
前言:
寫(xiě)一個(gè)二進(jìn)制,八進(jìn)制,十六進(jìn)制轉(zhuǎn)換為十進(jìn)制的函數(shù)
要求:
- 函數(shù)有兩個(gè)參數(shù),參數(shù)(1)是要轉(zhuǎn)換為十進(jìn)制的進(jìn)制數(shù),參數(shù)(2)是標(biāo)示參數(shù)(1)是什么進(jìn)制(2,8,16標(biāo)示二進(jìn)制,八進(jìn)制,十六進(jìn)制)。
- 要有報(bào)錯(cuò)信息,比如參數(shù)是1012,但參數(shù)(2)是2,顯然是進(jìn)制數(shù)表示有錯(cuò)誤。
系統(tǒng)表 pg_proc 存儲(chǔ)關(guān)于函數(shù)的信息
內(nèi)部函數(shù)在編譯之前需要先定義在 pg_proc.h 中,src/include/catalog/pg_proc.h
CATALOG(pg_proc,1255) BKI_BOOTSTRAP BKI_ROWTYPE_OID(81) BKI_SCHEMA_MACRO
{
NameData proname; /* procedure name */ /* 函數(shù)名,sql 中 select 函數(shù)名(); */
Oid pronamespace; /* OID of namespace containing this proc */ /* 模式OID */
Oid proowner; /* procedure owner */ /* 用戶OID */
Oid prolang; /* OID of pg_language entry */
float4 procost; /* estimated execution cost */ /* 估計(jì)執(zhí)行成本 */
float4 prorows; /* estimated # of rows out (if proretset) */ /* 結(jié)果行估計(jì)數(shù) */
Oid provariadic; /* element type of variadic array, or 0 */
regproc protransform; /* transforms calls to it during planning */
bool proisagg; /* is it an aggregate? */ /* 是否為聚集函數(shù) */
bool proiswindow; /* is it a window function? */ /* 是否為窗口函數(shù) */
bool prosecdef; /* security definer */ /* 函數(shù)是一個(gè)安全定義器,也就是一個(gè)“setuid"函數(shù) */
bool proleakproof; /* is it a leak-proof function? */ /* 有無(wú)其他影響 */
bool proisstrict; /* strict with respect to NULLs? */ /* 遇到 NULL 值是否直接返回 NULL */
bool proretset; /* returns a set? */ /* 函數(shù)返回一個(gè)集合 */
char provolatile; /* see PROVOLATILE_ categories below */
int16 pronargs; /* number of arguments */ /* 參數(shù)個(gè)數(shù) */
int16 pronargdefaults; /* number of arguments with defaults */ /* 默認(rèn)參數(shù)的個(gè)數(shù) */
Oid prorettype; /* OID of result type */ /* 返回參數(shù)類(lèi)型OID */
/*
* variable-length fields start here, but we allow direct access to
* proargtypes
*/
oidvector proargtypes; /* parameter types (excludes OUT params) */ /* 存放函數(shù)參數(shù)類(lèi)型的數(shù)組 */
#ifdef CATALOG_VARLEN
Oid proallargtypes[1]; /* all param types (NULL if IN only) */
char proargmodes[1]; /* parameter modes (NULL if IN only) */
text proargnames[1]; /* parameter names (NULL if no names) */
pg_node_tree proargdefaults;/* list of expression trees for argument
* defaults (NULL if none) */
Oid protrftypes[1]; /* types for which to apply transforms */
text prosrc BKI_FORCE_NOT_NULL; /* procedure source text */ /* 函數(shù)處理器如何調(diào)用函數(shù),實(shí)現(xiàn)函數(shù)的函數(shù)名 */
text probin; /* secondary procedure info (can be NULL) */
text proconfig[1]; /* procedure-local GUC settings */
aclitem proacl[1]; /* access permissions */
#endif
} FormData_pg_proc;
在 proc.h 添加函數(shù)定義:
/* myfunc */
DATA(insert OID = 6663 ( x_to_dec PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 23 "25 23" _null_ _null_ _null_ _null_ _null_ x_to_dec _null_ _null_ _null_ ));
DESCR("x_to_dec.");
OID = 6663 /* OID 唯一,不能與其他定義 OID 重復(fù) */
x_to_dec /* sql 中 select x_to_dec(); */
2 0 23 "25 23" /* 傳遞兩個(gè)參數(shù); 默認(rèn) 0; 返回值類(lèi)型 OID = 23; 參數(shù)1類(lèi)型 OID = 25, 參數(shù)2類(lèi)型 OID = 23 */
x_to_dec /* 自定義函數(shù)名 */
這里的傳遞參數(shù)類(lèi)型和返回值類(lèi)型都用的了 OID
系統(tǒng)表 pg_type 存儲(chǔ)數(shù)據(jù)類(lèi)型的信息
postgres=# select oid,typname from pg_type where typname = 'text' or typname = 'int4'; oid | typname -----+--------- 23 | int4 25 | text (2 rows)
在 src/backend/utils/adt/myfuncs.c 實(shí)現(xiàn)自定義的函數(shù)
首先創(chuàng)建函數(shù)的整體部分:
Datum /* Datum 類(lèi)型是PG系統(tǒng)函數(shù)大量引用的類(lèi)型,其定義為:typedef uintptr_c Datum */
x_to_dec (PG_FUNCTION_ARGS) /* 函數(shù)名; 參數(shù) */
{
/* 獲取參數(shù) */
text *arg1 = PG_GETARG_TEXT_P(0);
int32 arg2 = PG_GETARG_INT32(1);
/** 實(shí)現(xiàn)功能 **/
/* 返回 */
PG_RETURN_INT32(sum);
}
這里的 PG_GETARG_XXXX() 和 PG_RETURN_XXXXX() 在 src/include/fmgr.h
知道了如何獲取參數(shù)以及返回返回值,接下來(lái)是具體的實(shí)現(xiàn):
Datum x_to_dec (PG_FUNCTION_ARGS)
{
int n = 0, i = 0, sum = 0, t = 0;
text *arg1 = PG_GETARG_TEXT_P(0);
int32 arg2 = PG_GETARG_INT32(1);
char *str = text_to_cstring(arg1);
n = strlen(str);
switch(arg2)
{
case 2:
for(i = n - 1; i >= 0; i--)
{
if((str[i] - '0') != 1 && (str[i] - '0') != 0)
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Please enter the correct binary number, such as '110011'.")));
}
sum += (str[i] - '0') * ((int)pow(2, n - 1 - i));
}
break;
case 8:
for(i = n - 1; i >= 0; i--)
{
if(!(str[i] >= '0' && str[i] <= '7'))
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Please enter the correct octal number, for example '34567'.")));
}
sum += (str[i] - '0') * ((int)pow(8, n - 1 - i));
}
break;
case 16:
for(i = n - 1; i >= 0; i--)
{
if( !(str[i] >= '0' && str[i] <= '9') )
{
if(str[i] >= 'A' && str[i] <= 'F')
{
// Uppercase to lowercase
str[i] = str[i] + 32;
} else if ( !(str[i] >= 'a' && str[i] <= 'f') ) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Please enter the correct hexadecimal number, for example '9f'.")));
}
}
if(str[i] <= '9')
{
t = str[i] - '0';
} else {
t = str[i] - 'a' + 10;
}
sum = sum * 16 + t;
}
break;
default:
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Out of range! The second parameter, please enter: 2, 4, 16.")));
}
PG_RETURN_INT32(sum);
}
其中用到了text_to_cstring(arg1) ,類(lèi)型轉(zhuǎn)換的相關(guān)函數(shù)定義在 src/backend/utils/adt/varlena.c
/*
* text_to_cstring
*
* Create a palloc'd, null-terminated C string from a text value.
*
* We support being passed a compressed or toasted text value.
* This is a bit bogus since such values shouldn't really be referred to as
* "text *", but it seems useful for robustness. If we didn't handle that
* case here, we'd need another routine that did, anyway.
*/
char *
text_to_cstring(const text *t)
{
/* must cast away the const, unfortunately */
text *tunpacked = pg_detoast_datum_packed((struct varlena *) t);
int len = VARSIZE_ANY_EXHDR(tunpacked);
char *result;
result = (char *) palloc(len + 1);
memcpy(result, VARDATA_ANY(tunpacked), len);
result[len] = '\0';
if (tunpacked != t)
pfree(tunpacked);
return result;
}
結(jié)果:
postgres=# select x_to_dec('111',2);
x_to_dec
----------
7
(1 row)
postgres=# select x_to_dec('aA',16);
x_to_dec
----------
170
(1 row)
postgres=# select x_to_dec('aA',1);
ERROR: Out of range! The second parameter, please enter: 2, 4, 16.
以上就是進(jìn)制轉(zhuǎn)換的實(shí)例,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- C語(yǔ)言16進(jìn)制與ASCII字符相互轉(zhuǎn)換
- C語(yǔ)言每日練習(xí)之進(jìn)制轉(zhuǎn)換
- C語(yǔ)言的進(jìn)制轉(zhuǎn)換及算法實(shí)現(xiàn)教程
- C語(yǔ)言用棧實(shí)現(xiàn)十進(jìn)制轉(zhuǎn)換為二進(jìn)制的方法示例
- C語(yǔ)言中網(wǎng)絡(luò)地址與二進(jìn)制數(shù)之間轉(zhuǎn)換的函數(shù)小結(jié)
- 編寫(xiě)C語(yǔ)言程序進(jìn)行進(jìn)制轉(zhuǎn)換的問(wèn)題實(shí)例
- C語(yǔ)言進(jìn)制轉(zhuǎn)換代碼分享
- C語(yǔ)言實(shí)現(xiàn)任意進(jìn)制轉(zhuǎn)換器
相關(guān)文章
C++實(shí)現(xiàn)Armadillo庫(kù)與OpenCV庫(kù)之間的數(shù)據(jù)格式轉(zhuǎn)換
在C++領(lǐng)域,數(shù)據(jù)格式轉(zhuǎn)換是常見(jiàn)且關(guān)鍵的任務(wù),尤其是在使用多個(gè)不同的庫(kù)進(jìn)行復(fù)雜的數(shù)據(jù)處理時(shí),Armadillo和OpenCV是兩個(gè)在科學(xué)計(jì)算和計(jì)算機(jī)視覺(jué)領(lǐng)域非常受歡迎的庫(kù),下面我們將詳細(xì)探討如何在C++中實(shí)現(xiàn)Armadillo庫(kù)與OpenCV庫(kù)之間的數(shù)據(jù)格式轉(zhuǎn)換,需要的朋友可以參考下2025-03-03
C語(yǔ)言 ffmpeg與sdl實(shí)現(xiàn)播放視頻同時(shí)同步時(shí)鐘詳解
使用ffmpeg和sdl實(shí)現(xiàn)播放視頻后,需要再實(shí)現(xiàn)時(shí)鐘同步才能正常的播放視頻,尤其是有音頻的情況,我們通常需要將視頻同步到音頻來(lái)確保音畫(huà)同步2022-09-09
C++ 動(dòng)態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)
這篇文章主要介紹了C++ 動(dòng)態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)的相關(guān)資料,需要的朋友可以參考下2015-06-06
C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)的問(wèn)題詳解
這篇文章主要給大家介紹了關(guān)于C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Qt開(kāi)發(fā)之QTreeWidget的使用教程詳解
這篇文章主要為大家詳細(xì)介紹了Qt中QTreeWidget使用的相關(guān)資料,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Qt有一定的幫助,感興趣的小伙伴可以了解一下2022-12-12
利用Matlab仿真實(shí)現(xiàn)圖像煙霧識(shí)別(k-means聚類(lèi)圖像分割+LBP+PCA+SVM)
本文主要介紹了利用k-means聚類(lèi)實(shí)現(xiàn)圖像分割+LBP算法進(jìn)行特征提取+PCA算法進(jìn)行特征降維+SVM算法訓(xùn)練二分類(lèi)模型從而實(shí)現(xiàn)煙霧識(shí)別。文中介紹很詳細(xì),感興趣的朋友可以了解一下2021-12-12
利用C語(yǔ)言實(shí)現(xiàn)經(jīng)典多級(jí)時(shí)間輪定時(shí)器
C語(yǔ)言是一門(mén)通用計(jì)算機(jī)編程語(yǔ)言,廣泛應(yīng)用于底層開(kāi)發(fā),這篇文章主要給大家介紹了關(guān)于利用C語(yǔ)言實(shí)現(xiàn)經(jīng)典多級(jí)時(shí)間輪定時(shí)器的相關(guān)資料,需要的朋友可以參考下2021-07-07

