覚書 mot2bin

VBScriptで書いてみた。

 

Option Explicit

function Test(filepath_source, filepath_dest, address_offset, linebytes)
    Dim srBin, srMot
    Dim srBinData
    Dim binlength
    Dim strOutputLine
    Dim sum, val
    Dim i, j
    Dim address


    Set srBin = CreateObject("ADODB.Stream")
    Set srMot = CreateObject("ADODB.Stream")

    srBin.Mode = 3 'Read/Write Mode
    srBin.Type = 1 'Binary Type

    srMot.Mode = 3 'Write Mode
    srMot.Type = 2 'Text Type

    srBin.Open
    srMot.Open

    ' WScript.Echo filepath
    srBin.LoadFromFile(filepath_source)

    srBin.Position = 0 
    srMot.Position = 0

    srBinData = srBin.Read()
    srBin.Close
    Set srBin = Nothing
    binlength = UBound(srBinData) + 1

    'HEADER
    strOutputLine = "S00F000068656C6C6F202020202000003C" 'Wikipediaから
    srMot.WriteText strOutputLine, 1 'Write Line

    'DATA
    sum = 0
    val = 0
    i = 1
    for i=1 to binlength
    ' for i=1 to 16

        if 1 = (i mod linebytes) then
            address = &H1000000& + address_offset + i - 1
            strOutputLine = "S2"
            strOutputline = strOutputLine & right(hex(3 + linebytes+ 1 + &H100),2) ' address data checksum
            strOutputLine = strOutputLine & right(hex(address), 6)
        end if

        val = AscB(MidB(srBinData, i, 1))
        strOutputLine = strOutputLine + Right(Hex(val + &H100), 2)

        if *1 OR (i = binlength)) then
            ' sum = 0
            ' for j = 3 to Len(strOutputLine) step 2
            '     sum = sum + CInt("&H" & mid(strOutputLine, j, 2))
            ' next
            ' sum = &HFF - (sum And &H00FF)
            ' strOutputLine = strOutputLine & right(hex(sum + &H100), 2) 'checksum
            sum = calculateChecksum(mid(strOutputLine,3, len(strOutputLine)-2))
            strOutputLine = strOutputLine & right(hex(sum + &H100), 2) 'checksum
            ' WScript.Echo strOutputLine
            srMot.WriteText strOutputLine, 1 'Write Line
        end if
    next

    'FOOTER
    val = CInt(binlength / linebytes)
    if (val <= 65535) then
        strOutputLine = "S503"
        strOutputline = strOutputLine & right(hex(val + &H10000&), 4)
    else
        strOutputLine = "S605"
        strOutputline = strOutputLine & right(hex(val + &H10000&), 6)
    end if
    sum = calculateChecksum(mid(strOutputLine,3, len(strOutputLine)-2))
    strOutputLine = strOutputLine & right(hex(sum + &H100), 2)
    srMot.WriteText strOutputLine, 1 'Write Line
    ' WScript.Echo strOutputLine

    strOutputLine = "S804000000"
    sum = calculateChecksum(mid(strOutputLine,3, len(strOutputLine)-2))
    strOutputLine = strOutputLine & right(hex(sum + &H100), 2)
    srMot.WriteText strOutputLine, 1 'Write Line

      srMot.SaveToFile filepath_dest, 2
    srMot.Close

    Set srMot = Nothing
    
End function

Function calculateChecksum(targetString)
    Dim i, sum
    'WScript.Echo targetString
    sum = 0
    for i = 1 to Len(targetString) step 2
        sum = sum + CInt("&H" & mid(targetString, i, 2))
    next
    sum = &HFF - (sum And &H00FF)
    calculateChecksum = sum
end function

 

Visual studio Code でCopyするとこうなるのか。

Option Explicit

