진수변환 스크립트

“디지털 공학” 이란 과목을 듣는중
교수가 진수변환을 해오라는 무지막지한 숙제를 내준 관계로 (다합치면 백문제넘으려나)
귀찮아서 만들어버린 스크립트

; ————————————————————
; | Made by Xeriars                                                               |
; | (변경해서 쓰는건 니맘대로지만 이 부분은 지우지 말아주세요.)  |
; ————————————————————

alias dec2bqox {
if ($exists(div_tmp.txt)) { remove div_tmp.txt }
if (!$1) {
return 변환할숫자와 진법을 입력하여 주십시요. (ex : $chr(36) $+ dec2bqo(100,bin))
halt
}
if ($1 !isnum) {
return $1 $+ 은(는) 올바른 숫자가 아닙니다. 다시 확인하여 주십시요.
halt
}
if (!$2) {
return 몇진법으로 환산하실것인지 입력하여 주십시요.
halt
}
if ($1 > 100000000000000000000) {
return 숫자가 너무 큽니다. 다시 입력하여 주십시요.
halt
}
if ($2 == bin) { goto narycalc } | if ($2 == qua) { goto narycalc } | if ($2 == oct) { goto narycalc } | if ($2 == hex) { goto narycalc }
else {
return $2 $+ 은(는) 변환할수 없는 진법입니다.
return bin : 2진수, qua : 4진수, oct : 8진수, hex : 16진수 (ex : $chr(36) $+ dec2bqo(100,bin))
halt
}
:narycalc
var %i 0
var %divider $1
var %nary = $replace($2,bin,2,qua,4,oct,8,hex,16)
:div
write div_tmp.txt $calc(%divider % %nary)
if (%divider > 1) {
var %divider = $int($calc(%divider / %nary))
inc %i
goto div
}
var %q 0
while (%q < $lines(div_tmp.txt)) {
if (%nary == 16) {
var %div_result = %div_result $+ $replace($read(div_tmp.txt,%q),10,A,11,B,12,C,13,D,14,E,15,F)
}
if (%nary != 16) {
var %div_result = %div_result $+ $read(div_tmp.txt,%q)
}
inc %q
}
remove div_tmp.txt
return $reverse(%div_result,%div_result)
}
alias 숙제 {
var %i 00
var %amount 150
var %num $rand(1000,9999)
while (%i < %amount) {
inc %i
write con_result.txt %i $+ $chr(46) %num
write con_result.txt 2진수 : $dec2bqox(%num,bin)
write con_result.txt 4진수 : $dec2bqox(%num,qua)
write con_result.txt 8진수 : $dec2bqox(%num,oct)
write con_result.txt 16진수 : $dec2bqox(%num,hex)
write con_result.txt $crlf
var %num $rand(1000,9999)
}
}

결과는 다음과 같이 con_result.txt로 저장.

1. 4492
2진수 : 1000110001100
4진수 : 1012030
8진수 : 10614
16진수 : 118C

2. 4954
2진수 : 1001101011010
4진수 : 1031122
8진수 : 11532
16진수 : 135A

3. 2697
2진수 : 101010001001
4진수 : 0222021
8진수 : 05211
16진수 : 0A89

냠냠.. 귀찮~
위 함수로 살짝 건드려서 만든.. @진수변환 스크립트

; ————————————————————
; | Made by Xeriars                                                               |
; | (변경해서 쓰는건 니맘대로지만 이 부분은 지우지 말아주세요.)  |
; ————————————————————

on *:text:*:#: {
if ($1 == @진수변환) {
if (!$2) { msg $chan Usage : @진수변환 변환할진수. (10진수만 가능) | halt }
if ($2 !isnum) { msg $chan 올바른 숫자가 아닙니다. 숫자로만 입력하십시오. | halt }
msg $chan 2진수 : $dec2bqox($2,bin)
msg $chan 4진수 : $dec2bqox($2,qua)
msg $chan 8진수 : $dec2bqox($2,oct)
msg $chan 16진수 : $dec2bqox($2,hex)
halt
}
}

댓글 남기기