Conversions Library
The mikroC PRO for 8051 Conversions Library provides routines for numerals to strings and BCD/decimal conversions.
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
- 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); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| 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) |
ShortToStr
| Prototype |
void ShortToStr(short input, char *output); |
|---|---|
| Returns |
Nothing. |
| Description |
Converts input signed short 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 :
|
| 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) |
WordToStr
| Prototype |
void WordToStr(unsigned input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| 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) |
IntToStr
| Prototype |
void IntToStr(int input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| 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) |
LongToStr
| Prototype |
void LongToStr(long input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| 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) |
LongWordToStr
| Prototype |
void LongWordToStr(unsigned long input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| 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) |
FloatToStr
| Prototype |
unsigned char FloatToStr(float fnum, unsigned char *str); |
|---|---|
| Returns |
|
| Description |
Converts a floating point number to a string. Parameters :
The output string is left justified and null terminated after the last digit. Note :
Given floating point number will be truncated to 7 most significant digits before conversion.
|
| 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" |
WordToStrWithZeros
| Prototype |
void WordToStrWithZeros(unsigned int input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 remaining positions on the left (if any) are filled with zeros. Parameters :
|
| 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) |
IntToStrWithZeros
| Prototype |
void IntToStrWithZeros(int input, char *output); |
|---|---|
| Returns |
Nothing. |
| Description |
Converts input integer 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 remaining positions on the left (if any) are filled with zeros. Parameters :
|
| 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) |
LongWordToStrWithZeros
| Prototype |
void LongWordToStrWithZeros(unsigned long input, char *output); |
|---|---|
| Returns |
Nothing. |
| Description |
Converts input longword 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 zeros. Parameters :
|
| 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) |
LongIntToStrWithZeros
| Prototype |
void LongIntToStrWithZeros(long input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 zaeros. Parameters :
|
| 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) |
ByteToHex
| Prototype |
void ByteToHex(char input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| 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" |
ShortToHex
| Prototype |
void ShortToHex(unsigned short input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| Requires |
Destination string should be at least 3 characters in length. |
| Example |
short t = -100; char txt[3]; ... ShortToHex(t, txt); // txt is "9C" |
WordToHex
| Prototype |
void WordToHex(unsigned input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| Requires |
Destination string should be at least 5 characters in length. |
| Example |
unsigned t = 1111; char txt[5]; ... WordToHex(t, txt); // txt is "0457" |
IntToHex
| Prototype |
void IntToHex(int input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| Requires |
Destination string should be at least 5 characters in length. |
| Example |
int j = -32768; char txt[5]; ... IntToHex(j, txt); // txt is "8000" |
LongWordToHex
| Prototype |
void LongWordToHex(unsigned long input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| 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" |
LongIntToHex
| Prototype |
void LongIntToHex(long int input, char *output); |
|---|---|
| Returns |
Nothing. |
| 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 :
|
| 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" |
Dec2Bcd
| Prototype |
unsigned short Dec2Bcd(unsigned short decnum); |
|---|---|
| Returns |
Converted BCD value. |
| Description |
Converts input unsigned short integer number to its appropriate BCD representation. Parameters :
|
| Requires |
Nothing. |
| Example |
unsigned short a, b; ... a = 22; b = Dec2Bcd(a); // b equals 34 |
Bcd2Dec
| Prototype |
unsigned short Bcd2Dec(unsigned short decnum); |
|---|---|
| Returns |
Converted decimal value. |
| Description |
Converts 8-bit BCD numeral to its decimal equivalent. Parameters :
|
| Requires |
Nothing. |
| Example |
unsigned short a, b; ... a = 0x34; // a equals 0x34 b = Bcd2Dec(a); // b equals 22 |
Dec2Bcd16
| Prototype |
unsigned Dec2Bcd16(unsigned decnum); |
|---|---|
| Returns |
Converted BCD value. |
| Description |
Converts unsigned 16-bit decimal value to its BCD equivalent. Parameters :
|
| Requires |
Nothing. |
| Example |
unsigned a, b; ... a = 2345; b = Dec2Bcd16(a); // b equals 9029 |
Bcd2Dec16
| Prototype |
unsigned Bcd2Dec16(unsigned bcdnum); |
|---|---|
| Returns |
Converted decimal value. |
| Description |
Converts 16-bit BCD numeral to its decimal equivalent. Parameters :
|
| Requires |
Nothing. |
| Example |
unsigned a, b; ... a = 0x1234; // a equals 4660 b = Bcd2Dec16(a); // b equals 1234 |
Rtrim
| Prototype |
char *Rtrim(char *string); |
|---|---|
| Returns |
The function returns pointer to the array without trailing spaces. |
| Description |
Trims the trailing spaces from array given with * Parameters :
|
| Requires |
Nothing. |
| Example |
char *res;
res = Rtrim("mikroe "); // trims the trailing spaces and returns the address of the first non-space character
|
Ltrim
| Prototype |
char *Ltrim(char *string); |
|---|---|
| Returns |
The function returns pointer to the array without leading spaces. |
| Description |
Trims the leading spaces from array given with * Parameters :
|
| Requires |
Nothing. |
| Example |
char *res;
res = Ltrim(" mikroe"); // trims the leading spaces and returns the address of the first non-space character
|
What do you think about this topic ? Send us feedback!



Note :
Given floating point number will be truncated to 7 most significant digits before conversion.