function Test(filepath_source, filepath_dest, address_offset, linebytes)
    Dim srBin, srMot
    Dim srBinData
    Dim binlength
    Dim strOutputLine
    Dim sum, val
    Dim i, j
    Dim address


    Set srBin = CreateObject("ADODB.Stream")
    Set srMot = CreateObject("ADODB.Stream")

    srBin.Mode = 3 'Read/Write Mode
    srBin.Type = 1 'Binary Type

    srMot.Mode = 3 'Write Mode
    srMot.Type = 2 'Text Type

    srBin.Open
    srMot.Open

    ' WScript.Echo filepath
    srBin.LoadFromFile(filepath_source)

    srBin.Position = 0
    srMot.Position = 0

    srBinData = srBin.Read()
    srBin.Close
    Set srBin = Nothing
    binlength = UBound(srBinData) + 1

    'HEADER
    strOutputLine = "S00F000068656C6C6F202020202000003C" 'Wikipediaから
    srMot.WriteText strOutputLine, 1 'Write Line

    'DATA
    sum = 0
    val = 0
    i = 1
    for i=1 to binlength
   
        if 1 = (i mod linebytes) then
            address = &H1000000& + address_offset + i - 1
            strOutputLine = "S2"
            strOutputline = strOutputLine & right(hex(3 + linebytes+ 1 + &H100),2) ' address data checksum
            strOutputLine = strOutputLine & right(hex(address), 6)
        end if

        val = AscB(MidB(srBinData, i, 1))
        strOutputLine = strOutputLine + Right(Hex(val + &H100), 2)

        if *2 OR (i = binlength)) then
            ' sum = 0
            ' for j = 3 to Len(strOutputLine) step 2
            '   sum = sum + CInt("&H" & mid(strOutputLine, j, 2))
            ' next
            ' sum = &HFF - (sum And &H00FF)
            ' strOutputLine = strOutputLine & right(hex(sum + &H100), 2) 'checksum
            sum = calculateChecksum(mid(strOutputLine,3, len(strOutputLine)-2))
            strOutputLine = strOutputLine & right(hex(sum + &H100), 2) 'checksum
            ' WScript.Echo strOutputLine
            srMot.WriteText strOutputLine, 1 'Write Line
        end if
    next

    'FOOTER
    val = CInt(binlength / linebytes)
    if (val <= 65535) then
        strOutputLine = "S503"
        strOutputline = strOutputLine & right(hex(val + &H10000&), 4)
    else
        strOutputLine = "S605"
        strOutputline = strOutputLine & right(hex(val + &H10000&), 6)
    end if
    sum = calculateChecksum(mid(strOutputLine,3, len(strOutputLine)-2))
    strOutputLine = strOutputLine & right(hex(sum + &H100), 2)
    srMot.WriteText strOutputLine, 1 'Write Line
    ' WScript.Echo strOutputLine

    strOutputLine = "S804000000"
    sum = calculateChecksum(mid(strOutputLine,3, len(strOutputLine)-2))
    strOutputLine = strOutputLine & right(hex(sum + &H100), 2)
    srMot.WriteText strOutputLine, 1 'Write Line

    srMot.SaveToFile filepath_dest, 2
    srMot.Close

    Set srMot = Nothing
   
End function

Function calculateChecksum(targetString)
    Dim i, sum
    'WScript.Echo targetString
    sum = 0
    for i = 1 to Len(targetString) step 2
        sum = sum + CInt("&H" & mid(targetString, i, 2))
    next
    sum = &HFF - (sum And &H00FF)
    calculateChecksum = sum
end function

Dim intArgsCount

' WScript.Echo calculateChecksum("010203")


' intArgsCount = Wscript.Arguments.Count
' If intArgsCount <> 4 Then
'   WScript.Echo "arguments: filepath_source, filepath_dest, address_offset, linebytes"  
'   Wscript.Quit
' End If
 
' Test WScript.Arguments(0), WScript.Arguments(1), WScript.Arguments(2), WScript.Arguments(3)

*1:0 = (i mod linebytes

*2:0 = (i mod linebytes