C语言实现进制转换函数的实例详解
发布时间:2020-12-24 18:40:34 所属栏目:经验 来源:网络整理
导读:副标题#e# C语言实现进制转换函数的实例详解 前言: 写一个二进制,八进制,十六进制转换为十进制的函数 要求: 函数有两个参数,参数(1)是要转换为十进制的进制数,参数(2)是标示参数(1)是什么进制(2,8,16标示二进制,八进制,十六进制)。 要有报错信息,
|
其中用到了text_to_cstring(arg1),类型转换的相关函数定义在 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] = ' ';
if (tunpacked != t)
pfree(tunpacked);
return result;
}
结果:
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,16.
以上就是进制转换的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! (编辑:南平站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |


