Python脚本输出的更改取决于我使用的计算机

时间:2018-09-20 19:31:37

标签: python python-2.7 twos-complement

我遇到了一个奇怪的问题,其中相同的脚本输出的结果不同。相同的python版本,相同的库。

Lat=((INPUT_FILE[location[i]+OFFSET+4])<<24|
     (INPUT_FILE[location[i]+OFFSET+5])<<16|
     (INPUT_FILE[location[i]+OFFSET+6])<<8|
     (INPUT_FILE[location[i]+OFFSET+7])<<0)/11930464.71
Long=((INPUT_FILE[location[i]+OFFSET+0])<24|
      (INPUT_FILE[location[i]+OFFSET+1])<16|
      (INPUT_FILE[location[i]+OFFSET+2])<<8|
      (INPUT_FILE[location[i]+OFFSET+3])<<0)/11930464.71
print(Long)

基本上我要做的就是从文件中读取字节,然后使用数学将其转换为坐标。在Windows计算机上,它正确地解释为二进制补码负数,而在Linux计算机上,它输出的值就像是一个无符号数。好像是Q字D字问题。

import argparse 
import os       
import stat     
import decimal
import sys      
import struct   
import numpy as np 
import re 
import binascii 


in_file   = None
log_file  = None
logging   = False
FS_MAP    = None
PAGE_SIZE = 0x20000
count=0
INPUT_FILE=[]
DirectoryList=""
LIMIT=0


def PreviousDestinatioins ():
    print("SCANNING for the Previous Destinations Stored in the pers_NaviControllerLastDestinationsList")
    #So lets deal with the first format found in the logs
    RegEx = re.compile(binascii.unhexlify("00000000")+b"[\x05-\x0f]"+binascii.unhexlify("00010102"))
    location = [m.start(0) for m in RegEx.finditer(INPUT_FILE)]
    ARRAY=[]
    f.write("<Folder><name>Previous Destinations</name>\n")
    for i in range(0,len(location)):
        OFFSET=9
        Tableoffset=1
        print(" ")
        STATE=""
        CITY=""
        ZIPCODE=""
        ROAD=""
        StreetNumber=""
        BusinessName=""
        while(Tableoffset!=0x7c12):
            TEXT="" 
            while INPUT_FILE[location[i]+OFFSET]>31:
                TEXT=TEXT+chr(INPUT_FILE[location[i]+OFFSET])
                OFFSET=OFFSET+1
            if Tableoffset==1:
                STATE=TEXT
            if Tableoffset==0x20215:
                OFFSET=OFFSET+20
            if Tableoffset==0x201:
                CITY=TEXT
            if Tableoffset==0x601:
                ZIPCODE=TEXT
            if Tableoffset==0x301:
                ROAD=TEXT
            if Tableoffset==0x501:
                StreetNumber=TEXT
            if Tableoffset==0x11501:
                BusinessName=TEXT
            Tableoffset=INPUT_FILE[location[i]+OFFSET]<<24|INPUT_FILE[location[i]+OFFSET+1]<<16|INPUT_FILE[location[i]+OFFSET+2]<<8|INPUT_FILE[location[i]+OFFSET+3]
            OFFSET=OFFSET+5
        address=BusinessName+" "+StreetNumber+" "+ROAD+" "+CITY+" "+STATE+", "+ZIPCODE
        print(address)
        OFFSET=OFFSET-1
        Lat=((INPUT_FILE[location[i]+OFFSET+4])<<24|(INPUT_FILE[location[i]+OFFSET+5])<<16|(INPUT_FILE[location[i]+OFFSET+6])<<8|(INPUT_FILE[location[i]+OFFSET+7])<<0)/11930464.71
        Long=((INPUT_FILE[location[i]+OFFSET+0])<<24|(INPUT_FILE[location[i]+OFFSET+1])<<16|(INPUT_FILE[location[i]+OFFSET+2])<<8|(INPUT_FILE[location[i]+OFFSET+3])<<0)/11930464.71
        print(str(((INPUT_FILE[location[i]+OFFSET+0])<<24|(INPUT_FILE[location[i]+OFFSET+1])<<16|(INPUT_FILE[location[i]+OFFSET+2])<<8|(INPUT_FILE[location[i]+OFFSET+3])<<0)/11930464.71))
        if ((25 <= Lat <= 50) and (-125 <= Long <= -65)):
                print("Lat %f"%Lat+"      Long %f"%Long)
                f.write("<Placemark><Style><IconStyle><scale>2.0</scale><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-blank.png</href></Icon></IconStyle><LabelStyle><scale>.5</scale></LabelStyle></Style><name><![CDATA["+address+"]]></name><Point><altitudeMode>clampToGround </altitudeMode><extrude>0</extrude><coordinates>"+str(Long)+","+str(Lat)+",0</coordinates></Point></Placemark>\n")
    f.write("</Folder>\n")
    return

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='')
    parser.add_argument('-i', '--input', help='Process this input file.', dest='in_file', action='store')

    opts = parser.parse_args()

    if opts.in_file is None:
        print("No input file.")
        parser.print_help()
        exit(-1)

    INPUT_FILE = np.memmap(opts.in_file, mode='r')
    FILESIZE=len(INPUT_FILE)
    resultstable=[]
    f=open("RecoveredGPSData.kml","w")
    f.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\"      xmlns:gx=\"http://www.google.com/kml/ext/2.2\"> <Document><name>Recovered GPS Data</name>\n")
    print("Filesize is %d"%FILESIZE)
    print("Note that this script needs to be modified if looking for coordinates outside of the contential US\n\n\n")
    print("This script can take up to five minutes.")
    PreviousDestinatioins ()
    f.write("</Document></kml>")    

0 个答案:

没有答案