Conversions Library

mikroBasic PRO for dsPIC30/33 and PIC24 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 sub functions convert decimal values to BCD and vice versa:

ByteToStr

Prototype

sub procedure ByteToStr(dim input as byte, dim byref output as string[3])

Description

Converts input byte to a string. 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

Nothing.

Example
dim t as byte
    txt as string[3]
...
t = 24
ByteToStr(t, txt)  ' txt is " 24" (one blank here)
Notes

None.

ShortToStr

Prototype

sub procedure ShortToStr(dim input as short, dim byref output as string[4])

Description

Converts input short (signed byte) number to a string. The output string is right justified and remaining positions on the left (if any) are filled with blanks.

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

Nothing.

Requires

Nothing.

Example
dim t as short
    txt as string[4]
...
t = -24
ShortToStr(t, txt)  ' txt is " -24" (one blank here)
Notes

None.

WordToStr

Prototype

sub procedure WordToStr(dim input as word, dim byref output as string[5])

Description

Converts input word to a string. 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

Nothing.

Example
dim t as word
    txt as string[5]
...
t = 437
WordToStr(t, txt)  ' txt is "  437" (two blanks here)
Notes

None.

WordToStrWithZeros

Prototype

sub procedure WordToStrWithZeros(dim input as word, dim byref output as string[5])

Description

Converts input word to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: word to be converted
  • output: destination string
Returns

Nothing.

Requires

Nothing.

Example
dim t as word
    txt as string[5]
...
t = 437
WordToStrWithZeros(t, txt)  ' txt is '00437'
Notes

None.

IntToStr

Prototype

sub procedure IntToStr(dim input as integer, dim byref output as string[6])

Description

Converts input integer number to a string. The output string is right justified and the remaining positions on the left (if any) are filled with blanks.

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

Nothing.

Requires

Nothing.

Example
dim input as integer 
    txt as string[6]
...
input = -4220
IntToStr(input, txt)   ' txt is " -4220"
Notes

None.

LongintToStr

Prototype

sub procedure LongintToStr(dim input as longint, dim byref output as string[11])

Description

Converts input longint number to a string. The output string is right justified and the remaining positions on the left (if any) are filled with blanks.

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

Nothing.

Requires

Nothing.

Example
dim input as longint 
    txt as string[11]
...
input = -12345678
LongintToStr(input, txt)    ' txt is "  -12345678"
Notes

None.

LongWordToStr

Prototype

sub procedure LongWordToStr(dim input as longword, dim byref output as string[10])

Description

Converts input double word number to a string. The output string is right justified and the remaining positions on the left (if any) are filled with blanks.

Parameters
  • input: double word number to be converted
  • output: destination string
Returns

Nothing.

Requires

Nothing.

Example
dim input as longint 
    txt as string[10]
...
input = 12345678
LongWordToStr(input, txt)    ' txt is "  12345678"
Notes

None.

FloatToStr

Prototype

sub procedure FloatToStr(dim fnum as float, dim byref str as string[20]) as byte

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

Nothing.

Requires

Nothing.

Example
dim ff1, ff2, ff3 as float
    txt as string[20]
  ...  
  ff1 = -374.2
  ff2 = 123.456789
  ff3 = 0.000001234

  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.

WordToStrWithZeros

Prototype

sub procedure WordToStrWithZeros(dim input as word, dim byref output as string[5])

Description

Converts input word to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: word to be converted
  • output: destination string
Returns

Nothing.

Requires

Nothing.

Example
dim t as word 
    txt as string[5]

t = 437
WordToStrWithZeros(t, txt)  ' txt is "00437"
Notes

None.

IntToStrWithZeros

Prototype

sub procedure IntToStrWithZeros(dim input as integer, dim byref output as string[6])

Description

Converts input integer to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: word to be converted
  • output: destination string
Returns

Nothing.

Requires

Nothing.

Example
dim  t as integer 
    txt as string[6]

t = -3276
IntToStrWithZeros(t, txt)  ' txt is "-03276"
Notes

None.

LongWordToStrWithZeros

Prototype

sub procedure LongWordToStrWithZeros(dim input as longword, dim byref output as string[10])

Description

Converts input dword to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: word to be converted
  • output: destination string
Returns

Nothing.

Requires

Nothing.

Example
dim t as longword 
    txt as string[10]

t = 12345678
LongWordToStrWithZeros(t, txt)  ' txt is "0012345678"
Notes

None.

LongIntToStrWithZeros

Prototype

sub procedure LongIntToStrWithZeros(dim input as longint, dim byref output as string[11])

Description

Converts input longint to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: word to be converted
  • output: destination string
Returns

Nothing.

Requires

Nothing.

Example
dim t as longint 
    txt as string[11]

