ssts-hospital-web-master项目实战记录二十二:项目迁移-基础模块实现(convert-util)

记录时间:2024-02-26

一、convert-util模块实现

 framework/utils/convert-util.ts

//字符串转Ascii码
const StringToAscii = function (str: string) {
  let ans = ''
  for (let i = 0; i < str.length; i++) {
    const c = str.substring(i, i + 1)
    const code = c.charCodeAt(0)
    ans += code.toString(16)
  }
  return ans.toUpperCase()
}
//Ascii码转字符串
const ASCIIToString = function (str: string) {
  let ret = ''
  const count = 2
  try {
    if (str.length % count != 0) {
      const error = '长度=' + str.length + ',不能被' + count + '整除。'
      throw error
    }
    for (let i = 0; i < str.length; i += count) {
      const s = str.substring(i, i + count)
      const code = parseInt(s, 16)
      ret += String.fromCharCode(code)
    }
  } catch (e) {
    ret = str
  }

  return ret
}

export { StringToAscii, ASCIIToString }

 

二、调用示例

 无

三、运行测试

 无

 

posted @ 2024-02-26 08:55  lizhigang  阅读(2)  评论(0编辑  收藏  举报