Excel VBA十六进制到ascii错误 [英] Excel VBA hex to ascii error

查看:481
本文介绍了Excel VBA十六进制到ascii错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友



我发现以下功能将十六进制转换为文本。它适用于大部分十六进制文字转换,但会产生异常结果。 p>

例如:Hex值为:

  050003d40201414c4552542d42656c6f772038353435206966204e462054726164657320666f722031352d3230206d696e75746573202c77617463682050414e49432050414e4943207570746f20353439332d2d383437360d0a0d0a53656c6c204549434845522061742032343931302e2e2e73746f706c6f7373732032353 

结果,我获得使用hex2text功能= <强> |



正确的结果应该是这样的:

  ALERT-低于8545如果NF交易15-20分钟,观看PANIC PANIC至5493--8476 ........ 

(注意:我使用hex2ascii转换网站 http:// www。 rapidtables.com/c转换/数字/十六进制到ascii.htm 以获得正确的结果)



我的Hex2text函数是:

 公共函数HexToText(文本作为范围)As String 
Dim i As Integer
Dim DummyStr As String

对于i = 1到Len(文本)步骤2
DummyStr = DummyStr& Chr(Val(& H&(Mid(text,i,2))))
DoEvents
Next i

HexToText = DummyStr
结束功能

请帮助/提示

解决方案

正如Alex所说,前6个字节正在引起问题。



您可以从第13个字符开始,通过更改ForNext循环:

  For i = 13 To Len(text)步骤2 
pre>

或者您可以过滤ASCII码低于32的任何字符。

 code>如果Val(& H&(Mid(Text,i,2)))> 31然后DummyStr = DummyStr& Chr(Val(& H&(Mid(Text,i,2))))

或者替换(例如)一个空格。

 如果Val(& H& (Text,i,2)))> 31然后
DummyStr = DummyStr& Chr(Val(& H&(Mid(Text,i,2))))
Else
DummyStr = DummyStr&
结束如果


Friends,

I have found below stated function for conversion of hex to text.It works fin for most of hex to text conversions but give abnormal result.

For Example: Hex value is:

050003d40201414c4552542d42656c6f772038353435206966204e462054726164657320666f722031352d3230206d696e75746573202c77617463682050414e49432050414e4943207570746f20353439332d2d383437360d0a0d0a53656c6c204549434845522061742032343931302e2e2e73746f706c6f7373732032353

Result i get on using hex2text Function = |

Correct Result should have been something like:

ALERT-Below 8545 if NF Trades for 15-20 minutes ,watch PANIC PANIC upto 5493--8476........

(Note: I used hex2ascii conversion website http://www.rapidtables.com/convert/number/hex-to-ascii.htm to obtain correct results)

My Hex2text Function is:

Public Function HexToText(text As Range) As String
    Dim i As Integer
    Dim DummyStr As String

    For i = 1 To Len(text) Step 2
       DummyStr = DummyStr & Chr(Val("&H" & (Mid(text, i, 2))))
       DoEvents
    Next i

    HexToText = DummyStr
End Function

PLEASE HELP/SUGGEST

解决方案

As Alex has stated, the first 6 bytes are causing the issue.

You could either start at the 13th character by changing the ForNext loop:

 For i = 13 To Len(text) Step 2

Or you could filter any characters with ASCII code lower than 32 out..

If Val("&H" & (Mid(Text, i, 2))) > 31 Then DummyStr = DummyStr & Chr(Val("&H" & (Mid(Text, i, 2))))

Or replace them with (for instance) a space..

If Val("&H" & (Mid(Text, i, 2))) > 31 Then 
   DummyStr = DummyStr & Chr(Val("&H" & (Mid(Text, i, 2))))
Else
   DummyStr = DummyStr & " "
End If

这篇关于Excel VBA十六进制到ascii错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