t = -12345678
LongIntToStrWithZeros(t, txt)  ' txt is "-0012345678"
Notes

None.

ByteToHex

Prototype

dim procedure ByteToHex(dim input as byte, dim byref output as string[2])

Description

Converts input number to a string containing the number's hexadecimal representation. The output string is right justified and remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: byte to be converted
  • output: destination string
Returns

Nothing.

Requires

Nothing.

Example
dim t as byte
    txt as string[2] 

t = 2
ByteToHex(t, txt)  ' txt is "02"
Notes

None.

ShortToHex

Prototype

sub procedure ShortToHex(dim input as short, dim byref output as string[2])

Description

Converts input number to a string containing the number's hexadecimal representation. The output string is right justified and remaining positions on the left (if any) are filled with zeros.

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

Nothing.

Requires

Nothing.

Example
dim t as short
    txt as string[2]
  ...  
t = -100
ShortToHex(t, txt)  ' txt is "9C"
Notes

None.

WordToHex

Prototype

sub procedure WordToHex(dim input as word, dim byref output as string[4])

Description

Converts input number to a string containing the number's hexadecimal representation. The output string is right justified and remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: word to be converted
  • output: destination string
Returns

Nothing.

Requires

Nothing.

Example
dim t as word
    txt as string[4]

t = 1111
WordToHex(t, txt)  ' txt is "0457"
Notes

None.

IntToHex

Prototype

sub procedure IntToHex(dim input as integer, dim byref output as string[4])

Description

Converts input number to a string containing the number's hexadecimal representation. 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

Nothing.

Example
dim input as integer 
    txt as string[4]

input = -32768
IntToHex(input, txt)   ' txt is "8000"
Notes

None.

LongWordToHex

Prototype

sub procedure LongWordToHex(dim input as longword, dim byref output as string[8])

Description

Converts input number to a string containing the number's hexadecimal representation. The output string is right justified and remaining positions on the left (if any) are filled with zeros.

Parameters
  • input: double word number to be converted
  • output: destination string
Returns

Nothing.

Example
dim input as longword 
    txt as string[8]

input = 65535
LongWordToHex(input, txt)    ' txt is "0000FFFF"
Notes

None.

LongIntToHex

Prototype

sub procedure LongIntToHex(dim input as longint, dim byref output as string[8])

Description

Converts input number to a string containing the number's hexadecimal representation. The output string is right justified and remaining positions on the left (if any) are filled with zeros.

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

Nothing.

Requires

Nothing.

Example
dim input as longint 
    txt as string[8]

input = -2147483648
LongIntToHex(input, txt)    ' txt is "80000000"
Notes

None.

StrToInt

Prototype

sub function StrToInt(dim byref input as string[6]) as integer

Description

Converts a string to an integer.

Parameters
  • input: string to be converted
Returns

Integer variable.

Requires

Input string is assumed to be the correct representation of a number. The conversion will end with the first character which is not a decimal digit.

Example
dim ii as integer
    
main:
  ...
  ii = StrToInt("-1234")
end.
Notes

None.

StrToWord

Prototype

sub function StrToWord(dim byref input as string[5]) as word

Description

Converts a string to word.

Parameters
  • input: string to be converted
Returns

Word variable.

Requires

Input string is assumed to be the correct representation of a number. The conversion will end with the first character which is not a decimal digit.

Example
dim ww as word

main:
  ...
  ww = StrToword("65432")
end.
Notes

None.

Bcd2Dec

Prototype

sub function Bcd2Dec(dim bcdnum as byte) as byte

Description

Converts input BCD number to its appropriate decimal representation.

Parameters
  • bcdnum: number to be converted
Returns

Converted decimal value.

Requires

Nothing.

Example
dim a, b as byte
...
a = 22
b = Bcd2Dec(a) ' b equals 34
Notes

None.

Dec2Bcd

Prototype

sub function Dec2Bcd(dim decnum as byte) as byte

Description

Converts input number to its appropriate BCD representation.

Parameters
  • decnum: number to be converted
Returns

Converted BCD value.

Requires

Nothing.

Example
dim a, b as byte
...
a = 22
b = Dec2Bcd(a) ' b equals 34
Notes

None.

Bcd2Dec16

Prototype

sub function Bcd2Dec16(dim bcdnum as word) as word

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
dim a, b as word
...
a = 0x1234        ' a equals 4660
b = Bcd2Dec16(a)  ' b equals 1234
Notes

None.

Dec2Bcd16

Prototype

sub function Dec2Bcd16(dim decnum as word) as word

Description

Converts decimal value to its BCD equivalent.

Parameters
  • decnum decimal number to be converted
Returns

Converted BCD value.

Requires

Nothing.

Example
dim a, b as word
...
a = 2345
b = Dec2Bcd16(a)  ' b equals 9029
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