Conversions Library

mikroBasic PRO for PIC 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])

Returns

Nothing.

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

Requires

Nothing.

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

ShortToStr

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

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

WordToStr

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

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

IntToStr

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim input as integer 
    txt as string[6]
'...

input = -4220
IntToStr(input, txt)   ' txt is ' -4220'

LongintToStr

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim input as longint 
    txt as string[11]
'...

input = -12345678
LongintToStr(input, txt)    ' txt is '  -12345678'

LongWordToStr

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim input as longword 
    txt as string[10]
'...

input = 12345678
LongWordToStr(input, txt)    ' txt is '  12345678'

FloatToStr

Prototype

sub procedure FloatToStr(dim input as real, dim byref output as string[23])

Returns

Nothing.

Description

Converts a floating point number to a string.

Parameters :

  • input: floating point number to be converted
  • output: destination string

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

Nothing.

Example
dim ff1, ff2, ff3 as real
    txt as string[23]
  ...  
  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"

WordToStrWithZeros

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim t as word 
    txt as string[5]

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

IntToStrWithZeros

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim  t as integer 
    txt as string[6]

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

LongWordToStrWithZeros

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim t as longword 
    txt as string[10]

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

LongIntToStrWithZeros

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim t as longint 
    txt as string[11]

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

ByteToHex

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim t as byte
    txt as string[2] 

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

ShortToHex

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

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

WordToHex

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim t as word
    txt as string[4]

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

IntToHex

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim input as integer 
    txt as string[4]

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

LongWordToHex

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim input as longword 
    txt as string[8]

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

LongIntToHex

Prototype

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

Returns

Nothing.

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

Requires

Nothing.

Example
dim input as longint 
    txt as string[8]

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

StrToInt

Prototype

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

Returns

Integer variable.

Description

Converts a string to integer.

Requires

The string is assumed to be a correct representation of a number.

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

StrToWord

Prototype

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

Returns

Word variable.

Description

Converts a string to word.

Requires

input string with length of max 5 chars.
The string is assumed to be a correct representation of a number.

Example
dim ww as word

main:

  ww = StrToword('65432')

end. 

Bcd2Dec

Prototype

sub function Bcd2Dec(dim bcdnum as byte) as byte

Returns

Converted decimal value.

Description

Converts input BCD number to its appropriate decimal representation.

Parameters :

  • bcdnum: number to be converted

Requires

Nothing.

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

Dec2Bcd

Prototype

sub function Dec2Bcd(dim decnum as byte) as byte

Returns

Converted BCD value.

Description

Converts input number to its appropriate BCD representation.

Parameters :

  • decnum: number to be converted

Requires

Nothing.

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

Bcd2Dec16

Prototype

sub function Bcd2Dec16(dim bcdnum as word) as word

Returns

Converted decimal value.

Description

Converts 16-bit BCD numeral to its decimal equivalent.

Parameters :

  • bcdnum: 16-bit BCD numeral to be converted

Requires

Nothing.

Example
dim a, b as word
...
a = 0x1234        ' a equals 4660
b = Bcd2Dec16(a)  ' b equals 1234

Dec2Bcd16

Prototype

sub function Dec2Bcd16(dim decnum as word) as word

Returns

Converted BCD value.

Description

Converts decimal value to its BCD equivalent.

Parameters :

  • decnum decimal number to be converted

Requires

Nothing.

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