简体   繁体   English

VBA Excel:Dec2Hex不止一个地方

[英]VBA Excel: Dec2Hex more than one place

In a macro (shown below), I convert a row of cells from Decimal to Hexadecimal. 在一个宏(如下所示)中,我将一行单元格从Decimal转换为Hexadecimal。 When I use the normal Dec2Hex conversion (NOT in VBA), I am able to get two places (8 --> 08). 当我使用常规的Dec2Hex转换(在VBA中为NOT)时,我可以获得两个位置(8-> 08)。 However, when I use Dec2Hex conversion IN VBA, I can't seem to get 2 places until I actually need to characters (8 --> 8 BUT 10 --> 0A). 但是,当我在VBA中使用Dec2Hex转换时,在我实际上需要输入字符之前,似乎无法获得2位(8-> 8 BUT 10-> 0A)。 How could I force that last character without just adding on a 0. Thanks! 我怎么能不加0就强制最后一个字符。谢谢!

FOLLOW UP 跟进

One interesting thing I noticed is if you look in the macro, I add a space after each hex value. 我注意到的一件有趣的事情是,如果您查看宏,则会在每个十六进制值后添加一个空格。 However, when looking at the resulting file, a space is only added when two characters are present. 但是,当查看结果文件时,仅当存在两个字符时才添加空格。

SAMPLE 样品

F3 07 00 31 FA 10 // F3 07 00 31 FA 10 //

F3 07 00 31 FB 20 // F3 07 00 31 FB 20 //

F3 07 00 31 FC 00 // F3 07 00 31 FC 00 //

F3 25 00 31 FF 830B 5114// <-- Notice the odd spacing. F3 25 00 31 FF 830B 5114 // <-注意奇数间距。 It should look like ...08 03 0B... 它看起来应该像... 08 03 0B ...

F3 07 00 31 FA 11 // F3 07 00 31 FA 11 //

Private Sub CommandButton3_Click()

Dim fso As New FileSystemObject
Dim stream As TextStream
Dim FilePath As String
Dim saveDialog As Variant
Dim hexVal As String
Dim updateRate As String

'"N" is selected elsewhere by the user and simply dictates how far down a row cells are active     


For i = 4 To N + 3
    Cells(3, i) = Application.WorksheetFunction.Dec2Hex(Cells(2, i), 2) & " "
Next i

For i = 4 To N + 3
    hexVal = hexVal & Cells(3, i)
Next i

updateRate = ComboBox1.Value

saveDialog = Application.GetSaveAsFilename(FileFilter:="Script Files(*.us1),*.us1")

    Set stream = fso.OpenTextFile(saveDialog, ForWriting, True)

        stream.WriteLine "F3 07 00 31 FA 10 // "
        stream.WriteLine "F3 07 00 31 FB " + updateRate + " // "
        stream.WriteLine "F3 07 00 31 FC 00 // "
        stream.WriteLine "F3 25 00 31 FF " + hexVal + "// "
        stream.WriteLine "F3 07 00 31 FA 11 // "

    stream.Close

End Sub

The problem I think is that the cells 3,i are numeric or general. 我认为的问题是单元格3,i是数字的还是通用的。 Either of these will truncate leading zeros. 这些中的任何一个都会截断前导零。 However, if you change the cell format to TEXT the leading zero will not get truncated before you write out to the file. 但是,如果将单元格格式更改为TEXT,则在写出文件之前,前导零将不会被截断。

You did: 您做了:

> For i = 4 To N + 3
>     Cells(3, i) = Application.WorksheetFunction.Dec2Hex(Cells(2, i), 2) & " " 
> Next i
> 
> For i = 4 To N + 3
>     hexVal = hexVal & Cells(3, i) 
> Next i

First, VBA (Excel) has the "hex()" function, why you use worksheetfunction.dec2hex? 首先,VBA(Excel)具有“ hex()”功能,为什么要使用worksheetfunction.dec2hex? Second, why you use a cell to store your hex for later retrieve and insert into the hexval? 其次,为什么要使用单元格来存储十六进制以供以后检索并插入十六进制?

You can replace both with: 您可以将两者替换为:

For i = 4 To N + 3
    hext = hex(cells(2,i)) & " "
    Cells(3, i) = hext
    hexVal = hexVal & hext
Next i

If you want even number of digits in hex values, for example "B" becomes "0B", 3FC becomes 03FC, then: 如果希望十六进制值中的偶数个数字,例如“ B”变为“ 0B”,3FC变为03FC,则:

For i = 4 To N + 3
    hext = hex(cells(2,i)) & " "
    if len(hext) Mod 2 = 0 then hext = "0" & hext
    Cells(3, i) = hext  ' if you really need Cells(3,i) to store it
    hexVal = hexVal & hext
Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM