Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active April 9, 2024 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdkfz181tiger/a8df14e1af8a123893e6806ab5d3b3bf to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/a8df14e1af8a123893e6806ab5d3b3bf to your computer and use it in GitHub Desktop.
基数変換で遊んでみよう_その1_ASCII変換
# coding: utf-8
# ASCII符号表(コンピュータ概論:p46)
ASCII = [[], [],
["SP", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/"],
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?"],
["@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"],
["P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "¥", "]", "^", "_"],
["`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"],
["p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "DEL"]
]
def main():
print("main.py!!")
msg = "HELLO"
# 文字列 "HELLO" -> ASCII
# -> [ "01001000", "01000101", "01001100", "01001100", "01001111" ]
bin_result = str2ascii(msg)
print("BIN:", msg, "->", bin_result)
# 2進数(8bit) -> 10進数変換
# -> [ 72, 69, 76, 76, 79 ]
dec_result = bin2dec(bin_result)
print("DEC:", msg, "->", dec_result)
# 検証
# 10進数(ASCIIコード) -> HELLO
check_result = dec2str(dec_result)
print("Check:", ''.join(check_result))
def str2ascii(s):
result = []
for c in s:
i, j = char2ascii(c)
result.append(d2b(i) + d2b(j))
return result
def char2ascii(c):
for i in range(len(ASCII)):
for j in range(len(ASCII[i])):
if c == ASCII[i][j]: return i, j
def bin2dec(bin):
result = []
for b in bin: result.append(b2d(b))
return result
def dec2str(dec):
result = []
for d in dec: result.append(chr(d))
return result
def d2b(d): return bin(d)[2:].zfill(4)
def d2h(d): return hex(d)[2:].zfill(1)
def b2d(b): return int(b, 2)
def h2b(h): return int(h, 16)
if __name__ == "__main__":
main()
"use strict";
// ASCII符号表(コンピュータ概論:p46)
const ASCII = [[], [],
["SP", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/"],
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?"],
["@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"],
["P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "¥", "]", "^", "_"],
["`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"],
["p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "DEL"]
];
$(document).ready(()=>{
console.log("Ready!!");
const msg = "HELLO";
// 文字列 "HELLO" -> ASCII
// -> [ "01001000", "01000101", "01001100", "01001100", "01001111" ]
const bin = str2ascii(msg);
console.log("BIN:", msg, "->", bin);
// 2進数 -> 10進数
// -> [ 72, 69, 76, 76, 79 ]
const dec = bin2dec(bin);
console.log("DEC:", msg, "->", dec);
// 検証
// 10進数 -> "HELLO"
const check = dec2str(dec);
console.log("Check:", check);
});
const str2ascii = str=>{
let result = [];
for(let s of str){
const [i, j] = char2ascii(s);
result.push(d2b(i) + d2b(j));// Decimal -> Binary
}
return result;
}
const char2ascii = c=>{
for(let i=0; i<ASCII.length; i++){
for(let j=0; j<ASCII[i].length; j++){
if(c == ASCII[i][j]) return [i, j];
}
}
}
const bin2dec = bin=>{
let result = [];
for(let b of bin) result.push(b2d(b));
return result;
}
const dec2str = dec=>{
let result = [];
for(let d of dec) result.push(String.fromCharCode(d));
return result.join("");
}
const d2b = d=>d.toString(2).padStart(4, "0");
const d2h = d=>d.toString(16).padStart(1, "0");
const b2d = b=>parseInt(b, 2);
const h2b = h=>parseInt(h, 16);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment