c-Long嵌入式系统中Long到char的转换函数 [英] c - Long Long to char conversion function in embedded system
本文介绍了c-Long嵌入式系统中Long到char的转换函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用嵌入式系统,因此我需要实现一种将long long转换为char的方法.
Im working with a embedded system and I need implement a way to convert long long to char.
问题是我无法在此系统中使用sprintf来执行此操作,因此我正在寻找实现此目的的替代方法/功能.
The problem is that I can not use sprintf in this system to do that, so im looking for alternative ways/functions to implement this.
欢迎使用LongLongToChar函数的实现建议.
Suggestions of implementations for LongLongToChar function are welcome.
推荐答案
Google"itoa".有很多变化.这是一个例子.
Google "itoa". There are many variations. Here's an example.
char* itoa(int val, int base){
static char buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}
具体来说,这是一个"lltoa".
Specifically, here's an 'lltoa'.
#include <stdio.h>
#include <limits.h>
char* lltoa(long long val, int base){
static char buf[64] = {0};
int i = 62;
int sign = (val < 0);
if(sign) val = -val;
if(val == 0) return "0";
for(; val && i ; --i, val /= base) {
buf[i] = "0123456789abcdef"[val % base];
}
if(sign) {
buf[i--] = '-';
}
return &buf[i+1];
}
int main() {
long long a = LLONG_MAX;
long long b = LLONG_MIN + 1;
long long c = 23;
printf("%ld\n", sizeof(a));
printf("max '%s'\n", lltoa(a, 10));
printf("min '%s'\n", lltoa(b, 10));
printf("-1 '%s'\n", lltoa((long long)-1, 10));
printf("23 '%s'\n", lltoa(c, 10));
}
这篇关于c-Long嵌入式系统中Long到char的转换函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文