文字列をQuoted-Pintableにエンコード・デコードします。
Quoted-Printableとは、電子メールの送受信の際に用いられるデータの変換方式のひとつで、本文に用いられる8ビット文字を7ビットのデータであるASCIIコードに変換する方式のことです。
- 構文
- UString = QuotedPrintable.encode( str )
- UString = QuotedPrintable.decode( str )
- 引数
- str
- エンコード・デコードしたい文字列
- 戻値
- エンコード・デコードした文字列
プログラム
//////////////////////////////////////////////////
// 【引数】
// str : エンコード・デコードしたい文字列
// 【戻値】
// エンコード・デコードした文字列
//////////////////////////////////////////////////
MODULE QuotedPrintable
FUNCTION encode(str)
DIM res = ""
FOR n = 1 TO LENGTH(str)
DIM s = COPY(str, n, 1)
DIM Matches = reExecute(JISToSJIS(decToHex(ExcelFunction.CODE("<#DBL>" + s + "<#DBL>"))), "([0-9A-F]{2})([0-9A-F]{2})")
FOR m = 0 TO Matches.Count - 1
res = res + "=" + Matches.Item(m).SubMatches(0) + "=" + Matches.Item(m).SubMatches(1)
NEXT
NEXT
RESULT = res
FEND
FUNCTION decode(str)
DIM res = ""
DIM Matches = reExecute(str, "=([0-9A-F]{2})+")
DIM n = 0
WHILE n <= Matches.Count - 1
SELECT TRUE
CASE hexToDec(Matches.Item(n).SubMatches(0)) >= $81
res = res + ExcelFunction.CHAR(hexToDec(SJISToJIS(Matches.Item(n).SubMatches(0) + Matches.Item(n+1).SubMatches(0))))
n = n + 2
DEFAULT
res = res + ExcelFunction.CHAR(hexToDec(Matches.Item(n).SubMatches(0)))
n = n + 1
SELEND
WEND
RESULT = res
FEND
ENDMODULE
//////////////////////////////////////////////////
// 【引数】
// bin : 2進数
// 【戻値】
// 10進数に変換した値
//////////////////////////////////////////////////
FUNCTION binToDec(bin)
dec = 0
FOR n = 1 TO LENGTH(bin)
dec = dec + COPY(bin, n, 1) * POWER(2, LENGTH(bin) - n)
NEXT
RESULT = dec
FEND
//////////////////////////////////////////////////
// 【引数】
// dec : 10進数
// 【戻値】
// 2進数に変換した値
//////////////////////////////////////////////////
FUNCTION decToBin(dec)
bin = ""
REPEAT
bin = (dec MOD 2) + bin
dec = INT(dec/2)
UNTIL dec = 0
RESULT = bin
FEND
//////////////////////////////////////////////////
// 【引数】
// dec : 10進数
// 【戻値】
// 16進数に変換した値
//////////////////////////////////////////////////
FUNCTION decToHex(dec)
RESULT = FORMAT(VAL(dec), 0, -1)
FEND
//////////////////////////////////////////////////
// 【引数】
//
// 【戻値】
//
//////////////////////////////////////////////////
MODULE ExcelFunction
CONST Excel = CREATEOLEOBJ("Excel.Application")
FUNCTION ASC(str)
RESULT = Excel.Evaluate("ASC(" + str + ")")
FEND
FUNCTION BAHTTEXT(num)
RESULT = Excel.Evaluate("BAHTTEXT(" + num + ")")
FEND
FUNCTION CHAR(num)
RESULT = Excel.Evaluate("CHAR(" + num + ")")
FEND
FUNCTION CLEAN(str)
RESULT = Excel.Evaluate("CLEAN(" + str + ")")
FEND
FUNCTION CODE(str)
RESULT = Excel.Evaluate("CODE(" + str + ")")
FEND
FUNCTION DATEVALUE(str)
RESULT = Excel.Evaluate("DATEVALUE(<#DBL>" + str + "<#DBL>)")
FEND
FUNCTION YEN(num, digit = "")
RESULT = Excel.Evaluate("YEN(" + num + ", " + digit + ")")
FEND
FUNCTION EXACT(str1, str2)
RESULT = Excel.Evaluate("EXACT(" + str1 + ", " + str2 + ")")
FEND
FUNCTION FIND(str)
RESULT = Excel.Evaluate("FIND(" + str + ")")
FEND
FUNCTION FINDB(str)
RESULT = Excel.Evaluate("FINDB(" + str + ")")
FEND
FUNCTION FIXED(str, digit = "", separator = "")
RESULT = Excel.Evaluate("FIXED(" + str + ", " + digit + ", " + separator + ")")
FEND
FUNCTION LEFT(str, num = "")
RESULT = Excel.Evaluate("LEFT(<#DBL>" + str + "<#DBL>, " + num + ")")
FEND
FUNCTION LEFTB(str, byte = "")
RESULT = Excel.Evaluate("LEFTB(" + str + ", " + byte + ")")
FEND
FUNCTION LEN(str)
RESULT = Excel.Evaluate("LEN(<#DBL>" + str + ")")
FEND
FUNCTION LENB(str)
RESULT = Excel.Evaluate("LENB(" + str + ")")
FEND
FUNCTION LOWER(str)
RESULT = Excel.Evaluate("LOWER(" + str + ")")
FEND
FUNCTION MID(str, start, num)
RESULT = Excel.Evaluate("MID(" + str + ", " + start + ", " + num + ")")
FEND
FUNCTION MIDB(str, start, num)
RESULT = Excel.Evaluate("MIDB(" + str + ", " + start + ", " + num + ")")
FEND
FUNCTION PROPER(str)
RESULT = Excel.Evaluate("PROPER(" + str + ")")
FEND
FUNCTION REPLACE(str, start, length, replace)
RESULT = Excel.Evaluate("REPLACE(" + str + ", " + start + ", " + length + ", " + replace + ")")
FEND
FUNCTION REPLACEB(str, start, byte, replace)
RESULT = Excel.Evaluate("REPLACEB(" + str + ", " + start + ", " + byte + ", " + replace + ")")
FEND
FUNCTION REPT(str, num)
RESULT = Excel.Evaluate("REPT(" + str + ", " + num + ")")
FEND
FUNCTION RIGHT(str, num = "")
RESULT = Excel.Evaluate("RIGHT(<#DBL>" + str + "<#DBL>, " + num + ")")
FEND
FUNCTION RIGHTB(str, byte = "")
RESULT = Excel.Evaluate("RIGHTB(" + str + ", " + byte + ")")
FEND
FUNCTION SEARCH(str, target, start = "")
RESULT = Excel.Evaluate("SEARCH(" + str + ", " + target + ", " + start + ")")
FEND
FUNCTION SEARCHB(str, target, start = "")
RESULT = Excel.Evaluate("SEARCHB(" + str + ", " + target + ", " + start + ")")
FEND
FUNCTION SUBSTITUTE(str1, str2, str3, target = "")
RESULT = Excel.Evaluate("SUBSTITUTE(" + str1 + ", " + str2 + ", " + str3 + ", " + target + ")")
FEND
FUNCTION T(num)
RESULT = Excel.Evaluate("T(" + num + ")")
FEND
FUNCTION TEXT(num, format)
RESULT = Excel.Evaluate("TEXT(" + num + ", <#DBL>" + format + "<#DBL>)")
FEND
FUNCTION TRIM(str)
RESULT = Excel.Evaluate("TRIM(" + str + ")")
FEND
FUNCTION UPPER(str)
RESULT = Excel.Evaluate("UPPER(" + str + ")")
FEND
FUNCTION VALUE(str)
RESULT = Excel.Evaluate("VALUE(" + str + ")")
FEND
ENDMODULE
//////////////////////////////////////////////////
// 【引数】
// hex : 16進数
// 【戻値】
// 2進数に変換した値
//////////////////////////////////////////////////
FUNCTION hexToBin(hex)
HASHTBL hb
hb["0"] = "0000"; hb["1"] = "0001";
hb["2"] = "0010"; hb["3"] = "0011";
hb["4"] = "0100"; hb["5"] = "0101";
hb["6"] = "0110"; hb["7"] = "0111";
hb["8"] = "1000"; hb["9"] = "1001";
hb["a"] = "1010"; hb["b"] = "1011";
hb["c"] = "1100"; hb["d"] = "1101";
hb["e"] = "1110"; hb["f"] = "1111";
bin = ""
FOR n = 1 TO LENGTH(hex)
bin = bin + hb[COPY(hex, n, 1)]
NEXT
RESULT = bin
FEND
//////////////////////////////////////////////////
// 【引数】
// hex : 16進数
// 【戻値】
// 10進数に変換した値
//////////////////////////////////////////////////
FUNCTION hexToDec(hex)
dec = 0
hex = STRCONV(hex, SC_LOWERCASE)
FOR n = 1 TO LENGTH(hex)
str = COPY(hex, n, 1)
IFB CHKNUM(str) THEN
num = str
ELSE
num = ASC(str) - 87
ENDIF
dec = dec + (num * POWER(16, LENGTH(hex) - n))
NEXT
RESULT = dec
FEND
//////////////////////////////////////////////////
// 【引数】
// expr : 評価する式
// truepart : 評価した式がTrueのときに返す値
// falsepart : 評価した式がFalseのときに返す値
// 【戻値】
// truepart : 評価した式がTrueのとき、falsepart : 評価した式がFalseのとき
//////////////////////////////////////////////////
FUNCTION IIF(expr, truepart, falsepart)
IFB EVAL(expr) THEN
RESULT = truepart
ELSE
RESULT = falsepart
ENDIF
FEND
//////////////////////////////////////////////////
// 【引数】
// JIS : 16進数
// 【戻値】
// シフトJIS(16進数)
//////////////////////////////////////////////////
FUNCTION JISToSJIS(JIS)
DIM arr[1]
DIM res[1]
arr[0] = hexToDec(COPY(JIS, 1, 2))
arr[1] = hexToDec(COPY(JIS, 3, 2))
// 上位8bit
arr[0] = arr[0] - $21
// 上位7bit
res[0] = binToDec(COPY(decToBin(arr[0]), 1, LENGTH(decToBin(arr[0])) - 1))
res[0] = decToHex(res[0] + IIF(res[0] <= $1E, $81, $C1))
// 下位8bit
SELECT COPY(hexToBin(arr[0]), LENGTH(hexToBin(arr[0])))
CASE "0"
res[1] = arr[1] + $1F
IFB res[1] >= $7F THEN
res[1] = decToHex(res[1] + 1)
ELSE
res[1] = decToHex(res[1])
ENDIF
CASE "1"
res[1] = decToHex(arr[1] + $7E)
SELEND
RESULT = JOIN(res, "")
FEND
//////////////////////////////////////////////////
// 【引数】
// str : 正規表現による検索の対象となる文字列
// Pattern : 正規表現で使用するパターンを設定
// IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse
// Global : 文字列全体を検索する場合はTrue、しない場合はFalse
// 【戻値】
// 正規表現で検索した結果をMatchesコレクションとして返します。
//////////////////////////////////////////////////
FUNCTION reExecute(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
DIM re = CREATEOLEOBJ("VBScript.RegExp")
re.Pattern = Pattern
re.IgnoreCase = IgnoreCase
re.Global = Global
RESULT = re.Execute(str)
FEND
//////////////////////////////////////////////////
// 【引数】
// SJIS : シフトJIS(16進数)
// 【戻値】
// JIS(16進数)
//////////////////////////////////////////////////
FUNCTION SJISToJIS(SJIS)
DIM arr[1]
DIM res[1]
arr[0] = hexToDec(COPY(SJIS, 1, 2))
arr[1] = hexToDec(COPY(SJIS, 3, 2))
IFB arr[0] <= $9F THEN
arr[0] = arr[0] - $71
ELSE
arr[0] = arr[0] - $B1
ENDIF
arr[0] = arr[0] * 2 + 1
IF arr[1] >= $7F THEN arr[1] = arr[1] - 1
IFB arr[1] >= $9E THEN
arr[1] = arr[1] - $7D
arr[0] = arr[0] + 1
ELSE
arr[1] = arr[1] - $1F
ENDIF
res[0] = COPY("0" + decToHex(arr[0]), LENGTH("0" + decToHex(arr[0])) - 1)
res[1] = COPY("0" + decToHex(arr[1]), LENGTH("0" + decToHex(arr[1])) - 1)
RESULT = JOIN(res, "")
FEND
関連記事
- Base64
- 文字列をBASE64にエンコード・デコードします。BASE64とは、バイナリデータを一定の規則に基づいてテキスト(文字)データに置き換える変換方式の一つで、64種類の英数字のみを用いてデータを表現する方式。 電子メールの添付ファイル(MIME)などでよく用いられます。
- Cipher
- 引数に指定した文字列をシーザー暗号・ROT13・ヴィジュネル暗号・XOR暗号の4種類に暗号化または復号します。
- WordFunction
- MORSE
- 引数に指定した文字列をモールス信号、またはモールス信号を文字列に変換します。