/*
* mysprintf.c
*
* Created on: 2013-11-11
* Author: LOVT
*/
#include "mysprintf.h"
#include "stdio.h"
//数据长度不能超过 65535
u16 mystrlen(u8 u8str[])
{
u16 i = 0;
while(u8str[i] != '\0')
{
i ++;
if(i > 65535)
break;
}
return i;
}
u8 CheckIntLength(long lData)
{
u8 u8Len = 0;
while(lData != 0)
{
lData = lData / 10;
u8Len ++;
}
return u8Len;
}
u32 GetCalcuData(u8 u8len)
{
u32 u32Data;
if(u8len > 10)
return 0;
switch(u8len)
{
case 0:
u32Data = 1;
break;
case 1:
u32Data = 10;
break;
case 2:
u32Data = 100;
break;
case 3:
u32Data = 1000;
break;
case 4:
u32Data = 10000;
break;
case 5:
u32Data = 100000;
break;
case 6:
u32Data = 1000000;
break;
case 7:
u32Data = 10000000;
break;
case 8:
u32Data = 100000000;
break;
case 9:
u32Data = 1000000000;
break;
default:
break;
}
return u32Data;
}
/******************************************************************
// Summary: 整形转化为字符串 10进制 %d
// Parameter: [out/u8]u8Str[] 输出的字符串
// [in/long]lData 数据
// [in/u8]u8Width 宽度
// [in/u8]u8Zero 是否0补齐 1补齐 0 不补
//
// Return: [out/u8]返回缓存的实际使用大小
******************************************************************/
u8 IntD2Str(u8 u8Str[], long lData, u8 u8Width, u8 u8Zero)
{
u8 i = 0, j;
u8 u8Len, u8Len1 = 0;
u8 u8Flag = 0;
u8 u8Stt;
u8 u8LenMax = 0;
if(lData < 0)
{
u8Str[i] = '-';
i ++;
lData = 0 - lData;
u8Flag = 1;
}
u8Len = CheckIntLength(lData);
u8Len1 = u8Len;
if(u8Flag == 1)
{
u8Len1 = u8Len + 1;
}
if(u8Zero == 1)
{
if(u8Width > u8Len1)
{
u8Len1 = u8Width - u8Len1;
u8Stt = i;
for(i = 0; i < u8Len1; i ++)
{
u8Str[u8Stt + i] = '0';
}
for(j = 0; j < u8Len; j ++)
{
u8Str[u8Stt + u8Len1 + j] = lData / GetCalcuData(u8Len - 1 - j) + '0';
lData = lData % GetCalcuData(u8Len - 1 - j);
}
u8LenMax = u8Width;
}
else
{
for(j = 0; j < u8Len; j ++)
{
u8Str[i + j] = lData / GetCalcuData(u8Len - 1 - j) + '0';
lData = lData % GetCalcuData(u8Len - 1 - j);
}
u8LenMax = i + u8Len;
}
}
else
{
for(j = 0; j < u8Len; j ++)
{
u8Str[i + j] = lData / GetCalcuData(u8Len - 1 - j) + '0';
lData = lData % GetCalcuData(u8Len - 1 - j);
}
u8LenMax = i + u8Len;
if(u8Width > u8Len1)
{
u8Stt = i + u8Len;
u8Len1 = u8Width - u8Len1;
for(i = 0; i < u8Len1; i ++)
{
u8Str[u8Stt + i] = ' ';
}
u8LenMax = u8Width;
}
}
return u8LenMax;
}
/******************************************************************
// Summary: 浮点数转化为字符串 %f
// Parameter: [out/u8]u8Str[] 输出的字符串
// [in/float]fData 数据
// [in/u8]u8Width 宽度
// [in/u8]u8Dot 小数点个数
// [in/u8]u8Align 是否开始对齐
//
// Return: [out/u16]
******************************************************************/
u8 Float2Str(u8 u8Str[], float fData, u8 u8Width, u8 u8Dot, u8 u8Align)
{
u8 i = 0, j;
long lIntData, lDotData, lTemp;
u8 u8IntLen, u8DotLen;
u8 u8Len = 0;
u8 u8Stt;
u8 u8Flag = 0;
u8 u8LenMax = 0;
if(u8Align == 1) //左对齐
{
if(fData < 0)
{
u8Str[i] = '-';
i ++;
fData = 0 - fData;
u8Len = 1;
}
lIntData = (long)fData;
u8IntLen = CheckIntLength(lIntData);
for(j = 0; j < u8IntLen; j ++) //存整数部分
{
u8Str[i + j] = lIntData / GetCalcuData(u8IntLen - 1 - j) + '0';
lIntData = lIntData % GetCalcuData(u8IntLen - 1 - j);
}
u8Str[i + u8IntLen] = '.'; //存小数点
lDotData = (long)((fData - (long)fData) * GetCalcuData(u8Dot + 1)); //小数部分 4舍5入
lTemp = lDotData % 10;
if(lTemp >= 5)
{
lDotData = lDotData / 10 + 1;
}
else
{
lDotData = lDotData / 10;
}
for(j = 0; j < u8Dot; j ++) //存小数部分
{
u8Str[i + u8IntLen + 1 + j] = lDotData / GetCalcuData(u8Dot - 1 - j) + '0';
lDotData = lDotData % GetCalcuData(u8Dot - 1 - j);
}
u8Len += (1 + u8Dot + u8IntLen);
u8LenMax = u8Len;
if(u8Width > u8Len)
{
u8Len = u8Width - u8Len;
u8Stt = i + u8IntLen + 1 + u8Dot;
for(i = 0; i < u8Len; i ++)
{
u8Str[u8Stt + i] = ' ';
}
u8LenMax = u8Width;
}
}
else
{
if(fData < 0)
{
fData = 0 - fData;
u8Len = 1;
u8Flag = 1;
}
lIntData = (long)fData;
u8IntLen = CheckIntLength(lIntData);
u8Len += (1 + u8Dot + u8IntLen);
u8LenMax = u8Len;
if(u8Width > u8Len)
{
u8Len = u8Width - u8Len;
// u8Stt = i + u8Len + 1 + u8Dot;
for(i = 0; i < u8Len; i ++) //先补齐
{
u8Str[i] = ' ';
}
u8Stt = u8Len;
if(u8Flag == 1)
{
u8Str[u8Stt] = '-';
u8Stt ++;
}
u8LenMax = u8Width;
}
else
{
u8Stt = 0;
if(u8Flag == 1)
{
u8Str[u8Stt] = '-';
u8Stt ++;
}
}
for(j = 0; j < u8IntLen; j ++) //存整数部分
{
u8Str[u8Stt + j] = lIntData / GetCalcuData(u8IntLen - 1 - j) + '0';
lIntData = lIntData % GetCalcuData(u8IntLen - 1 - j);
}
u8Str[u8Stt + u8IntLen] = '.'; //存小数点
lDotData = (long)((fData - (long)fData) * GetCalcuData(u8Dot + 1)); //小数部分 4舍5入
lTemp = lDotData % 10;
if(lTemp >= 5)
{
lDotData = lDotData / 10 + 1;
}
else
{
lDotData = lDotData / 10;
}
for(j = 0; j < u8Dot; j ++) //存小数部分
{
u8Str[u8Stt + u8IntLen + 1 + j] = lDotData / GetCalcuData(u8Dot - 1 - j) + '0';
lDotData = lDotData % GetCalcuData(u8Dot - 1 - j);
}
}
return u8LenMax;
}
//%s
void Txt2Str(u8 u8Tgt[], u8 u8Src[])
{
while(*u8Src != '\0')
{
* u8Tgt ++ = *u8Src ++;
}
}
//一个参数 浮点数 只支持 %-x.xf %x.xf %.xf前后都可以加其他字符
u8 mysprintf_1_float(u8 *u8Src, const u8 *u8Tgt, float fData)
{
const u8 *u8Ptr = u8Tgt;
u8 i, u8SttAddr, u8SttAddr1;
u8 u8Len, u8Len1, u8LenMax;
u8 u8Temp, u8Width;
u8 u8Dot;
char Buf[5];
u8Len = mystrlen(u8Tgt);
if(u8Len > 32)
return 0;
//获取%
for(i = 0; i < u8Len; i ++)
{
if(u8Ptr[i] == '%')
{
u8SttAddr = i;
break;
}
else
{
u8Src[i] = u8Ptr[i];
}
}
u8LenMax = i;
for(i = 0; i < 5; i ++)
{
if(u8Ptr[u8SttAddr + 1 + i] == 'f')
{
u8SttAddr1 = u8SttAddr + 1 + i;
break;
}
else
{
Buf[i] = u8Ptr[u8SttAddr + 1 + i];
}
}
if(i == 5 || i == 0 || i == 1) //%和f之间不能超过5的宽度的数据 或不支持该格式
return 0;
if(i == 2) //%.xf
{
u8Dot = Buf[1] - '0';
u8Len1 = Float2Str(u8Src + u8SttAddr, fData, 1, u8Dot, 0);
u8LenMax += u8Len1;
}
else if(i == 3)
{
u8Width = Buf[0] - '0';
u8Dot = Buf[2] - '0';
u8Len1 = Float2Str(u8Src + u8SttAddr, fData, u8Width, u8Dot, 0);
u8LenMax += u8Len1;
}
else if(i == 4)
{
if(Buf[0] == '-')
{
u8Width = Buf[1] - '0';
u8Dot = Buf[3] - '0';
u8Len1 = Float2Str(u8Src + u8SttAddr, fData, u8Width, u8Dot, 1);
u8LenMax += u8Len1;
}
}
u8SttAddr1 ++;
while(u8Ptr[u8SttAddr1] != '\0')
{
u8Src[u8LenMax] = u8Ptr[u8SttAddr1];
u8LenMax ++;
u8SttAddr1 ++;
}
return 1;
}
//一个参数 整形 只支持 %d %xd %0xd %xxd 前后都可以加其他字符
u8 mysprintf_1_int(u8 *u8Src, const u8 *u8Tgt, long data)
{
const u8 *u8Ptr = u8Tgt;
u8 i, u8SttAddr, u8SttAddr1;
u8 u8Len, u8Len1, u8LenMax;
u8 u8Temp, u8Width;
u8 u8Zero = 0;
char Buf[5];
u8Len = mystrlen(u8Tgt);
if(u8Len > 32)
return 0;
//获取%
for(i = 0; i < u8Len; i ++)
{
if(u8Ptr[i] == '%')
{
u8SttAddr = i;
break;
}
else
{
u8Src[i] = u8Ptr[i];
}
}
u8LenMax = i;
//获取d
for(i = 0; i < 3; i ++)
{
if(u8Ptr[u8SttAddr + 1 + i] == 'd')
{
u8SttAddr1 = u8SttAddr + 1 + i;
break;
}
else
{
Buf[i] = u8Ptr[u8SttAddr + 1 + i];
}
}
if(i == 3) //%和d之间不能超过3的宽度的数据
return 0;
if(i == 0) //%d
{
u8Len1 = IntD2Str(u8Src + u8SttAddr, (long)data, 1, 0);
u8LenMax += u8Len1;
}
else if(i == 1) //%xd x 1-9
{
u8Len1 = IntD2Str(u8Src + u8SttAddr, (long)data, Buf[0] - '0', 0);
u8LenMax += u8Len1;
}
else if(i == 2)
{
if(Buf[0] == '0') //%0xd
{
u8Len1 = IntD2Str(u8Src + u8SttAddr, (long)data, Buf[1] - '0', 1);
u8LenMax += u8Len1;
}
else //%xxd
{
u8Width = (Buf[0] - '0')* 10 + (Buf[1] - '0');
u8Len1 = IntD2Str(u8Src + u8SttAddr, (long)data, u8Width, 0);
u8LenMax += u8Len1;
}
}
u8SttAddr1 ++;
while(u8Ptr[u8SttAddr1] != '\0')
{
u8Src[u8LenMax] = u8Ptr[u8SttAddr1];
u8LenMax ++;
u8SttAddr1 ++;
}
return 1;
}
int main(void)
{
u8 *u8Str = "2ewqeqwttttw";
u8 u8Buf[32];
float fData = 15.856;
// printf("%d\r\n", mystrlen(u8Str));
// printf("%d\r\n", CheckIntLength(-5416));
// Int2Str(u8Buf, -15835);
// printf("%d", (long)fData);
// Float2Str(u8Buf, -55.62487, 3);
memset(u8Buf, 0, 32);
// sprintf(u8Buf, "%8.3f", 55.62);
// Txt2Str(u8Buf, "abcdefg");
// IntD2Str(u8Buf, -1234, 8, 1);
// Float2Str(u8Buf, -123.456, 8, 1, 1);
// IntD2Str(u8Buf, 52132, 1, 0);
// mysprintf_1_int(u8Buf, "SDSAR%4d: qweqweqw", 23);
// Float2Str();
// sprintf(u8Buf, "SDSAR%03d", 23);
mysprintf_1_float(u8Buf, "aasad%-7.3fdasda", 52.36);
return 0;
}
上面的是 .c文件