本ページには広告が含まれています。
ASC関数は、文字を文字コードに変換する関数です。引数に指定された文字に対応するUnicodeもしくはASCIIの値を返します。文字コードを文字に変換するには、CHR関数を利用します。
Unicodeの対応表はUnicode一覧表 - Wikipedia、シフトJISは文字コード表 シフトJIS(Shift_JIS)を参考にしてください。
- 構文
- LongWord = ASC( 引数 )
- 引数
- 引数 (String)必須
- 文字
- 戻り値
引数(文字)から文字コードを返します。
文字コードから文字を得るにはCHR関数を使います。
ASCII文字コード表
Unicodeの1から128まではASCIIコードでUnicodeと互換性があります。
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | NULL | SOH | STX | ETX | EOT | ENG | ACK | BEL | BS | HT | LF | VT | FF | CR | SO | SI |
10 | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN | EM | SUB | ESC | FS | GS | RS | US |
20 | SP | ! | " | # | $ | % | & | ' | ( | ) | * | + | , | - | . | / |
30 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | : | ; | = | > | ? | |
40 | @ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O |
50 | P | Q | R | S | T | U | V | W | X | Y | Z | [ | \ | ] | ^ | _ |
60 | ` | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o |
70 | p | q | r | s | t | u | v | w | x | y | z | { | | | } | ~ | DEL |
Unicode一覧
Unicode表の中からよく使うものを記載しておきます。YYY0行X列の文字コードはU+YYYXとなります。例えば文字あは3040行2列にあるので、文字コードはU+3042となります。
UWSCでは以下のように記述すると、12354と出力されます。
PRINT ASC("あ")
- 結果
12354
16進数表記にする場合はdecToHex関数 (自作関数)を使います。また4桁にゼロ埋めするためstrPad関数 (自作関数)を使っています。以下はU+3042と出力されます。
PRINT "U+" + strPad(decToHex(ASC("あ")), 4, "0", LEFT)
//////////////////////////////////////////////////
// 【引数】
// bin : 2進数
// 【戻り値】
// 16進数に変換した値
//////////////////////////////////////////////////
FUNCTION binToHex(bin)
HASHTBL bh
bh["0000"] = "0"; bh["0001"] = "1"; bh["0010"] = "2"; bh["0011"] = "3";
bh["0100"] = "4"; bh["0101"] = "5"; bh["0110"] = "6"; bh["0111"] = "7";
bh["1000"] = "8"; bh["1001"] = "9"; bh["1010"] = "A"; bh["1011"] = "B";
bh["1100"] = "C"; bh["1101"] = "D"; bh["1110"] = "E"; bh["1111"] = "F";
// 小数ならば
IFB POS(".", bin) <> 0 THEN
DIM num = COPY(bin, 1, POS(".", bin) - 1)
DIM frac = COPY(bin, POS(".", bin) + 1)
num = strPad(num, CEIL(LENGTH(num) / 4) * 4, "0", LEFT)
frac = strPad(frac, CEIL(LENGTH(frac) / 4) * 4, "0", RIGHT)
DIM hex = ""
FOR i = 1 TO LENGTH(num) STEP 4
hex = hex + bh[COPY(num, i, 4)]
NEXT
hex = hex + "."
FOR i = 1 TO LENGTH(frac) STEP 4
hex = hex + bh[COPY(frac, i, 4)]
NEXT
RESULT = hex
ELSE
len = CEIL(LENGTH(bin) / 4) * 4
FOR i = 1 TO len - LENGTH(bin)
bin = "0" + bin
NEXT
bin = REPLACE(FORMAT(bin, len), " ", "0")
hex = ""
FOR i = 1 TO LENGTH(bin) / 4
str = COPY(bin, i * 4 - 3, 4)
hex = hex + bh[str]
NEXT
RESULT = hex
ENDIF
FEND
//////////////////////////////////////////////////
// 【引数】
// num : 10進数もしくは2進数の値
// bit : ビット
// 【戻り値】
// ビットを反転した値
//////////////////////////////////////////////////
FUNCTION bitNot(num, bit = EMPTY)
IFB isString(num) THEN
DIM res = ""
FOR i = 1 TO LENGTH(num)
DIM str = COPY(num, i, 1)
IFB str = "0" OR str = "1" THEN
res = res + (1 - VAL(str))
ELSE
res = res + str
ENDIF
NEXT
RESULT = res
ELSE
DIM exponent = IIF(bit = EMPTY, CEIL(LOGN(2, num + 1)), bit)
RESULT = POWER(2, exponent) - num - 1
ENDIF
FEND
//////////////////////////////////////////////////
// 【引数】
// dec : 10進数
// signFlg : 符号付きならばTrue
// digits : 変換した16進数の桁数合わせを自動で行うかを示すブール値、もしくは桁数を表す数値(8,16,24,32,64のいずれか)を指定
// errorMsg : エラーメッセージを出力するかを示すブール値
// 【戻り値】
// 16進数に変換した値
//////////////////////////////////////////////////
FUNCTION decToHex(dec, signFlg = FALSE, digits = FALSE, errorMsg = FALSE)
DIM hex = ""
DIM msg = ""
DIM isError = FALSE
DIM dec2hex[] = "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
DIM decimalFlg = IIF(POS(".", dec) <> 0, TRUE, FALSE)
DIM negativeFlg = IIF(dec < 0, TRUE, FALSE)
dec = ABS(dec)
// (1) 10進数を整数部と小数部に分ける
DIM integer = IIF(decimalFlg, COPY(dec, 1, POS(".", dec) - 1), dec)
DIM decimal = IIF(decimalFlg, "0." + COPY(dec, POS(".", dec) + 1), 0)
// (2) 10進数(整数部)を16進数に変換する。
REPEAT
hex = dec2hex[integer MOD 16] + hex
integer = INT(integer / 16)
UNTIL integer = 0
// (3) 10進数(小数部)を16進数に変換する。
IFB decimalFlg THEN
hex = hex + "."
DIM loop = 0
REPEAT
loop = loop + 1
decimal = decimal * 16
hex = hex + dec2hex[decimal]
offset = POWER(10, LENGTH(decimal) - POS(".", decimal))
decimal = (decimal * offset - INT(decimal) * offset) / offset
UNTIL decimal = 0 OR loop > 16
ENDIF
// digitsがFALSE以外なら
IFB digits THEN
// (4) 16進数の桁合わせを行う
DIM tmp = hex
DIM hexInteger = TOKEN(".", tmp)
DIM hexDecimal = TOKEN(".", tmp)
// 整数部、小数部を4bit単位になるまで拡張
// 整数部、4の倍数になるまで整数部の先頭に'0'を追加
// ※16進数は4bit単位なのでこの処理は不要
DIM digit = LENGTH(hexInteger + hexDecimal)
integer = INT(dec)
IF signFlg AND COPY(hexToBin(hexInteger), 1, 1) = "1" THEN hexInteger = "0" + hexInteger
IFB signFlg THEN
IFB integer >= -128 AND integer <= 127 THEN // -2^7〜2^7-1
hexInteger = strRepeat("0", 2 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer >= -32768 AND integer <= 32767 THEN // -2^15〜2^15-1
hexInteger = strRepeat("0", 4 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer >= -8388608 AND integer <= 8388607 THEN // -2^23〜2^23-1
hexInteger = strRepeat("0", 6 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer >= -2147783648 AND integer <= 2147483647 THEN // -2^31〜2^31-1
hexInteger = strRepeat("0", 8 - LENGTH(hexInteger)) + hexInteger
ELSE
hexInteger = strRepeat("0", 16 - LENGTH(hexInteger)) + hexInteger
ENDIF
ELSE
IFB integer <= 255 THEN // 2^8-1
hexInteger = strRepeat("0", 2 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer <= 65535 THEN // 2^16-1
hexInteger = strRepeat("0", 4 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer <= 16777215 THEN // 2^24-1
hexInteger = strRepeat("0", 6 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer <= 4294967295 THEN // 2^32-1
hexInteger = strRepeat("0", 8 - LENGTH(hexInteger)) + hexInteger
ELSE
hexInteger = strRepeat("0", 16 - LENGTH(hexInteger)) + hexInteger
ENDIF
ENDIF
totalDigits = LENGTH(hexInteger + hexDecimal) * 4
// 64bitを超えたら
IFB totalDigits > 64 THEN
DIM del32 = totalDigits - 32
DIM del64 = totalDigits - 64
IFB del32 = LENGTH(hexDecimal) * 4 AND digits <> 64 THEN
hexDecimal = ""
msg = "32bitを超えたため、小数点以下を削除しました"
ELSEIF del32 < LENGTH(hexDecimal) * 4 AND digits <> 64 THEN
hexDecimal = COPY(hexDecimal, 1, (LENGTH(hexDecimal) * 4 - del32) / 4)
msg = "32bitを超えたため、小数点以下の一部を削除しました"
ELSEIF del64 = LENGTH(hexDecimal) * 4 AND del64 <> 0 THEN
hexDecimal = ""
msg = "64bitを超えたため、小数点以下を削除しました"
ELSEIF del64 < LENGTH(hexDecimal) * 4 THEN
hexDecimal = COPY(hexDecimal, 1, (LENGTH(hexDecimal) * 4 - del64) / 4)
msg = "64bitを超えたため、小数点以下の一部を削除しました"
ELSE
isError = TRUE
msg = "64bitを超えるため、変換できません"
ENDIF
ENDIF
// 整数部、小数部の合計桁数を8,16,24,32,64bit単位になるまで拡張
digit = LENGTH(hexInteger + hexDecimal)
DIM array[] = 8, 16, 24, 32, 64
FOR item IN array
IFB digit <= item THEN
hexInteger = strRepeat("0", VAL(item)/4 - digit) + hexInteger
BREAK
ENDIF
NEXT
totalDigits = LENGTH(hexInteger + hexDecimal) * 4
IFB digits = TRUE THEN
// 桁合わせを自動調整
IFB totalDigits > 64 THEN
digit = LENGTH(hexInteger + hexDecimal)
WHILE LENGTH(hexInteger) > 8 AND digit > digits
IFB COPY(hexInteger, 1, 1) = "0" THEN
digit = digit - 1
ELSE
BREAK
ENDIF
WEND
WHILE LENGTH(hexDecimal) * 4 > 4 AND LENGTH(hexInteger + hexDecimal) > digits
IFB COPY(hexDecimal, LENGTH(hexDecimal) - 1) = "0" THEN
hexDecimal = COPY(hexDecimal, 1, LENGTH(hexDecimal) - 1)
ELSE
BREAK
ENDIF
WEND
tmp = hexInteger + "." + hexDecimal
hexInteger = COPY(tmp, 1, POS(".", tmp) - 1)
hexDecimal = COPY(tmp, POS(".", tmp) + 1)
totalDigits = LENGTH(hexInteger + hexDecimal)
IFB totalDigits > 64 THEN
isError = TRUE
msg = "64bitを超えたため変換できません"
ENDIF
ENDIF
ELSE
// 指定ビットに調整
IFB totalDigits <= digits THEN
hexInteger = strPad(hexInteger, digits / 4 - LENGTH(hexDecimal), "0", LEFT)
ELSE
// 桁あふれ調整
totalDigits = LENGTH(hexInteger + hexDecimal)
digit = LENGTH(hexInteger + hexDecimal)
WHILE LENGTH(hexInteger) * 4 > 8 AND digit > digits
IFB COPY(hexInteger, 1, 1) = "0" THEN
hexInteger = COPY(hexInteger, 2)
digit = digit - 4 / 4
ELSE
BREAK
ENDIF
WEND
WHILE LENGTH(hexDecimal) * 4 > 4 AND LENGTH(hexInteger + hexDecimal) > digits
IFB COPY(hexDecimal, LENGTH(hexDecimal) - 1) = "0" THEN
hexDecimal = COPY(hexDecimal, 1, LENGTH(hexDecimal) - 1)
ELSE
BREAK
ENDIF
WEND
tmp = hexInteger + "." + hexDecimal
hexInteger = COPY(tmp, 1, POS(".", tmp) - 1)
hexDecimal = COPY(tmp, POS(".", tmp) + 1)
digit = LENGTH(hexInteger + hexDecimal) * 4
IFB digit > digits THEN
DIM deleteLength = digit - digits
IFB deleteLength = LENGTH(hexDecimal) * 4 THEN
hexDecimal = ""
msg = "指定ビット数にするため小数点以下を削除しました"
ELSEIF deleteLength < LENGTH(hexDecimal) * 4 THEN
hexDecimal = COPY(hexDecimal, 1, LENGTH(hexDecimal) - deleteLength / 4)
msg = "指定ビット数にするため小数点以下の一部を削除しました"
ELSE
isError = TRUE
msg = "指定ビット数では変換できません"
ENDIF
ENDIF
ENDIF
ENDIF
hex = hexInteger + IIF(hexDecimal <> "", "." + hexDecimal, "")
// (5) 入力値がマイナスのため、16進数をマイナス値に変換する
IFB negativeFlg THEN
bin = hexToBin(hex)
// 1の補数
bin = bitNot(bin)
// 2の補数
DIM res = ""
DIM carry = "1"
FOR i = LENGTH(bin) TO 1 STEP -1
IFB carry = "1" THEN
SELECT COPY(bin, i, 1)
CASE "0"
res = "1" + res
carry = 0
CASE "1"
res = "0" + res
DEFAULT
res = COPY(bin, i, 1) + res
SELEND
ELSE
res = COPY(bin, i, 1) + res
ENDIF
NEXT
hex = binToHex(res)
ENDIF
ENDIF
IF errorMsg AND msg <> "" THEN PRINT msg
RESULT = IIF(isError, ERR_VALUE, hex)
FEND
//////////////////////////////////////////////////
// 【引数】
// 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";
DIM bin = ""
IFB POS(".", hex) <> 0 THEN
FOR i = 1 TO LENGTH(hex)
DIM str = COPY(hex, i, 1)
IF str = "." THEN bin = bin + "."
bin = bin + hb[str]
NEXT
ELSE
FOR i = 1 TO LENGTH(hex)
bin = bin + hb[COPY(hex, i, 1)]
NEXT
ENDIF
RESULT = bin
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
//////////////////////////////////////////////////
// 【引数】
// variable : 型を調べる変数
// 【戻り値】
// : TRUE : 与えられた変数が文字列型である、
// FALSE : 与えられた変数が文字列型でない、 :
//////////////////////////////////////////////////
FUNCTION isString(variable)
RESULT = IIF(VARTYPE(variable) = VAR_ASTR OR VARTYPE(variable) = VAR_USTR, TRUE, FALSE)
FEND
//////////////////////////////////////////////////
// 【引数】
// num : 符号を求める数値
// 【戻り値】
// 1 : 正の数、0 : ゼロ、-1 : 負の数、ERR_VALUE : それ以外
//////////////////////////////////////////////////
FUNCTION sign(num)
SELECT TRUE
CASE !CHKNUM(num)
RESULT = ERR_VALUE
CASE num > 0
RESULT = 1
CASE num = 0
RESULT = 0
CASE num < 0
RESULT = -1
SELEND
FEND
//////////////////////////////////////////////////
// 【引数】
// input : 入力文字列
// length : 埋めたあとの長さ
// str : 埋める文字
// type : 埋める方向
// 【戻り値】
// 指定文字で埋めた文字列
//////////////////////////////////////////////////
FUNCTION strPad(input, length, str = " ", type = RIGHT)
DIM s = ""
SELECT type
CASE LEFT
FOR i = 1 TO CEIL((length - LENGTH(input)) / LENGTH(str))
s = s + str
NEXT
input = COPY(s, 1, length - LENGTH(input)) + input
CASE RIGHT
FOR i = 1 TO CEIL((length - LENGTH(input)) / LENGTH(str))
s = s + str
NEXT
input = input + COPY(s, 1, length - LENGTH(input))
SELEND
RESULT = input
FEND
//////////////////////////////////////////////////
// 【引数】
// inputs : 繰り返す文字列
// multiplier : inputsを繰り返す回数
// 【戻り値】
// inputsをmultiplier回を繰り返した文字列を返します
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
DIM res = ""
FOR n = 1 TO multiplier
res = res + inputs
NEXT
RESULT = res
FEND
- 結果
U+3042
ひらがな
U+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3040 | ぁ | あ | ぃ | い | ぅ | う | ぇ | え | ぉ | お | か | が | き | ぎ | く | |
3050 | ぐ | け | げ | こ | ご | さ | ざ | し | じ | す | ず | せ | ぜ | そ | ぞ | た |
3060 | だ | ち | ぢ | っ | つ | づ | て | で | と | ど | な | に | ぬ | ね | の | は |
3070 | ば | ぱ | ひ | び | ぴ | ふ | ぶ | ぷ | へ | べ | ぺ | ほ | ぼ | ぽ | ま | み |
3080 | む | め | も | ゃ | や | ゅ | ゆ | ょ | よ | ら | り | る | れ | ろ | ゎ | わ |
3090 | ゐ | ゑ | を | ん | ゔ | ゕ | ゖ | ゙゚ | ゚ | ゛ | ゜ | ゝ | ゞ | ゟ |
カタカナ
U+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
30A0 | ァ | ア | ィ | イ | ゥ | ウ | ェ | エ | ォ | オ | カ | ガ | キ | ギ | ク | |
30B0 | グ | ケ | ゲ | コ | ゴ | サ | ザ | シ | ジ | ス | ズ | セ | ゼ | ソ | ゾ | タ |
30C0 | ダ | チ | ヂ | ッ | ツ | ヅ | テ | デ | ト | ド | ナ | ニ | ヌ | ネ | ノ | ハ |
30D0 | バ | パ | ヒ | ビ | ピ | フ | ブ | プ | ヘ | ベ | ペ | ホ | ボ | ポ | マ | ミ |
30E0 | ム | メ | モ | ャ | ヤ | ュ | ユ | ョ | ヨ | ラ | リ | ル | レ | ロ | ヮ | ワ |
30F0 | ヰ | ヱ | ヲ | ン | ヴ | ヵ | ヶ | ヷ | ヸ | ヹ | ヺ | ・ | ー | ヽ | ヾ | ヿ |
数字・アルファベット
U+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0030 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | : | ; | < | = | > | ? |
0040 | @ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O |
0050 | P | Q | R | S | T | U | V | W | X | Y | Z | [ | \ | ] | ^ | _ |
0060 | ` | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o |
0070 | p | q | r | s | t | u | v | w | x | y | z | { | | | } | ~ | DEL |
使い方
文字コードを取得
aの文字コードを10進数で取得します。
PRINT ASC("a")
- 結果
97
引数に2文字以上の文字列を指定した場合、1文字目の文字コードのみ返し2文字目以降は無視されます。以下は0の文字コードのみ返ります。
PRINT ASC("012")
- 結果
48
16進数表記で取得
ASC関数で取得した10進数表記の文字コードをdecToHex関数 (自作関数)とstrPad関数 (自作関数)で16進数表記のU+XXXXの形式に変換します。
PRINT "U+" + strPad(decToHex(ASC("0")), 4, "0", LEFT)
//////////////////////////////////////////////////
// 【引数】
// bin : 2進数
// 【戻り値】
// 16進数に変換した値
//////////////////////////////////////////////////
FUNCTION binToHex(bin)
HASHTBL bh
bh["0000"] = "0"; bh["0001"] = "1"; bh["0010"] = "2"; bh["0011"] = "3";
bh["0100"] = "4"; bh["0101"] = "5"; bh["0110"] = "6"; bh["0111"] = "7";
bh["1000"] = "8"; bh["1001"] = "9"; bh["1010"] = "A"; bh["1011"] = "B";
bh["1100"] = "C"; bh["1101"] = "D"; bh["1110"] = "E"; bh["1111"] = "F";
// 小数ならば
IFB POS(".", bin) <> 0 THEN
DIM num = COPY(bin, 1, POS(".", bin) - 1)
DIM frac = COPY(bin, POS(".", bin) + 1)
num = strPad(num, CEIL(LENGTH(num) / 4) * 4, "0", LEFT)
frac = strPad(frac, CEIL(LENGTH(frac) / 4) * 4, "0", RIGHT)
DIM hex = ""
FOR i = 1 TO LENGTH(num) STEP 4
hex = hex + bh[COPY(num, i, 4)]
NEXT
hex = hex + "."
FOR i = 1 TO LENGTH(frac) STEP 4
hex = hex + bh[COPY(frac, i, 4)]
NEXT
RESULT = hex
ELSE
len = CEIL(LENGTH(bin) / 4) * 4
FOR i = 1 TO len - LENGTH(bin)
bin = "0" + bin
NEXT
bin = REPLACE(FORMAT(bin, len), " ", "0")
hex = ""
FOR i = 1 TO LENGTH(bin) / 4
str = COPY(bin, i * 4 - 3, 4)
hex = hex + bh[str]
NEXT
RESULT = hex
ENDIF
FEND
//////////////////////////////////////////////////
// 【引数】
// num : 10進数もしくは2進数の値
// bit : ビット
// 【戻り値】
// ビットを反転した値
//////////////////////////////////////////////////
FUNCTION bitNot(num, bit = EMPTY)
IFB isString(num) THEN
DIM res = ""
FOR i = 1 TO LENGTH(num)
DIM str = COPY(num, i, 1)
IFB str = "0" OR str = "1" THEN
res = res + (1 - VAL(str))
ELSE
res = res + str
ENDIF
NEXT
RESULT = res
ELSE
DIM exponent = IIF(bit = EMPTY, CEIL(LOGN(2, num + 1)), bit)
RESULT = POWER(2, exponent) - num - 1
ENDIF
FEND
//////////////////////////////////////////////////
// 【引数】
// dec : 10進数
// signFlg : 符号付きならばTrue
// digits : 変換した16進数の桁数合わせを自動で行うかを示すブール値、もしくは桁数を表す数値(8,16,24,32,64のいずれか)を指定
// errorMsg : エラーメッセージを出力するかを示すブール値
// 【戻り値】
// 16進数に変換した値
//////////////////////////////////////////////////
FUNCTION decToHex(dec, signFlg = FALSE, digits = FALSE, errorMsg = FALSE)
DIM hex = ""
DIM msg = ""
DIM isError = FALSE
DIM dec2hex[] = "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
DIM decimalFlg = IIF(POS(".", dec) <> 0, TRUE, FALSE)
DIM negativeFlg = IIF(dec < 0, TRUE, FALSE)
dec = ABS(dec)
// (1) 10進数を整数部と小数部に分ける
DIM integer = IIF(decimalFlg, COPY(dec, 1, POS(".", dec) - 1), dec)
DIM decimal = IIF(decimalFlg, "0." + COPY(dec, POS(".", dec) + 1), 0)
// (2) 10進数(整数部)を16進数に変換する。
REPEAT
hex = dec2hex[integer MOD 16] + hex
integer = INT(integer / 16)
UNTIL integer = 0
// (3) 10進数(小数部)を16進数に変換する。
IFB decimalFlg THEN
hex = hex + "."
DIM loop = 0
REPEAT
loop = loop + 1
decimal = decimal * 16
hex = hex + dec2hex[decimal]
offset = POWER(10, LENGTH(decimal) - POS(".", decimal))
decimal = (decimal * offset - INT(decimal) * offset) / offset
UNTIL decimal = 0 OR loop > 16
ENDIF
// digitsがFALSE以外なら
IFB digits THEN
// (4) 16進数の桁合わせを行う
DIM tmp = hex
DIM hexInteger = TOKEN(".", tmp)
DIM hexDecimal = TOKEN(".", tmp)
// 整数部、小数部を4bit単位になるまで拡張
// 整数部、4の倍数になるまで整数部の先頭に'0'を追加
// ※16進数は4bit単位なのでこの処理は不要
DIM digit = LENGTH(hexInteger + hexDecimal)
integer = INT(dec)
IF signFlg AND COPY(hexToBin(hexInteger), 1, 1) = "1" THEN hexInteger = "0" + hexInteger
IFB signFlg THEN
IFB integer >= -128 AND integer <= 127 THEN // -2^7〜2^7-1
hexInteger = strRepeat("0", 2 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer >= -32768 AND integer <= 32767 THEN // -2^15〜2^15-1
hexInteger = strRepeat("0", 4 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer >= -8388608 AND integer <= 8388607 THEN // -2^23〜2^23-1
hexInteger = strRepeat("0", 6 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer >= -2147783648 AND integer <= 2147483647 THEN // -2^31〜2^31-1
hexInteger = strRepeat("0", 8 - LENGTH(hexInteger)) + hexInteger
ELSE
hexInteger = strRepeat("0", 16 - LENGTH(hexInteger)) + hexInteger
ENDIF
ELSE
IFB integer <= 255 THEN // 2^8-1
hexInteger = strRepeat("0", 2 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer <= 65535 THEN // 2^16-1
hexInteger = strRepeat("0", 4 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer <= 16777215 THEN // 2^24-1
hexInteger = strRepeat("0", 6 - LENGTH(hexInteger)) + hexInteger
ELSEIF integer <= 4294967295 THEN // 2^32-1
hexInteger = strRepeat("0", 8 - LENGTH(hexInteger)) + hexInteger
ELSE
hexInteger = strRepeat("0", 16 - LENGTH(hexInteger)) + hexInteger
ENDIF
ENDIF
totalDigits = LENGTH(hexInteger + hexDecimal) * 4
// 64bitを超えたら
IFB totalDigits > 64 THEN
DIM del32 = totalDigits - 32
DIM del64 = totalDigits - 64
IFB del32 = LENGTH(hexDecimal) * 4 AND digits <> 64 THEN
hexDecimal = ""
msg = "32bitを超えたため、小数点以下を削除しました"
ELSEIF del32 < LENGTH(hexDecimal) * 4 AND digits <> 64 THEN
hexDecimal = COPY(hexDecimal, 1, (LENGTH(hexDecimal) * 4 - del32) / 4)
msg = "32bitを超えたため、小数点以下の一部を削除しました"
ELSEIF del64 = LENGTH(hexDecimal) * 4 AND del64 <> 0 THEN
hexDecimal = ""
msg = "64bitを超えたため、小数点以下を削除しました"
ELSEIF del64 < LENGTH(hexDecimal) * 4 THEN
hexDecimal = COPY(hexDecimal, 1, (LENGTH(hexDecimal) * 4 - del64) / 4)
msg = "64bitを超えたため、小数点以下の一部を削除しました"
ELSE
isError = TRUE
msg = "64bitを超えるため、変換できません"
ENDIF
ENDIF
// 整数部、小数部の合計桁数を8,16,24,32,64bit単位になるまで拡張
digit = LENGTH(hexInteger + hexDecimal)
DIM array[] = 8, 16, 24, 32, 64
FOR item IN array
IFB digit <= item THEN
hexInteger = strRepeat("0", VAL(item)/4 - digit) + hexInteger
BREAK
ENDIF
NEXT
totalDigits = LENGTH(hexInteger + hexDecimal) * 4
IFB digits = TRUE THEN
// 桁合わせを自動調整
IFB totalDigits > 64 THEN
digit = LENGTH(hexInteger + hexDecimal)
WHILE LENGTH(hexInteger) > 8 AND digit > digits
IFB COPY(hexInteger, 1, 1) = "0" THEN
digit = digit - 1
ELSE
BREAK
ENDIF
WEND
WHILE LENGTH(hexDecimal) * 4 > 4 AND LENGTH(hexInteger + hexDecimal) > digits
IFB COPY(hexDecimal, LENGTH(hexDecimal) - 1) = "0" THEN
hexDecimal = COPY(hexDecimal, 1, LENGTH(hexDecimal) - 1)
ELSE
BREAK
ENDIF
WEND
tmp = hexInteger + "." + hexDecimal
hexInteger = COPY(tmp, 1, POS(".", tmp) - 1)
hexDecimal = COPY(tmp, POS(".", tmp) + 1)
totalDigits = LENGTH(hexInteger + hexDecimal)
IFB totalDigits > 64 THEN
isError = TRUE
msg = "64bitを超えたため変換できません"
ENDIF
ENDIF
ELSE
// 指定ビットに調整
IFB totalDigits <= digits THEN
hexInteger = strPad(hexInteger, digits / 4 - LENGTH(hexDecimal), "0", LEFT)
ELSE
// 桁あふれ調整
totalDigits = LENGTH(hexInteger + hexDecimal)
digit = LENGTH(hexInteger + hexDecimal)
WHILE LENGTH(hexInteger) * 4 > 8 AND digit > digits
IFB COPY(hexInteger, 1, 1) = "0" THEN
hexInteger = COPY(hexInteger, 2)
digit = digit - 4 / 4
ELSE
BREAK
ENDIF
WEND
WHILE LENGTH(hexDecimal) * 4 > 4 AND LENGTH(hexInteger + hexDecimal) > digits
IFB COPY(hexDecimal, LENGTH(hexDecimal) - 1) = "0" THEN
hexDecimal = COPY(hexDecimal, 1, LENGTH(hexDecimal) - 1)
ELSE
BREAK
ENDIF
WEND
tmp = hexInteger + "." + hexDecimal
hexInteger = COPY(tmp, 1, POS(".", tmp) - 1)
hexDecimal = COPY(tmp, POS(".", tmp) + 1)
digit = LENGTH(hexInteger + hexDecimal) * 4
IFB digit > digits THEN
DIM deleteLength = digit - digits
IFB deleteLength = LENGTH(hexDecimal) * 4 THEN
hexDecimal = ""
msg = "指定ビット数にするため小数点以下を削除しました"
ELSEIF deleteLength < LENGTH(hexDecimal) * 4 THEN
hexDecimal = COPY(hexDecimal, 1, LENGTH(hexDecimal) - deleteLength / 4)
msg = "指定ビット数にするため小数点以下の一部を削除しました"
ELSE
isError = TRUE
msg = "指定ビット数では変換できません"
ENDIF
ENDIF
ENDIF
ENDIF
hex = hexInteger + IIF(hexDecimal <> "", "." + hexDecimal, "")
// (5) 入力値がマイナスのため、16進数をマイナス値に変換する
IFB negativeFlg THEN
bin = hexToBin(hex)
// 1の補数
bin = bitNot(bin)
// 2の補数
DIM res = ""
DIM carry = "1"
FOR i = LENGTH(bin) TO 1 STEP -1
IFB carry = "1" THEN
SELECT COPY(bin, i, 1)
CASE "0"
res = "1" + res
carry = 0
CASE "1"
res = "0" + res
DEFAULT
res = COPY(bin, i, 1) + res
SELEND
ELSE
res = COPY(bin, i, 1) + res
ENDIF
NEXT
hex = binToHex(res)
ENDIF
ENDIF
IF errorMsg AND msg <> "" THEN PRINT msg
RESULT = IIF(isError, ERR_VALUE, hex)
FEND
//////////////////////////////////////////////////
// 【引数】
// 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";
DIM bin = ""
IFB POS(".", hex) <> 0 THEN
FOR i = 1 TO LENGTH(hex)
DIM str = COPY(hex, i, 1)
IF str = "." THEN bin = bin + "."
bin = bin + hb[str]
NEXT
ELSE
FOR i = 1 TO LENGTH(hex)
bin = bin + hb[COPY(hex, i, 1)]
NEXT
ENDIF
RESULT = bin
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
//////////////////////////////////////////////////
// 【引数】
// variable : 型を調べる変数
// 【戻り値】
// : TRUE : 与えられた変数が文字列型である、
// FALSE : 与えられた変数が文字列型でない、 :
//////////////////////////////////////////////////
FUNCTION isString(variable)
RESULT = IIF(VARTYPE(variable) = VAR_ASTR OR VARTYPE(variable) = VAR_USTR, TRUE, FALSE)
FEND
//////////////////////////////////////////////////
// 【引数】
// num : 符号を求める数値
// 【戻り値】
// 1 : 正の数、0 : ゼロ、-1 : 負の数、ERR_VALUE : それ以外
//////////////////////////////////////////////////
FUNCTION sign(num)
SELECT TRUE
CASE !CHKNUM(num)
RESULT = ERR_VALUE
CASE num > 0
RESULT = 1
CASE num = 0
RESULT = 0
CASE num < 0
RESULT = -1
SELEND
FEND
//////////////////////////////////////////////////
// 【引数】
// input : 入力文字列
// length : 埋めたあとの長さ
// str : 埋める文字
// type : 埋める方向
// 【戻り値】
// 指定文字で埋めた文字列
//////////////////////////////////////////////////
FUNCTION strPad(input, length, str = " ", type = RIGHT)
DIM s = ""
SELECT type
CASE LEFT
FOR i = 1 TO CEIL((length - LENGTH(input)) / LENGTH(str))
s = s + str
NEXT
input = COPY(s, 1, length - LENGTH(input)) + input
CASE RIGHT
FOR i = 1 TO CEIL((length - LENGTH(input)) / LENGTH(str))
s = s + str
NEXT
input = input + COPY(s, 1, length - LENGTH(input))
SELEND
RESULT = input
FEND
//////////////////////////////////////////////////
// 【引数】
// inputs : 繰り返す文字列
// multiplier : inputsを繰り返す回数
// 【戻り値】
// inputsをmultiplier回を繰り返した文字列を返します
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
DIM res = ""
FOR n = 1 TO multiplier
res = res + inputs
NEXT
RESULT = res
FEND
- 結果
U+0030
arrayに指定した文字を10進数・16に進数の文字コードに変換した結果を出力します。
DIM array[] = "あ", "ア", "ア", "A", "A", "a", "a", "𠮷", "𩸽", "?", "#"
PRINT "文字,10進数,16進数"
FOR item IN array
PRINT item + "," + ASC(item) + ",0x" + decToHex(ASC(item))
NEXT
- 結果
文字, 10進数, 16進数 あ, 12354, 0x3042 ア, 12450, 0x30A2 ア, 65393, 0xFF71 A, 65313, 0xFF21 A, 65, 0x41 a, 65345, 0xFF41 a, 97, 0x61 𠮷, 134071, 0x20BB7 𩸽, 171581, 0x29E3D ?, 63, 0x3F #, 35, 0x23