EXCEL VBA计算带汉字的字符串的MD5值(VBA转UTF-8保存)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EXCEL VBA计算带汉字的字符串的MD5值(VBA转UTF-8保存)相关的知识,希望对你有一定的参考价值。

操作思路:
1.使用 ADODB.stream 把字符串转换为UFT-8的编码集存储到文件 AA
2.使用调用windows命令行CertUtil -hashfile c:11AA md5 对文件计算MD5值
3.回显结果直接输出到EXCEL
4.字符串截取第二行,去空格,输出MD5值

VBA代码:

strBody = "VBA变量字符串转换为UFT-8后MD5加密"

‘MD5计算
Dim stream As New ADODB.stream
stream.Open
stream.Type = adTypeText
stream.Charset = "utf-8"
stream.WriteText strBody
‘ Try to Remove UTF-8 BOM
stream.Position = 3 ‘skip BOM
Dim BinaryStream As New ADODB.stream
BinaryStream.Type = adTypeBinary
BinaryStream.Mode = adModeReadWrite
BinaryStream.Open
‘ Strips BOM (first 3 bytes)
stream.CopyTo BinaryStream
stream.Flush
stream.Close
str8 = BinaryStream.Read
BinaryStream.SaveToFile "C:11AA", adSaveCreateOverWrite
BinaryStream.Flush
BinaryStream.Close
Dim WSH, wExec, sCmd As String
Set WSH = CreateObject("WScript.Shell")
Set wExec = WSH.Exec("CertUtil -hashfile c:11AA md5")
Ar = Split(wExec.StdOut.ReadAll, vbCrLf)
N = UBound(Ar)
Sign = Replace(Ar(1), Space(1), "")
Set wExec = Nothing
Set WSH = Nothing
Range("A12") = strBody

以上是关于EXCEL VBA计算带汉字的字符串的MD5值(VBA转UTF-8保存)的主要内容,如果未能解决你的问题,请参考以下文章

Excel vba 将数字转换成字符串的函数是哪个

Excel VBA中的时间计算

excel vba 将一个文本框TEXTBOX1限定只能输入数字,如果输入其他汉字或者字母提示,输入错误,请输入数字

vba 语法

VBA语言基础

Excel vba 将数字转换成字符串的函数是哪个