Conversions Library
mikroBasic PRO for PIC32 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
- LongIntToStr
- LongWordToStr
- Uint64ToStr
- Int64ToStr
- FloatToStr
- ExtendedToStr
- WordToStrWithZeros
- IntToStrWithZeros
- LongWordToStrWithZeros
- LongIntToStrWithZeros
- Uint64ToStrWithZeros
- Int64ToStrWithZeros
- ByteToHex
- ShortToHex
- WordToHex
- IntToHex
- LongWordToHex
- LongIntToHex
- Uint64ToHex
- Int64ToHex
- StrToInt
- StrToWord
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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim input as longint
txt as string[10]
...
input = 12345678
LongWordToStr(input, txt) ' txt is " 12345678"
|
| Notes |
None. |
Uint64ToStr
| Prototype |
sub procedure Uint64ToStr(dim input as uint64, dim byref output as string[20]) |
|---|---|
| Description |
Converts input uint64 number to a string. The output string is right justified and the remaining positions on the left (if any) are filled with blanks. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim input as uint64
txt as char[20]
input = 0x1122334455667788
Uint64ToStr(input, txt) ' txt is ' 1122334455667788'
|
| Notes |
None. |
Int64ToStr
| Prototype |
sub procedure Int64ToStr(dim input as int64, dim byref output as string[20]) |
|---|---|
| Description |
Converts input int64 number to a string. The output string is right justified and the remaining positions on the left (if any) are filled with blanks. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim input as int64
txt as char[20]
input = 0x1122334455667788
Int64ToStr(input, txt) ' txt is ' 1122334455667788'
|
| 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 |
|
| 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. |
ExtendedToStr
| Prototype |
sub procedure ExtendedToStr(dim input as extended, dim byref output as string[23]) |
|---|---|
| Description |
Converts a extended floating point number to a string. The output string is left justified and null terminated after the last digit. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim ff1, ff2, ff3 as extended
txt as char[23]
...
ff1 = -374.2
ff2 = 123.456789
ff3 = 0.000001234
ExtendedToStr(ff1, txt) ' txt is "-374.20001"
ExtendedToStr(ff2, txt) ' txt is "123.45678"
ExtendedToStr(ff3, txt) ' txt is "0.000000"
|
| 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 |
|
| 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 |
|
| 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 longword to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros. |
| Parameters |
|
| 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 |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim t as longint
txt as string[11]
t = -12345678
LongIntToStrWithZeros(t, txt) ' txt is "-0012345678"
|
| Notes |
None. |
Uint64ToStrWithZeros
| Prototype |
sub procedure Uint64ToStrWithZeros(dim input as uint64, dim byref output as string[20]) |
|---|---|
| Description |
Converts input uint64 to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim t as uint64
txt as char[20]
'...
t = 1234567890
Uint64ToStrWithZeros(t, txt) ' txt is '00000000001234567890'
|
| Notes |
None. |
Int64ToStrWithZeros
| Prototype |
sub procedure Int64ToStrWithZeros(dim input as int64, dim byref output as string[20]) |
|---|---|
| Description |
Converts input int64 to a string. The output string is right justified and the remaining positions on the left (if any) are filled with zeros. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim t as int64
txt as char[20]
'...
t = -1234567890
Int64ToStrWithZeros(t, txt) ' txt is '-0000000001234567890'
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim input as longint
txt as string[8]
input = -2147483648
LongIntToHex(input, txt) ' txt is "80000000"
|
| Notes |
None. |
Uint64ToHex
| Prototype |
sub procedure Uint64ToHex(dim input as uint64, dim byref output as string[16]) |
|---|---|
| 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 |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim input as uint64
txt as char[16]
'...
input = 4294967295
Uint64ToHex(input, txt) ' txt is '00000000FFFFFFFF'
|
| Notes |
None. |
Int64ToHex
| Prototype |
sub procedure Int64ToHex(dim input as int64, dim byref output as string[16]) |
|---|---|
| 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 |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
dim input as int64
txt as char[16]
'...
input = -4294967295
Int64ToHex(input, txt) ' txt is '-0000000FFFFFFFF'
|
| Notes |
None. |
StrToInt
| Prototype |
sub function StrToInt(dim byref input as string[6]) as integer |
|---|---|
| Description |
Converts a string to an integer. |
| Parameters |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| Returns |
Converted BCD value. |
| Requires |
Nothing. |
| Example |
dim a, b as word ... a = 2345 b = Dec2Bcd16(a) ' b equals 9029 |
| Notes |
None. |
What do you think about this topic ? Send us feedback!



