Board logo

标题: 如何将txt转成bin文件? [打印本页]

作者: appeal     时间: 2007-9-19 15:41    标题: 如何将txt转成bin文件?

利用Python 2.5加下面的程序,把bin转成了txt文件,但是修改后不知道应该怎么转回bin文件.请高手帮忙.



# strings_bin_converter.py (c) 2006 Stefan Reutter (alias alpaca)
# Version 0.72
# A script to convert a number of MTW2 .strings.bin files to their .txt counterparts

import sys
import codecs
import struct
import os

def convertFile(filepath):
    """
    Takes a type 2 .strings.bin file and converts it to a text file
    """
    f = open(filepath+'.strings.bin', 'rb')
    (style1,) = struct.unpack('h',f.read(2))
    (style2,) = struct.unpack('h', f.read(2))
    if style1 == 2 and style2 == 2048:
        fw = codecs.open(filepath, encoding='UTF-16', mode='w')
        fw.write(unicode(struct.pack('HH',0xFFFE,0xAC00),'UTF-16')+'\r\n')
        (length,) = struct.unpack('i',f.read(4))
        for string in range(length):
            (strlen,) = struct.unpack('h', f.read(2))
            tagstr = ''
            for i in range(strlen):
                tagstr += unicode(f.read(2), 'UTF-16')
            (strlen,) = struct.unpack('h', f.read(2))
            screenstr = ''
            for i in range(strlen):
                tempstr = f.read(2)
                (e,) = struct.unpack('H',tempstr)
                if e == 10:
                    screenstr += '\\n'
                else:
                    screenstr += unicode(tempstr, 'UTF-16')
            fw.write('{'+tagstr+'}'+screenstr+'\r\n')
        fw.close()
    f.close()

if sys.argv[1] == 'all':
    arr = []
    i = 1
    for filepath in os.listdir(''):
        if filepath.find('.strings.bin') > -1:
            arr.append(filepath.split('.strings.bin')[0])
    for filepath in arr:
        print 'Converting file '+filepath+'('+str(i)+' of '+str(len(arr))+')'
        convertFile(filepath)
        i+=1
else:
    for i in range(len(sys.argv)-1):
        path = sys.argv[i+1]
        convertFile(path)








[ Last edited by appeal on 2007-9-19 at 05:29 PM ]
作者: electronixtar     时间: 2007-9-19 16:43
python也能到这里提问,不错不错很有前途。

请问楼主这里的Python是否DGJPP在DOS下的编译版本?
作者: appeal     时间: 2007-9-19 17:33
又上传了3个贴图,高手这回能理解了吗?我把txt.strings.bin用python和那个小程序转成了txt文件,给游戏做了汉化,现在想转回去txt.strings.bin,不知道该怎么办.
作者: lp1129     时间: 2007-9-19 23:06
贴图可以直接以附件上传的,不用外链
作者: lp1129     时间: 2007-9-19 23:09
可以肯定,转回BIN格式基本上就不能用了,这样做汉化,不太好用
作者: appeal     时间: 2007-9-19 23:45
就算这样,翻译内容是固定的,也算没白翻译.请问这里面的txt.strings.bin和txt要怎么相互转换?为什么转回去就不能用了呢?
作者: jackwu     时间: 2007-9-28 10:35
看不懂