vampiredom
2nd January 2012, 11:02
I wrote this as an exercise/experiment to try to duplicate some functions from Perl and other scripting languages by using Gscript. I hope others find this useful also.
It allows you to convert integers to arbitrary bases (and vice versa) using "extended" hex notation as needed (up to Base-64). It also allows encoding/decoding of MIME Base64 and RFC 4648 Base32 strings.
You can also encode/decode Base16 (hex) 4-bit text strings.
Download here (http://3dvp.com/BaseConvert.zip)
############################################################################################
# BaseConvert.avsi
#
# 2012/01/03 -> Some code cleanup
# 2012/01/02 -> Added Base16
# -> Documentation corrections
# 2012/01/02 -> First Release
############################################################################################
# Requires Gscript.dll
############################################################################################
# -----------------
# String Functions
# -----------------
# Ord(string c)
# -> Returns the ASCII value of a character as an integer
#
# encode_base64(string s, string "eol")
# -> Encodes a string to MIME Base64
# -> By default, lines are limited to 76 chars. Set eol="" to disable this.
#
# decode_base64(string s)
# -> Decodes a MIME Base64 string
#
# encode_base32(string s)
# -> Encodes an RFC 4648 Base32 string
#
# decode_base32(string s)
# -> Decodes an RFC 4648 Base32 string
#
# encode_base16(string s)
# -> Encodes a hexadecimal string
#
# decode_base16(string s)
# -> Decodes a hexadecimal string
#
# -----------------
# Integer Functions
# -----------------
#
# DecimalToBase(int n, int b, int "MinLen")
# -> Converts a base-10 integer (n) to an arbitrary base (b) with optional padding (MinLen)
# -> The result is a numeric or alphanumeric string
# -> Example #1:Convert 255 to octal (377)
# DecimalToBase(255, 8)
# -> Example #2:Convert 65535 to hexadecimal, 8 digits (00FFFF)
# DecimalToBase(65535, 16, 8)
# -> Example #3:Convert 16 to binary(10000)
# DecimalToBase(16, 2)
#
# BaseToDecimal(string s, base b)
# -> Convert a base number string to its base-10 value, returns integer
# -> Example #1: convert binary 10000 to decimal (16)
# BaseToDecimal("10000", 2)
# -> Example #2:Convert hexadecimal FFFF to decimal 65535
# DecimalToBase("FFFF", 16)
# -> Example #3: convert octal 377 to decimal (255)
# BaseToDecimal("377", 8)
#
# Notes:
# ------
# DecimalToBase and BaseToDecimal use a [0-9,A-Z,a-z] alphabet
# encode_base32 and decode_base32 uses [A-Z,2-7]
# encode_base64 and decode_base64 uses [A-Z,a-z,0-9,+,/]
# Different chars can be specified for values 62 and 63 by setting the global variables
# __base_char62__ and __base_char63__ to any single-character strings (such as "-" and "_", for example)
It allows you to convert integers to arbitrary bases (and vice versa) using "extended" hex notation as needed (up to Base-64). It also allows encoding/decoding of MIME Base64 and RFC 4648 Base32 strings.
You can also encode/decode Base16 (hex) 4-bit text strings.
Download here (http://3dvp.com/BaseConvert.zip)
############################################################################################
# BaseConvert.avsi
#
# 2012/01/03 -> Some code cleanup
# 2012/01/02 -> Added Base16
# -> Documentation corrections
# 2012/01/02 -> First Release
############################################################################################
# Requires Gscript.dll
############################################################################################
# -----------------
# String Functions
# -----------------
# Ord(string c)
# -> Returns the ASCII value of a character as an integer
#
# encode_base64(string s, string "eol")
# -> Encodes a string to MIME Base64
# -> By default, lines are limited to 76 chars. Set eol="" to disable this.
#
# decode_base64(string s)
# -> Decodes a MIME Base64 string
#
# encode_base32(string s)
# -> Encodes an RFC 4648 Base32 string
#
# decode_base32(string s)
# -> Decodes an RFC 4648 Base32 string
#
# encode_base16(string s)
# -> Encodes a hexadecimal string
#
# decode_base16(string s)
# -> Decodes a hexadecimal string
#
# -----------------
# Integer Functions
# -----------------
#
# DecimalToBase(int n, int b, int "MinLen")
# -> Converts a base-10 integer (n) to an arbitrary base (b) with optional padding (MinLen)
# -> The result is a numeric or alphanumeric string
# -> Example #1:Convert 255 to octal (377)
# DecimalToBase(255, 8)
# -> Example #2:Convert 65535 to hexadecimal, 8 digits (00FFFF)
# DecimalToBase(65535, 16, 8)
# -> Example #3:Convert 16 to binary(10000)
# DecimalToBase(16, 2)
#
# BaseToDecimal(string s, base b)
# -> Convert a base number string to its base-10 value, returns integer
# -> Example #1: convert binary 10000 to decimal (16)
# BaseToDecimal("10000", 2)
# -> Example #2:Convert hexadecimal FFFF to decimal 65535
# DecimalToBase("FFFF", 16)
# -> Example #3: convert octal 377 to decimal (255)
# BaseToDecimal("377", 8)
#
# Notes:
# ------
# DecimalToBase and BaseToDecimal use a [0-9,A-Z,a-z] alphabet
# encode_base32 and decode_base32 uses [A-Z,2-7]
# encode_base64 and decode_base64 uses [A-Z,a-z,0-9,+,/]
# Different chars can be specified for values 62 and 63 by setting the global variables
# __base_char62__ and __base_char63__ to any single-character strings (such as "-" and "_", for example)