不使用sprintf,使用求余%,且代码少
#include
#include
// 4个二进制位转成一个16进制位的字符(3个二进制位转成一个8进制位的字符)(十进制0-9转成字符)
char FourBinaryToOneHexadecimalChar(unsigned char num)
{
switch (num)
{
case 0: return '0';
case 1: return '1';
case 2: return '2';
case 3: return '3';
case 4: return '4';
case 5: return '5';
case 6: return '6';
case 7: return '7';
case 8: return '8';
case 9: return '9';
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
default: return 'X';
}
}
// 字符串反转
void ReverseString(char *Source, int Begin, int End)
{
int i;
char Temp;
for (i = 0; i < ((End - Begin + 1) / 2); ++i)
{
Temp = Source[Begin + i];
Source[Begin + i] = Source[End - i];
Source[End - i] = Temp;
}
}
// 比较float类型大小
int CompareFloat(float A, float B)
{
if ((A < B)
&& (fabs(A - B) > 1e-6f))
{
return -1;
}
else if ((A > B)
&& (fabs(A - B) > 1e-6f))
{
return 1;
}
else
{
return 0;
}
}
// float转字符串(十进制)(Precision是小数精度)
void FloatToStringDecimal(float Float, char *Destination, unsigned int Precision)
{
if (CompareFloat(Float, 0) != 0)
{
// 将小数移到整数位
float Temp = Float * powf(10.0f, (float)Precision);
// 负数转成正数,否则不能使用迷之转换
if (CompareFloat(Temp, 0) == -1)
{
Temp = -Temp;
}
float IntInFloat = 0.0;
modff(Temp, &IntInFloat);
int i = 0;
unsigned char num;
for (i = 0; (i < (int)Precision) && (CompareFloat(IntInFloat, 0) != 0); ++i)
{
// float 转成 unsigned int
// 迷之转换(因为不知道内部如何实现,所以叫迷之转换)
num = (unsigned char)((unsigned int)IntInFloat % 10);
Destination[i] = FourBinaryToOneHexadecimalChar(num);
IntInFloat /= 10;
modff(IntInFloat, &IntInFloat);
}
if (Precision != 0)
{
Destination[i] = '.';
++i;
}
while (CompareFloat(IntInFloat, 0) != 0)
{
num = (unsigned char)((unsigned int)IntInFloat % 10);
Destination[i] = FourBinaryToOneHexadecimalChar(num);
IntInFloat /= 10;
modff(IntInFloat, &IntInFloat);
++i;
}
if (CompareFloat(Float, 0) == -1)
{
Destination[i] = '-';
++i;
}
ReverseString(Destination, 0, i - 1);
Destination[i] = '\0';
return;
}
else
{
Destination[0] = '0';
Destination[1] = '\0';
return;
}
}
然后是测试
// 测试代码
fprintf(FP_BaseZeroth, "测试对象:void FloatToStringDecimal(float Float, char *Destination, unsigned int Precision)\n");
fprintf(FP_BaseZeroth, "测试输入:0; 123; 123.456; -123; -123.456; 1.23456e10f;\n");
fprintf(FP_BaseZeroth, "预测输出:0; 123; 123.456; -123; -123.456; 有效数字部分确定\n");
fprintf(FP_BaseZeroth, "实际输出:");
float A[6] = { 0.0f, 123.0f, 123.456f, -123.0f, -123.456f, 1.23456e10f };
char Des[100];
FloatToStringDecimal(A[0], Des, 0);
fprintf(FP_BaseZeroth, "%s; ", Des);
FloatToStringDecimal(A[1], Des, 0);
fprintf(FP_BaseZeroth, "%s; ", Des);
FloatToStringDecimal(A[2], Des, 3);
fprintf(FP_BaseZeroth, "%s; ", Des);
FloatToStringDecimal(A[3], Des, 0);
fprintf(FP_BaseZeroth, "%s; ", Des);
FloatToStringDecimal(A[4], Des, 3);
fprintf(FP_BaseZeroth, "%s; ", Des);
FloatToStringDecimal(A[5], Des, 3);
fprintf(FP_BaseZeroth, "%s; ", Des);
fprintf(FP_BaseZeroth, "\n\n");
测试结果
1:小端存储
float 转成HEX字符串
float y=0.1111111119389534f;
char a[9];
unsigned char *p;
p=(unsigned char *)(&y);
sprintf(a,"%02X%02X%02X%02X",p[0],p[1],p[2],p[3]);
2:HEX字符串转float
char a[9]="398EE33D";
float y;
unsigned char *p;
p=(unsigned char *)(&y);
sscanf(a,"%2x%2x%2x%2x",p,p+1,p+2,p+3);
3:大端存储
float 转成HEX字符串
float y=0.1111111119389534f;
char a[9];
unsigned char *p;
p=(unsigned char *)(&y);
sprintf(a,"%02X%02X%02X%02X",p[3],p[2],p[1],p[0]);
4:HEX字符串转float
char a[9]="398EE33D";
float y;
unsigned char *p;
p=(unsigned char *)(&y);
sscanf(a,"%2x%2x%2x%2x",p+3,p+2,p+1,p);
#include
#include
int main()
{
char s[50];
float a=1.03;
sprintf(s,"%f",a);//如果用sprintf函数小数点后会有6位
for(int i=strlen(s)-1;i>=0;i--)
{
if(s[i]!='0')
{
s[i+1]='\0';//需要把后面的0去掉
break;
}
}
printf("%s\n",s);
return 0;
}
char char_[7]=(char *)V的。
float value[8],a=0; char value0_char[2],value1_char[10],value2_char[10],value3_char[10]……
sprintf(value0_char, "%-3f",value[0]); //"-"负号表示左对齐,3
sprintf(value1_char, "%-10f",value[1]); //"-"负号表示左对齐,10:占10个空
给个思路,用sprintf函数,但IAR没用过,不清楚可行不,下面示例
#include
int main()
{
char s[50];
float a=1.03;
sprintf(s,"%f",a);
printf("%s\n",s);
return 0;
}