本帖最后由 tryhi 于 2024-10-29 17:13 编辑
用的不多,但是有这方面需求的人其实也不少,但是论坛也基本没这方面的代码,类似于vl-string->list和vl-list->string,只不过用的是UTF-8
- ;;将任意字符串转换为UTF-8编码,返回十进制数据表__作者:tryhi-大海
- ;;(try-str2UTF-8 "编码") ;-->(231 188 150 231 160 129)
- (defun try-str2UTF-8 (str / file_list fileget stream)
- (if (setq stream (vlax-create-object "Adodb.Stream"))
- (progn
- (vlax-put-property stream 'Type 2) ; 1二进制读取 2文本模式读取
- (vlax-put-property stream 'Mode 3) ; 1-读,2-写,3-读写
- (vlax-put-property stream 'Charset "utf-8") ; 设置编码为UTF-8
- (vlax-invoke stream 'Open)
- (vlax-invoke stream 'WriteText str)
- (vlax-put-property stream 'Position 0) ; 将位置重置为起始位置
- (vlax-put-property stream 'Type 1) ; 1二进制读取 2文本模式读取
- (setq FileGet(Vlax-Invoke-Method stream 'Read nil))
- (setq File_list (vlax-safearray->list (vlax-variant-value FileGet)))
- (vlax-release-object stream)
- (cdddr File_list);去除BOM
- )
- (princ"\n无法创建'Adodb.Stream'对象")
- )
- )
- ;;将UTF-8编码的十进制数据表转换为字符串__作者:tryhi-大海
- ;;(try-UTF-8toStr '(231 188 150 231 160 129)) ;-->"编码"
- (defun try-UTF-8toStr (decList / stream result saFileGet)
- (if (setq stream (vlax-create-object "Adodb.Stream"))
- (progn
- (vlax-put-property stream 'Type 1) ; 1二进制读取 2文本模式读取
- (vlax-invoke stream 'Open);; 打开流
- (setq saFileGet (vlax-make-safearray 17 (cons 0 (1- (length decList)))));; 创建SafeArray
- (vlax-safearray-fill saFileGet decList);; 填充SafeArray
- (Vlax-Invoke-Method stream 'Write saFileGet);; 写入二进制数据
- (vlax-put-property stream 'Position 0);; 将位置重置为起始位置
- (vlax-put-property stream 'Type 2); 2文本模式读取
- (vlax-put-property stream 'Charset "utf-8")
- (setq result (Vlax-Invoke-Method stream 'ReadText nil));; 读取文本数据
- (vlax-release-object stream);; 释放对象
- result
- )
- (princ"\n无法创建'Adodb.Stream'对象")
- )
- )
将字符串转换为UTF-8编码,返回十进制数据表
(try-str2UTF-8 "编码")
返回:(231 188 150 231 160 129)
将UTF-8编码的十进制数据表转换为字符串
(try-UTF-8toStr '(231 188 150 231 160 129))
返回:"编码"
|