提交 4e382518 编写于 作者: B baiy 提交者: ninecents

ascii编码转换 #41

上级 d97caef9
...@@ -39,6 +39,7 @@ const tool = [ ...@@ -39,6 +39,7 @@ const tool = [
{'name': 'time', 'title': '时间计算器', 'cat': ['other']}, {'name': 'time', 'title': '时间计算器', 'cat': ['other']},
{'name': 'uuid', 'title': 'UUID生成', 'cat': ['other']}, {'name': 'uuid', 'title': 'UUID生成', 'cat': ['other']},
{'name': 'jsonToObject', 'title': 'JSON转实体类', 'cat': ['conversion', 'serialize']}, {'name': 'jsonToObject', 'title': 'JSON转实体类', 'cat': ['conversion', 'serialize']},
{'name': 'ascii', 'title': 'ascii转换', 'cat': ['conversion']},
] ]
module.exports = { module.exports = {
......
...@@ -89,6 +89,14 @@ const routes = [ ...@@ -89,6 +89,14 @@ const routes = [
{ {
path: '/tool/uuid', path: '/tool/uuid',
component: r => require(['./views/tool/uuid.vue'], r) component: r => require(['./views/tool/uuid.vue'], r)
},
{
path: '/tool/jsonToObject',
component: r => require(['./views/tool/jsonToObject.vue'], r)
},
{
path: '/tool/ascii',
component: r => require(['./views/tool/ascii.vue'], r)
} }
] ]
......
<template>
<div>
<Tabs v-model="operation">
<TabPane label="转换" name="convent">
<option-block>
<Input v-model="current.data.dec" placeholder="多个字符用空格分隔">
<div slot="prepend" style="width: 100px"><strong>十进制</strong></div>
</Input>
</option-block>
<option-block>
<Input v-model="current.data.hex" placeholder="多个字符用空格分隔">
<div slot="prepend" style="width: 100px"><strong>十六进制</strong></div>
</Input>
</option-block>
<option-block>
<Input v-model="current.data.oct" placeholder="多个字符用空格分隔">
<div slot="prepend" style="width: 100px"><strong>八进制</strong></div>
</Input>
</option-block>
<option-block>
<Input v-model="current.data.bin" placeholder="多个字符用空格分隔">
<div slot="prepend" style="width: 100px"><strong>二进制</strong></div>
</Input>
</option-block>
<option-block>
<Input v-model="current.data.str">
<div slot="prepend" style="width: 100px"><strong>字符串</strong></div>
</Input>
</option-block>
<option-block :style="{textAlign:'center'}">
<FormItem>
<ButtonGroup>
<Button type="primary" @click="handle()" style="margin-right: 5px">转换</Button>
<Button type="primary" @click="clear()">清空</Button>
</ButtonGroup>
</FormItem>
</option-block>
</TabPane>
<TabPane label="编码表" name="reader">
<Table :columns="referenceColumns" :data="reference">
</Table>
</TabPane>
</Tabs>
</div>
</template>
<script>
import ascii from './library/ascii';
export default {
computed: {
reference() {
let lists = [];
for (let index=0; index < ascii.ascii_map.length; index++) {
let ob = new ascii.Ascii(index,'dec')
let isShow = !ascii.ascii_hidden.hasOwnProperty(ascii.ascii_map[index])
lists.push({
dec:ob.dec(),
hex:ob.hex(),
oct:ob.oct(),
bin:ob.bin(),
str:ob.str(),
show:isShow ? "" : "",
explain:isShow ? ob.str() : ascii.ascii_hidden[ascii.ascii_map[index]]
})
}
return lists;
},
},
created() {
this.current = Object.assign(this.current, this.$getToolData())
},
methods: {
handle() {
let s = "";
let currentType = "";
try {
for (const type of this.types) {
if (this.current.data[type]) {
s = this.current.data[type]
currentType = type
break;
}
}
if (!s){
throw "请输入对应的待转换编码"
}
} catch (err) {
return this.$Message.error("转换异常:" + err);
}
for (const type of this.types) {
this.current.data[type] = ascii.convent(s,currentType,type)
}
this.$saveToolData(this.current);
},
clear() {
for (const type of this.types) {
this.current.data[type] = ""
}
}
},
data() {
return {
types:['str', 'dec', 'hex', 'oct', 'bin'],
current: {
data: {
dec: "",
hex: "",
oct: "",
bin: "",
str: "",
}
},
operation: "convent",
referenceColumns: [
{
title: '十进制',
key: 'dec',
width:100
},
{
title: '十六进制',
key: 'hex',
width:100
},
{
title: '八进制',
key: 'oct',
width:100
},
{
title: '二进制',
key: 'bin',
},
{
title: '字符',
key: 'str',
width:100
},
{
title: '是否可见',
key: 'show',
width:100
},
{
title: '字符说明',
key: 'explain',
}
],
}
}
}
</script>
\ No newline at end of file
// ASCII MAP
import Radix from "radix.js";
const ASCII_MAP = ['NUL', 'SOH', 'STX', 'ETX', 'EOT', 'ENQ', 'ACK', 'BEL', 'BS', 'TAB', 'LF', 'VT', 'FF', 'CR', 'SO', 'SI',
'DLE', 'DC1', 'DC2', 'DC3', 'DC4', 'NAK', 'SYN', 'ETB', 'CAN', 'EM', 'SUB', 'ESC', 'FS', 'GS', 'RS', 'US',
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'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']
// ASCII 不可显示字符
const ASCII_HIDDEN = {
'NUL': "空字符(Null)",
'SOH': "标题开始",
'STX': "本文开始",
'ETX': "本文结束",
'EOT': "传输结束",
'ENQ': "请求",
'ACK': "确认回应",
'BEL': "响铃",
'BS': "退格",
'TAB': "水平定位符号",
'LF': "换行键",
'VT': "垂直定位符号",
'FF': "换页键",
'CR': "归位键",
'SO': "取消变换(Shift out)",
'SI': "启用变换(Shift in)",
'DLE': "跳出数据通讯",
'DC1': "设备控制一(XON 启用软件速度控制)",
'DC2': "设备控制二",
'DC3': "设备控制三(XOFF 停用软件速度控制)",
'DC4': "设备控制四",
'NAK': "确认失败回应",
'SYN': "同步用暂停",
'ETB': "区块传输结束",
'CAN': "取消",
'EM': "连接介质中断",
'SUB': "替换",
'ESC': "跳出",
'FS': "文件分割符",
'GS': "组群分隔符",
'RS': "记录分隔符",
'US': "单元分隔符",
'DEL': "删除",
}
const radix = new Radix();
class Ascii {
constructor(c, type = "str") {
let dec = -1;
c = c + "";
if (type !== "str"){
c = c.toLowerCase();
}
switch (type) {
case 'str':
dec = ASCII_MAP.indexOf(c)
break;
case 'dec':
dec = radix.convent(c, 10, 10);
break;
case 'hex':
dec = radix.convent(c, 16, 10);
break;
case 'oct':
dec = radix.convent(c, 8, 10);
break;
case 'bin':
dec = radix.convent(c.replace(/\b(0+)/gi, ""), 2, 10);
break;
default:
throw 'type error'
}
if (dec < 0 || dec > 127) {
throw 'input error'
}
this.decData = dec
}
dec() {
return this.decData + ""
}
hex() {
return (radix.convent(this.decData, 10, 16) + "").toUpperCase()
}
oct() {
return radix.convent(this.decData, 10, 8) + ""
}
bin() {
return (radix.convent(this.decData, 10, 2) + "").padStart(8, '0')
}
str() {
return ASCII_MAP[this.decData]
}
}
const convent = (s, currentType, targetType) => {
const types = ['str', 'dec', 'hex', 'oct', 'bin'];
if (!types.includes(currentType) || !types.includes(targetType)) {
throw 'type error'
}
let r = [];
for (const c of s.split(currentType === "str" ? '' : ' ')) {
let ascii = new Ascii(c, currentType);
r.push(ascii[targetType]())
}
return r.join(targetType === "str" ? '' : ' ');
}
export default {
convent,
Ascii,
ascii_map: ASCII_MAP,
ascii_hidden: ASCII_HIDDEN,
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册