Files
cut-abstractions/tests/dev1/dataHandle/common/base/StringBase64.ts

153 lines
4.3 KiB
TypeScript
Raw Normal View History

2025-07-22 18:22:31 +08:00
import gb2312 from './gb2312.json'
export class StringBase64 {
static ToBase64_gb2312(str: string): string {
let bin = this.toGB2312Bytes(str || '')
let str64 = this.encode(bin)
return str64
}
static _table = ['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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/']
/** 将二进制转换成base64 */
static encode(bin: number[]): string {
let codes = []
let un = 0
un = bin.length % 3
if (un == 1)
bin.push(0, 0)
else if (un == 2)
bin.push(0)
for (let i = 2; i < bin.length; i += 3) {
let c = bin[i - 2] << 16
c |= bin[i - 1] << 8
c |= bin[i]
codes.push(this._table[c >> 18 & 0x3F])
codes.push(this._table[c >> 12 & 0x3F])
codes.push(this._table[c >> 6 & 0x3F])
codes.push(this._table[c & 0x3F])
}
if (un >= 1) {
codes[codes.length - 1] = '='
bin.pop()
}
if (un == 1) {
codes[codes.length - 2] = '='
bin.pop()
}
return codes.join('')
}
/** 将utf8 转成gb2312字符串 */
static toGb2312String(str1: string): string {
let substr = ''
let a = ''
let b = ''
let c = ''
let i = -1
i = str1.indexOf('%')
if (i == -1) {
return str1
}
while (i != -1) {
if (i < 3) {
substr = substr + str1.substr(0, i - 1)
str1 = str1.substr(i + 1, str1.length - i)
a = str1.substr(0, 2)
str1 = str1.substr(2, str1.length - 2)
if ((Number.parseInt(`0x${a}`) & 0x80) == 0) {
substr = substr + String.fromCharCode(Number.parseInt(`0x${a}`))
}
else if ((Number.parseInt(`0x${a}`) & 0xE0) == 0xC0) { // two byte
b = str1.substr(1, 2)
str1 = str1.substr(3, str1.length - 3)
let widechar = (Number.parseInt(`0x${a}`) & 0x1F) << 6
widechar = widechar | (Number.parseInt(`0x${b}`) & 0x3F)
substr = substr + String.fromCharCode(widechar)
}
else {
b = str1.substr(1, 2)
str1 = str1.substr(3, str1.length - 3)
c = str1.substr(1, 2)
str1 = str1.substr(3, str1.length - 3)
let widechar = (Number.parseInt(`0x${a}`) & 0x0F) << 12
widechar = widechar | ((Number.parseInt(`0x${b}`) & 0x3F) << 6)
widechar = widechar | (Number.parseInt(`0x${c}`) & 0x3F)
substr = substr + String.fromCharCode(widechar)
}
}
else {
substr = substr + str1.substring(0, i)
str1 = str1.substring(i)
}
i = str1.indexOf('%')
}
return substr + str1
}
private static _unicode2gb
static getUnicode2gb() {
if (this._unicode2gb == null) {
this._unicode2gb = gb2312
}
return this._unicode2gb
}
static toGB2312Bytes(str: string): number[] {
let unicode2gb = this.getUnicode2gb()
let res = []; let len = str.length
for (let i = 0; i < len; i++) {
let code = str.charCodeAt(i)
if (code <= 0x007F) {
res.push(code)
}
else {
let hex = unicode2gb[`0x${code.toString(16).toUpperCase()}`]
let gb = Number(hex)
if (Number.isNaN(gb))
gb = Number('0xA1F5')
let arr = []
while (gb > 0) {
arr.push(gb & 0xFF)
gb >>= 8
}
while (arr.length > 0) res.push(arr.pop())
}
}
return res
}
static fromGB2312Bytes(gb2312Bytes: number[]): string {
let unicode2gb = this.getUnicode2gb()
let res = []
// var i = 0
for (let i = 0; i < gb2312Bytes.length; i++) {
let code = gb2312Bytes[i]
if (code < 0xA1 || code > 0xFE || i + 1 == gb2312Bytes.length) {
res.push(String.fromCharCode(code))
continue
}
let c2 = gb2312Bytes[i + 1]
if (code < 0xA1 || code > 0xFE) {
res.push(String.fromCharCode(code))
continue
}
let g = c2 | code << 8
c2 = Number(unicode2gb[`0x${g.toString(16).toUpperCase()}`])
if (typeof c2 == 'undefined') {
res.push(String.fromCharCode(code))
continue
}
res.push(String.fromCharCode(c2))
i++
}
return res.join('')
}
}