This repository was archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmystrlib.h
More file actions
107 lines (82 loc) · 1.85 KB
/
mystrlib.h
File metadata and controls
107 lines (82 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**********************************************************************
BrewManiac
created by Vito Tai
Copyright (C) 2015 Vito Tai
This soft ware is provided as-is. Use at your own risks.
You are free to modify and distribute this software without removing
this statement.
BrewManiac by Vito Tai is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
***********************************************************************/
#ifndef MY_STRING_LIB
#define MY_STRING_LIB
byte sprintIntDigit(char *buff, int number,int base)
{
byte indx=0;
while (base > 1)
{
int digit= number /base;
buff[indx++]=('0' + digit);
number = number - digit * base;
base = base /10;
}
buff[indx++]=('0' + number);
return indx;
}
byte sprintInt(char *buff,int number)
{
byte sign=0;
if(number <0)
{
*buff='-';
buff = buff +1;
number = 0- number;
sign=1;
}
int base=1;
if(number >= 10)
{
int cb=number;
while(cb > 9)
{
cb = cb / 10;
base = base *10;
}
}
return sign+sprintIntDigit(buff,number,base);
}
byte sprintFloat(char *buff,float value,byte precision)
{
byte len=0;
if(value <0)
{
buff[0]='-';
buff = buff;
value = 0.0- value;
len=1;
}
int base=1;
float r=0.5;
for(byte p=0; p < precision; p++)
{
base = base * 10;
r= r * 0.1;
}
float number=value+ r;
if (number >= 1.0)
{
int integer=(int)number;
len +=sprintInt(buff+len,integer);
number -= (float)integer;
}
else
{
buff[len]='0';
len += 1;
}
if(precision == 0) return len;
buff[len++]='.';
number = number * base;
len+=sprintIntDigit(buff+len,(int)number,base/10);
return len;
}
#endif