Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

RtlToFromString - Number Bases

$
0
0
For the most part we're already set for converting to/from number bases in VB6. But sometimes people want something more.

Here is a little bit more based upon two API calls:

  • RtlInt64ToUnicodeString()
  • RtlUnicodeStringToInteger()


Bases:

  • Binary (2)
  • Octal (8)
  • Decimal (10)
  • Hex (16)


This wrapper code supports a number of numeric data types.

FromString():

  • Byte
  • Integer
  • Long
  • Single


ToString():

  • Byte
  • Integer
  • Long
  • Single
  • Double
  • Date
  • Currency


ToString() will optionally pad with 0's on the left to fill out the full precision of the input numeric data type (32 bits for Long, etc.). By default leading 0's are suppressed.

FromString() can be told what base to convert explicitly, or the input text can use a leading prefix (like "0x" for Hex) to indicate the base. An optional + or - sign can also be used in the input text.


Tests:

Code:

    Dim B As Byte

    Report "------------------ FromString ----------------------"
    Report """-123""", "In Default", FromString("-123")
    Report """0b1111""", "In Default", FromString("0b1111", vbByte)
    Report """  +1111""", "In Binary", FromString("  +1111", vbByte, fmtBinary)
    Report """-b""", "In Hex", FromString("-b", vbInteger, fmtHex)
    Report "------------------- ToString -----------------------"
    B = 254
    Report "From Byte:"
    Report B, "Out Default", ToString(B)
    Report B, "Out Dec", ToString(B, fmtDecimal)
    Report B, "Out Hex", ToString(B, fmtHex)
    Report B, "Out Binary", ToString(B, fmtBinary)
    Report "From Integer:"
    Report 32767, "Out Dec", ToString(32767, fmtDecimal)
    Report 32767, "Out Hex", ToString(32767, fmtHex)
    Report 32767, "Out Binary", ToString(32767, fmtBinary)
    Report "From Long:"
    Report 32767&, "Out Dec", ToString(32767&, fmtDecimal)
    Report 32767&, "Out Oct pad", ToString(32767&, fmtOctal, True)
    Report 32767&, "Out Hex pad", ToString(32767&, fmtHex, True)
    Report 32767&, "Out Binary pad", ToString(32767&, fmtBinary, True)
    Report -65535, "Out Dec", ToString(-65535, fmtDecimal)
    Report -65535, "Out Hex", ToString(-65535, fmtHex)
    Report -65535, "Out Binary", ToString(-65535, fmtBinary)
    Report "From Double:"
    Report 1.123, "Out Dec", ToString(1.123, fmtDecimal)
    Report 1.123, "Out Hex", ToString(1.123, fmtHex)
    Report 1.123, "Out Binary", ToString(1.123, fmtBinary)
    Report "From Currency:"
    Report 0.0254@, "Out Dec", ToString(0.0254@, fmtDecimal)
    Report 0.0254@, "Out Hex", ToString(0.0254@, fmtHex)
    Report 0.0254@, "Out Binary pad", ToString(0.0254@, fmtBinary, True)

Results:

Code:

------------------ FromString ----------------------
"-123"        In Default    -123
"0b1111"      In Default    15
"  +1111"      In Binary      15
"-b"          In Hex        -11
------------------- ToString -----------------------
From Byte:
254            Out Default    254
254            Out Dec        254
254            Out Hex        FE
254            Out Binary    11111110
From Integer:
32767          Out Dec        32767
32767          Out Hex        7FFF
32767          Out Binary    111111111111111
From Long:
32767          Out Dec        32767
32767          Out Oct pad    00000077777
32767          Out Hex pad    00007FFF
32767          Out Binary pad 00000000000000000111111111111111
-65535        Out Dec        4294901761
-65535        Out Hex        FFFF0001
-65535        Out Binary    11111111111111110000000000000001
From Double:
1.123          Out Dec        4607736361554183979
1.123          Out Hex        3FF1F7CED916872B
1.123          Out Binary    11111111110001111101111100111011011001000101101000011100101011
From Currency:
0.0254        Out Dec        254
0.0254        Out Hex        FE
0.0254        Out Binary pad 0000000000000000000000000000000000000000000000000000000011111110

You could easily write your own wrappers for these API calls if you don't like these.
Attached Files

Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>