不使用库函数将long数组转换为char数组
如何在不使用库函数的情况下将不使用库函数将long数组转换为char数组,c,linux,C,Linux,如何在不使用库函数的情况下将long变量转换为char[]变量 工作示例(1)-线程安全,要求最小buffsize=40 static const char * xllitoa(long long int x, char *buff) { char *p = buff + 40; int sign = 0; *(p--) = 0; if (x < 0) sign = 1; else x = -x;
long
变量转换为char[]
变量 工作示例(1)-线程安全,要求最小buffsize=40
static const char *
xllitoa(long long int x, char *buff)
{
char *p = buff + 40;
int sign = 0;
*(p--) = 0;
if (x < 0) sign = 1;
else x = -x;
do { *(p--) = -(x % 10) + '0'; x /= 10; } while(x);
if (sign) *(p--) = '-';
return (const char *)(p+1);
}
静态常量字符*
xllitoa(长整型x,字符*浅黄色)
{
char*p=buff+40;
int符号=0;
*(p-)=0;
如果(x<0)符号=1;
否则x=-x;
do{*(p--)=-(x%10)+'0';x/=10;}while(x);
如果(符号)*(p--)='-';
返回(常量字符*)(p+1);
}
工作示例(2)-非线程安全
static const char *
xllitoa(long long int x)
{
static char buff[40];
char *p = buff + 40;
int sign = 0;
*(p--) = 0;
if (x < 0) sign = 1;
else x = -x;
do { *(p--) = -(x % 10) + '0'; x /= 10; } while(x);
if (sign) *(p--) = '-';
return (const char *)(p+1);
}
静态常量字符*
xllitoa(长整型x)
{
静态字符buff[40];
char*p=buff+40;
int符号=0;
*(p-)=0;
如果(x<0)符号=1;
否则x=-x;
do{*(p--)=-(x%10)+'0';x/=10;}while(x);
如果(符号)*(p--)='-';
返回(常量字符*)(p+1);
}
非常感谢评论家们。现在它也接受LLONG_MAX和LLONG_MIN。工作示例(1)-线程安全,要求最小buffsize=40
static const char *
xllitoa(long long int x, char *buff)
{
char *p = buff + 40;
int sign = 0;
*(p--) = 0;
if (x < 0) sign = 1;
else x = -x;
do { *(p--) = -(x % 10) + '0'; x /= 10; } while(x);
if (sign) *(p--) = '-';
return (const char *)(p+1);
}
静态常量字符*
xllitoa(长整型x,字符*浅黄色)
{
char*p=buff+40;
int符号=0;
*(p-)=0;
如果(x<0)符号=1;
否则x=-x;
do{*(p--)=-(x%10)+'0';x/=10;}while(x);
如果(符号)*(p--)='-';
返回(常量字符*)(p+1);
}
工作示例(2)-非线程安全
static const char *
xllitoa(long long int x)
{
static char buff[40];
char *p = buff + 40;
int sign = 0;
*(p--) = 0;
if (x < 0) sign = 1;
else x = -x;
do { *(p--) = -(x % 10) + '0'; x /= 10; } while(x);
if (sign) *(p--) = '-';
return (const char *)(p+1);
}
静态常量字符*
xllitoa(长整型x)
{
静态字符buff[40];
char*p=buff+40;
int符号=0;
*(p-)=0;
如果(x<0)符号=1;
否则x=-x;
do{*(p--)=-(x%10)+'0';x/=10;}while(x);
如果(符号)*(p--)='-';
返回(常量字符*)(p+1);
}
非常感谢评论家们。现在它也接受LLONG_MAX和LLONG_MIN。接受对所有值
LONG_MIN
到LONG_MAX
有效的答案后
这使用了一个helper函数来递归处理负值n
。通过使用负值,LONG\u MIN
没有问题
static char *ltostr_helper(long n, char *dest) {
if (n <= -10)
dest = ltostr_helper(n / 10, dest);
*dest++ = (char) ('0' - n % 10);
return dest; // return pointer to end
}
void ltostr(long n, char *dest) {
if (n < 0) {
*dest++ = '-';
} else {
n = -n;
}
*ltostr_helper(n, dest) = '\0';
}
int main(void) {
char buf[sizeof(long) * CHAR_BIT /3 + 3];// size buffer to our needs
ltostr(0, buf); printf("%s\n", buf);
ltostr(123, buf); printf("%s\n", buf);
ltostr(-123, buf); printf("%s\n", buf);
ltostr(LONG_MAX, buf); printf("%s\n", buf);
ltostr(LONG_MIN, buf); printf("%s\n", buf);
return 0;
}
Output
0
123
-123
9223372036854775807
-9223372036854775808
static char*ltostr\u助手(长n,char*dest){
如果(n之后)接受对所有值有效的答案LONG\u MIN
到LONG\u MAX
这使用了一个helper函数来递归处理负值n
。通过使用负值,LONG\u MIN
没有问题
static char *ltostr_helper(long n, char *dest) {
if (n <= -10)
dest = ltostr_helper(n / 10, dest);
*dest++ = (char) ('0' - n % 10);
return dest; // return pointer to end
}
void ltostr(long n, char *dest) {
if (n < 0) {
*dest++ = '-';
} else {
n = -n;
}
*ltostr_helper(n, dest) = '\0';
}
int main(void) {
char buf[sizeof(long) * CHAR_BIT /3 + 3];// size buffer to our needs
ltostr(0, buf); printf("%s\n", buf);
ltostr(123, buf); printf("%s\n", buf);
ltostr(-123, buf); printf("%s\n", buf);
ltostr(LONG_MAX, buf); printf("%s\n", buf);
ltostr(LONG_MIN, buf); printf("%s\n", buf);
return 0;
}
Output
0
123
-123
9223372036854775807
-9223372036854775808
static char*ltostr\u助手(长n,char*dest){
如果(NC没有一个代码>字符串类型;你想要一个C字符串还是一个C++ <代码:STD::String ?@ Altman,代码> Simulf不是系统调用。你只需要分割和模运算,以及字符集的知识。你需要的唯一系统调用是函数。不是系统调用。我想问你的目标是哪个操作系统的系统调用。另外,你真的知道系统调用是什么吗?你的问题很模糊。你是在尝试a)将某个计算中的long
整数值转换为适合向用户显示的文本表示,还是b)使用char[]获取构成long
值的原始字节获取单独的字节,或者C)其他的东西…C没有一个“代码>字符串类型;你想要一个C字符串还是一个C++ <代码:STD::String ?@ Altman,代码> Simulf不是系统调用。你只需要分割和模运算,以及字符集的知识。你需要的唯一系统调用就是乐趣。action.sprintf()不是系统调用。我想问你的目标是哪个操作系统的系统调用。另外,你真的知道系统调用是什么吗?你的问题是不明确的。你是在试图a)将一些计算得到的long
整数值转换成适合向用户显示的文本表示形式,还是b)获取构成系统调用的原始字节在long
值处使用char[]
获取单个字节,或者c)完全其他的东西…+1因为这是一个解决方案,但您不使用系统调用…这个问题问得很糟糕吗?使用'0'
而不是48
。C99保证'1'
位于'0'
'0'
可能不同于48
(EBCDIC?)。如果long
在某些(未来?)系统上是128位,那么。也许应该使用sizeof(long-long)
的一些公式。注意,在C99中,您可以使用const char*xllitoa(long-long int x,char buff[static 32]){…}明确地记录“缓冲区必须是32个字符”的要求(如果模糊的话):const char xllitoa
以两种方式失败。1)它不终止缓冲区(但随后操作所述字符数组,而不是“字符串”Hmmmm。2)它不适用于LLONG\u MIN
@soerium C指定LLONG\u MIN+1,因为它是解决方案,但您不使用系统调用…问题问得很糟吗?使用'0'
而不是48
。C99保证'1'
位于'0'
'0'
可能与48
不同>(EBCDIC?)。如果long
在某些(未来的?)系统上是128位的,那么。也许应该使用sizeof(long-long)
的公式。注意,在C99中,您可以使用const char*xllitoa(long-long int x,char buff[static 32]){…}明确地记录“缓冲区必须是32个字符”的要求
xllitoa()
有两种方式失败。1)它不终止缓冲区(但随后操作所述字符数组而不是“字符串”Hmmmm。2)它不适用于LLONG_MIN
@soerium C为sizeof(long)指定LLONG_MIN+1
。负值技巧适用于任何允许的long
表示:.+1表示sizeof(long)