Conversions Library
The mikroC PRO for AVR Conversions Library provides routines for numerals to strings and BCD/decimal conversions.
Library Dependency Tree

Library Routines
You can get text representation of numerical value by passing it to one of the following routines:
- ByteToStr
- ShortToStr
- WordToStr
- IntToStr
- LongToStr
- LongWordToStr
- FloatToStr
- FloatToStr_FixLen
- ByteToStrWithZeros
- ShortToStrWithZeros
- WordToStrWithZeros
- IntToStrWithZeros
- LongWordToStrWithZeros
- LongIntToStrWithZeros
- ByteToHex
- ShortToHex
- WordToHex
- IntToHex
- LongWordToHex
- LongIntToHex
- Rtrim
- Ltrim
The following functions convert decimal values to BCD and vice versa:
ByteToStr
Prototype |
void ByteToStr(unsigned short input, char *output); |
---|---|
Description |
Converts input byte to a string. The output string has fixed width of 4 characters including null character at the end (string termination). The output string is right justified and remaining positions on the left (if any) are filled with blanks. |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 4 characters in length. |
Example |
unsigned short t = 24; char txt[4]; ... ByteToStr(t, txt); // txt is " 24" (one blank here) |
Notes |
None. |
ShortToStr
Prototype |
void ShortToStr(short input, char *output); |
---|---|
Description |
Converts input short (signed byte) number to a string. The output string has fixed width of 5 characters including null character at the end (string termination). The output string is right justified and remaining positions on the left (if any) are filled with blanks. |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 5 characters in length. |
Example |
short t = -24; char txt[5]; ... ShortToStr(t, txt); // txt is " -24" (one blank here) |
Notes |
None. |
WordToStr
Prototype |
void WordToStr(unsigned input, char *output); |
---|---|
Description |
Converts input word to a string. The output string has fixed width of 6 characters including null character at the end (string termination). The output string is right justified and the remaining positions on the left (if any) are filled with blanks. |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 6 characters in length. |
Example |
unsigned t = 437; char txt[6]; ... WordToStr(t, txt); // txt is " 437" (two blanks here) |
Notes |
None. |
IntToStr
Prototype |
void IntToStr(int input, char *output); |
---|---|
Description |
Converts input signed integer number to a string. The output string has fixed width of 7 characters including null character at the end (string termination). The output string is right justified and the remaining positions on the left (if any) are filled with blanks. |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 7 characters in length. |
Example |
int j = -4220; char txt[7]; ... IntToStr(j, txt); // txt is " -4220" (one blank here) |
Notes |
None. |
LongToStr
Prototype |
void LongToStr(long input, char *output); |
---|---|
Description |
Converts input signed long integer number to a string. The output string has fixed width of 12 characters including null character at the end (string termination). The output string is right justified and the remaining positions on the left (if any) are filled with blanks. |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 12 characters in length. |
Example |
long jj = -3700000; char txt[12]; ... LongToStr(jj, txt); // txt is " -3700000" (three blanks here) |
Notes |
None. |
LongWordToStr
Prototype |
void LongWordToStr(unsigned long input, char *output); |
---|---|
Description |
Converts input unsigned long integer number to a string. The output string has fixed width of 11 characters including null character at the end (string termination). The output string is right justified and the remaining positions on the left (if any) are filled with blanks. |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 11 characters in length. |
Example |
unsigned long jj = 3700000; char txt[11]; ... LongWordToStr(jj, txt); // txt is " 3700000" (three blanks here) |
Notes |
None. |
FloatToStr
Prototype |
unsigned char FloatToStr(float fnum, unsigned char *str); |
---|---|
Description |
Converts a floating point number to a string. The output string is left justified and null terminated after the last digit. |
Parameters |
|
Returns |
|
Requires |
Destination string should be at least 14 characters in length. |
Example |
float ff1 = -374.2; float ff2 = 123.456789; float ff3 = 0.000001234; char txt[15]; ... FloatToStr(ff1, txt); // txt is "-374.2" FloatToStr(ff2, txt); // txt is "123.4567" FloatToStr(ff3, txt); // txt is "1.234e-6" |
Notes |
Given floating point number will be truncated to 7 most significant digits before conversion. |
FloatToStr_FixLen
Prototype |
unsigned cha FloatToStr_FixLen(float fnum, unsigned char *str, char len); |
---|---|
Description |
Converts a floating point number to a fixed length string. The output string is left justified and null terminated after the fixed length. |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
float ff1 = -374.2536; char txt[15]; ... FloatToStr_FixLen(ff1, txt, 6); // txt is "-374.2" |
Notes |
Given floating point number will be truncated to 7 most significant digits before conversion. |
ByteToStrWithZeros
Prototype |
void ByteToStrWithZeros(unsigned char input, char *output); |
---|---|
Description |
Converts input byte to a string. The output string has fixed width of 3 characters including null character at the end (string termination).
|
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
unsigned char t = 37; char txt[3]; ... ByteToStrWithZeros(t, txt); // txt is '037' |
Notes |
None. |
ShortToStrWithZeros
Prototype |
void ShortToStrWithZeros(short input, char *output); |
---|---|
Description |
Converts input integer to a string. The output string has fixed width of 4 characters including null character at the end (string termination).
|
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
short t = -37; char txt[4]; ... ShortToStrWithZeros(t, txt); // txt is '-037' |
Notes |
None. |
WordToStrWithZeros
Prototype |
void WordToStrWithZeros(unsigned int input, char *output); |
---|---|
Description |
Converts input word to a string. The output string has fixed width of 6 characters including null character at the end (string termination).
|
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 6 characters in length. |
Example |
unsigned short t = 437; char txt[6]; ... WordToStrWithZeros(t, txt); // txt is "0437" (one zero here) |
Notes |
None. |
IntToStrWithZeros
Prototype |
void IntToStrWithZeros(int input, char *output); |
---|---|
Description |
Converts input integer to a string. The output string has fixed width of 7 characters including null character at the end (string termination).
|
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 7 characters in length. |
Example |
short t = -3276; char txt[7]; ... IntToStrWithZeros(t, txt); // txt is "-03276" (one zero here) |
Notes |
None. |
LongWordToStrWithZeros
Prototype |
void LongWordToStrWithZeros(unsigned long input, char *output); |
---|---|
Description |
Converts input longword to a string. The output string has fixed width of 11 characters including null character at the end (string termination).
|
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 11 characters in length. |
Example |
unsigned t = 12345678; char txt[11]; ... LongWordToStrWithZeros(t, txt); // txt is "0012345678" (two zeros) |
Notes |
None. |
LongIntToStrWithZeros
Prototype |
void LongIntToStrWithZeros(long input, char *output); |
---|---|
Description |
Converts input signed long integer number to a string. The output string has fixed width of 12 characters including null character at the end (string termination).
|
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 12 characters in length. |
Example |
int j = -12345678; char txt[12]; ... LongIntToStrWithZeros(j, txt); // txt is "-0012345678" (one zero here) |
Notes |
None. |
ByteToHex
Prototype |
void ByteToHex(char input, char *output); |
---|---|
Description |
Converts input number to a string containing the number's hexadecimal representation. The output string has fixed width of 3 characters including null character at the end (string termination). |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 3 characters in length. |
Example |
unsigned short t = 2; char txt[3]; ... ByteToHex(t, txt); // txt is "02" |
Notes |
None. |
ShortToHex
Prototype |
void ShortToHex(unsigned short input, char *output); |
---|---|
Description |
Converts input number to a string containing the number's hexadecimal representation. The output string has fixed width of 3 characters including null character at the end (string termination). |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 3 characters in length. |
Example |
short t = -100; char txt[3]; ... ShortToHex(t, txt); // txt is "9C" |
Notes |
None. |
WordToHex
Prototype |
void WordToHex(unsigned input, char *output); |
---|---|
Description |
Converts input number to a string containing the number's hexadecimal representation. The output string has fixed width of 5 characters including null character at the end (string termination). |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 5 characters in length. |
Example |
unsigned t = 1111; char txt[5]; ... WordToHex(t, txt); // txt is "0457" |
Notes |
None. |
IntToHex
Prototype |
void IntToHex(int input, char *output); |
---|---|
Description |
Converts input number to a string containing the number's hexadecimal representation. The output string has fixed width of 5 characters including null character at the end (string termination). |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 5 characters in length. |
Example |
int j = -32768; char txt[5]; ... IntToHex(j, txt); // txt is "8000" |
Notes |
None. |
LongWordToHex
Prototype |
void LongWordToHex(unsigned long input, char *output); |
---|---|
Description |
Converts input number to a string containing the number's hexadecimal representation. The output string has fixed width of 9 characters including null character at the end (string termination). |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 9 characters in length. |
Example |
unsigned long jj = 65535; char txt[9]; ... LongWordToHex(jj, txt); // txt is "0000FFFF" |
Notes |
None. |
LongIntToHex
Prototype |
void LongIntToHex(long int input, char *output); |
---|---|
Description |
Converts input number to a string containing the number's hexadecimal representation. The output string has fixed width of 9 characters including null character at the end (string termination). |
Parameters |
|
Returns |
Nothing. |
Requires |
Destination string should be at least 9 characters in length. |
Example |
long int jj = -2147483648; char txt[9]; ... LongIntToHex(jj, txt); // txt is "80000000" |
Notes |
None. |
Dec2Bcd
Prototype |
unsigned short Dec2Bcd(unsigned short decnum); |
---|---|
Description |
Converts input unsigned short integer number to its appropriate BCD representation. |
Parameters |
|
Returns |
Converted BCD value. |
Requires |
Nothing. |
Example |
unsigned short a, b; ... a = 22; b = Dec2Bcd(a); // b equals 34 |
Notes |
None. |
Bcd2Dec
Prototype |
unsigned short Bcd2Dec(unsigned short bcdnum); |
---|---|
Description |
Converts 8-bit BCD numeral to its decimal equivalent. |
Parameters |
|
Returns |
Converted decimal value. |
Requires |
Nothing. |
Example |
unsigned short a, b; ... a = 34; b = Bcd2Dec(22); // b equals 22 |
Notes |
None. |
Dec2Bcd16
Prototype |
unsigned Dec2Bcd16(unsigned decnum); |
---|---|
Description |
Converts unsigned 16-bit decimal value to its BCD equivalent. |
Parameters |
|
Returns |
Converted BCD value. |
Requires |
Nothing. |
Example |
unsigned a, b; ... a = 2345; b = Dec2Bcd16(a); // b equals 9029 |
Notes |
None. |
Bcd2Dec16
Prototype |
unsigned Bcd2Dec16(unsigned bcdnum); |
---|---|
Description |
Converts 16-bit BCD numeral to its decimal equivalent. |
Parameters |
|
Returns |
Converted decimal value. |
Requires |
Nothing. |
Example |
unsigned a, b; ... a = 0x1234; // a equals 4660 b = Bcd2Dec16(a); // b equals 1234 |
Notes |
None. |
Rtrim
char *Rtrim(char *string);
Trims the trailing spaces from array given with *string
string
: array to be trimmed.
The function returns the address of the first string character.
Nothing.
char *res; res = Rtrim("mikroe "); // trims the trailing spaces and returns the address of the first character
None.
Ltrim
Prototype |
char *Ltrim(char *string); |
---|---|
Description |
Trims the leading spaces from array given with * |
Parameters |
|
Returns |
The function returns the address of the first non-space character. |
Requires |
Nothing. |
Example |
char *res; res = Ltrim(" mikroe"); // trims the leading spaces and returns the address of the first non-space character |
Notes |
None. |
What do you think about this topic ? Send us feedback!