自定义函数 JSA_A_STRINGTOUTF16TOUINT8ARRAY 字符串转UTF-16编码

function JSA_A_STRINGTOUTF16TOUINT8ARRAY(string, format, endianness, hasBom) {
       //表格中使用函数,参数留空时,默认值处理
    string = string === -2147352572 ? undefined : string;
    format = format === -2147352572 ? undefined : format;
    endianness = endianness === -2147352572 ? undefined : endianness;
    hasBom = hasBom === -2147352572 ? undefined : hasBom;
    
    // 参数验证和默认值处理
    string = Array.isArray(string.valueOf()) ? string.valueOf().flat(Infinity).join("") : string.valueOf();
    const outputFormat = (format || 'dec').toLowerCase();
    const endianParam = (endianness || 'BE').toUpperCase();
    const addBom = hasBom || false;
    
    // 验证参数有效性
    if (string === undefined || string === "") {
        return "#N/A";
    }
    
    if (!['bin', 'hex', 'dec'].includes(outputFormat)) {
        return "#N/A";
    }
    
    if (!['BE', 'LE'].includes(endianParam)) {
        return "#N/A";
    }
    
    const inputArray = Array.from(string); // 将字符串转换为字符数组
    const charCount = inputArray.length;
    
    const encodeChar = (char) => { // 编码单个字符的函数
        const codePoint = char.codePointAt(0);
        const isSurrogate = codePoint > 65535;
        let bytes;
        
        if (isSurrogate) { // 处理代理对(surrogate pairs)
            const codePointOffset = codePoint - 65536;
            const highSurrogate = 55296 | ((codePointOffset >> 10) & 1023);
            const lowSurrogate = 56320 | (codePointOffset & 1023);
            
            const toBytes = (word) => { // 将代理对转换为字节
                const highByte = (word >> 8) & 255;
                const lowByte = word & 255;
                return endianParam === 'BE' ? [highByte, lowByte] : [lowByte, highByte];
            };
            
            bytes = [...toBytes(highSurrogate), ...toBytes(lowSurrogate)];
        } else { // 处理基本多文种平面(BMP)字符
            const toBytes = (word) => {
                const highByte = (word >> 8) & 255;
                const lowByte = word & 255;
                return endianParam === 'BE' ? [highByte, lowByte] : [lowByte, highByte];
            };
            
            bytes = toBytes(codePoint);
        }
        
        switch (outputFormat) { // 根据输出格式转换字节
            case 'bin':
                return bytes.map(b => {
                    const binStr = b.toString(2);
                    return '0'.repeat(8 - binStr.length) + binStr;
                });
            case 'hex':
                return bytes.map(b => {
                    const hexStr = b.toString(16).toUpperCase();
                    return hexStr.length === 1 ? '0' + hexStr : hexStr;
                });
            case 'dec':
                return bytes;
            default:
                return bytes;
        }
    };
    
    const encodedChars = inputArray.flatMap(char => encodeChar(char)); // 编码所有字符
    
    let bom = []; // 生成BOM(字节顺序标记)
    if (addBom) {
        switch (outputFormat) {
            case 'bin':
                bom = endianParam === 'BE' ? ['11111110', '11111111'] : ['11111111', '11111110'];
                break;
            case 'hex':
                bom = endianParam === 'BE' ? ['FE', 'FF'] : ['FF', 'FE'];
                break;
            case 'dec':
                bom = endianParam === 'BE' ? [254, 255] : [255, 254];
                break;
        }
    }
    
    // 返回最终结果
    const array = addBom ? [...bom, ...encodedChars] : encodedChars;
    
    let radix;
    
    switch (outputFormat) {
        case 'bin':
            radix = 2;
            break;
        case 'hex':
            radix = 16;
            break;
        case 'dec':
            radix = 10;
            break;
    }
    
    const uint8Array = new Uint8Array(array.map(v => parseInt(v, radix)));
    
    return true ? array : uint8Array
}

插入函数对话框、函数参数对话框:

加载宏文件function Workbook_Open(){}中添加以下代码。

Application.MacroOptions("JSA_A_STRINGTOUTF16TOUINT8ARRAY", "将字符串转换为UTF-16编码的数组(函数返回判断改为:true)或Uint8Array(函数返回判断改为:false)。", undefined, undefined, undefined, undefined, 14, undefined, undefined, undefined, ['字符串。', '输出格式。"dec":十进制字节值(默认),"hex":十六进制字节值,"bin":二进制字节值。', '字节序,"utf-16"需指定。"BE":大端序(高位字节在前)(默认),"LE":小端序(低位字节在前)。', '是否添加字节序标记(BOM),"utf-16"需指定。TRUE:添加,FALSE:不添加(默认)。']);

云南省
浏览 22
收藏
点赞
分享
+1
1
+1
全部评论 1
 
497128657
关系图:https://bbs.wps.cn/topic/68275
· 云南省
回复