Conversions Library

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

Library Dependency Tree

Conversions Library Dependency Tree

Library Routines

You can get text representation of numerical value by passing it to one of the following routines:

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
  • input: byte to be converted
  • output: destination string
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 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
  • input: signed short number to be converted
  • output: destination string
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
  • input: word to be converted
  • output: destination string
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
  • input: signed integer number to be converted
  • output: destination string
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
  • input: signed long integer number to be converted
  • output: destination string
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
  • input: unsigned long integer number to be converted
  • output: destination string
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.

LongLongUnsignedToStr

Prototype

void LongLongUnsignedToStr(unsigned long long input, char *output);

Description

Converts input unsigned long long integer number to a string. The output string has fixed width of 21 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
  • input: unsigned long long integer number to be converted
  • output: destination string
Returns

Nothing.

Requires

Destination string should be at least 11 characters in length.

Example
unsigned long long jj = 3700000;
char txt[21];
...
LongLongUnsignedToStr(jj, txt);
// txt is "             3700000" (13 blanks here)
Notes

None.

LongLongSignedToStr

Prototype

void LongLongSignedToStr(long long input, char *output);

Description

Converts input long long integer number to a string. The output string has fixed width of 21 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
  • input: long long integer number to be converted
  • output: destination string
Returns

Nothing.

Requires

Destination string should be at least 11 characters in length.

Example
long long jj = -3700000;
char txt[21];
...
LongLongSignedToStr(jj, txt);
// txt is "             -3700000" (12 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
  • fnum: floating point number to be converted
  • str: destination string
Returns
  • 3 if input number is NaN
  • 2 if input number is -INF
  • 1 if input number is +INF
  • 0 if conversion was successful
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.

LongDoubleToStr

Prototype

unsigned char LongDoubleToStr(long double dnum, unsigned char *str);

Description

Converts a long double floating point number to a string.

The output string is left justified and null terminated after the last digit.

Parameters
  • fnum: long double floating point number to be converted
  • str: destination string
Returns
  • 3 if input number is NaN
  • 2 if input number is -INF
  • 1 if input number is +INF
  • 0 if conversion was successful
Requires

Destination string should be at least 23 characters in length.

Example
long double ff1 = -374.2;
long double ff2 = 123.456789;
long double ff3 = 0.000001234;
char txt[23];
...
LongDoubleToStr(ff1, txt);  // txt is "-374.2"
LongDoubleToStr(ff2, txt);  // txt is "123.4567"
LongDoubleToStr(ff3, txt);  // txt is "1.234e-6"
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).
The output string is right justified and remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: unsigned integer to be converted
  • output: destination string
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).
The output string is right justified and remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: integer number to be converted
  • output: destination string
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).
The output string is right justified and the remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: unsigned long number to be converted
  • output: destination string
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).
The output string is right justified and the remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: signed long number to be converted
  • output: destination string
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.

LongLongUnsignedToStrWithZeros

Prototype

void LongLongUnsignedToStrWithZeros(unsigned long long input, char *output);

Description

Converts input unsigned long long integer number to a string. The output string has fixed width of 21 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
  • input: unsigned long long number to be converted
  • output: destination string
Returns

Nothing.

Requires

Destination string should be at least 12 characters in length.

Example
unsigned long long j = 1234567890;
char txt[21];
...
LongLongUnsignedToStrWithZeros(j, txt);  // txt is "00000000001234567890" (ten zeros here)
Notes

None.

LongLongSignedToStrWithZeros

Prototype

void LongLongSignedToStrWithZeros(long long input, char *output);

Description

Converts input long long integer number to a string. The output string has fixed width of 21 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
  • input: long long number to be converted
  • output: destination string
Returns

Nothing.

Requires

Destination string should be at least 12 characters in length.

Example
unsigned long long j = -1234567890;
char txt[21];
...
LongLongUnsignedToStrWithZeros(j, txt);  // txt is "-0000000001234567890" (nine zeros 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
  • input: byte to be converted
  • output: destination string
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
  • input: signed short number to be converted
  • output: destination string
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
  • input: unsigned integer to be converted
  • output: destination string
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
  • input: signed integer number to be converted
  • output: destination string
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
  • input: unsigned long integer number to be converted
  • output: destination string
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
  • input: signed long integer number to be converted
  • output: destination string
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.

LongLongUnsignedToHex

Prototype

void LongLongUnsignedToHex(unsigned long long input, char *output);

Description

Converts input number to a string containing the number's hexadecimal representation. The output string has fixed width of 17 characters including null character at the end (string termination).

Parameters
  • input: unsigned long long integer number to be converted
  • output: destination string
Returns

Nothing.

Requires

Destination string should be at least 17 characters in length.

Example
unsigned long long jj = 4294967295;
char txt[17];
...
LongLongUnsignedToHex(jj, txt);  // txt is "00000000FFFFFFFF"
Notes

None.

LongLongSignedToHex

Prototype

void LongLongSignedToHex(long long input, char *output);

Description

Converts input number to a string containing the number's hexadecimal representation. The output string has fixed width of 17 characters including null character at the end (string termination).

Parameters
  • input: long long integer number to be converted
  • output: destination string
Returns

Nothing.

Requires

Destination string should be at least 17 characters in length.

Example
long long jj = -4294967295;
char txt[17];
...
LongLongSignedToHex(jj, txt);  // txt is "-0000000FFFFFFFF"
Notes

None.

Dec2Bcd

Prototype

unsigned short Dec2Bcd(unsigned short decnum);

Description

Converts input unsigned short integer number to its appropriate BCD representation.

Parameters
  • decnum: unsigned short integer number to be converted
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
  • bcdnum: 8-bit BCD numeral to be converted
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
  • decnum unsigned 16-bit decimal number to be converted
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
  • bcdnum: 16-bit BCD numeral to be converted
Returns

Converted decimal value.

Requires

Nothing.

Example
unsigned a, b;
...
a = 0x1234;          // a equals 4660
b = Bcd2Dec16(a);    // b equals 1234
Notes

None.

Rtrim

Prototype

char *Rtrim(char *string);

Description

Trims the trailing spaces from array given with *string

Parameters
  • string: array to be trimmed.
Returns

The function returns the address of the first non-space character.

Requires

Nothing.

Example
char *res; 

res = Rtrim("  mikroe");  // trims the trailing spaces and returns the address of the first non-space character
Notes

None.

Ltrim

Prototype

char *Ltrim(char *string);

Description 66

Trims the leading spaces from array given with *string

Parameters
  • string: array to be trimmed.
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.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code