使用R编辑二进制文件

时间:2018-04-25 12:17:30

标签: c r shiny binaryfiles

我需要使用R编辑现有的二进制文件,可以在此处下载二进制文件:https://www.dropbox.com/s/1a4mb6pj5heju4t/CAMTRAP.CTC?dl=0

原始文件是用C语言编写的,结构如下:

typedef struct  {
 int8 RTCMonth; // RTC properties used to set camera clock
 int8 RTCDay;
 int8 RTCHour;
 int8 RTCMin;
 int8 RTCSec;
 int16 RTCYear;
 int8 setRTC; // set = 1 to set the RTC using the RTC properties
 int8 softwareVersion[51];
 int16 flashImagesPerTrigger;
 int16 daylightImagesPerTrigger;
 int16 delayBetweenImagesFlash;
 float delayBetweenImagesDaylight;
 int32 imageCounter;
 int8  clearImageCounter; // set = 1 to clear image counter
 int16 SPARE0;
 int8 cameraID[9];
 int8 usbBackupMethod; // 0=FULL, 1=INCREMENTAL, 2=FULL PLUS DELETE
 int16 flashImageBrightness; // 0=NORMAL, 1=BRIGHT, 2=BRIGHTEST
 int8  RESERVED1[32];
 int8  RESERVED2;
 int8 logFileExtention[3];
 int8 SPARE1[3];
 int8 RESERVED3;
 float flashCrossoverCdS; 
 int8  setFlashCrossoverCdS; // = 1 to set
 float SPARE3;   
 int8  SPARE4;
 float AspectRatio;    
 int8 setAspectRatio; // = 1 to set
 int8 derivativeClipNormal;  
 int8 setDerivativeClipNormal; // = 1 to set
 int8 purgeLogFile; // = 1 to purge log file
 int8 SPARE5; 
 float chargeFlashTarget;   
 int8 setChargeFlashTarget; // = 1 to set
 int8 jpegCompressionRatio; // 12=LOW, 24=MEDIUM, 40=HIGH
 int8 setJpegCompressionRatio; // = 1 to set
 int8 setExifComment; // = 1 to set
 int8 exifComment[50];
 //GPS parms and location data 
 int8 setRTCFromGPS; // = 1 to set
 int8  RESERVED4;
 int16 utcOffsetHours; // local UTC time adjustment
 int16 utcOffsetMinutes;
 int8 adjustRtcForDaylightSavings; // set = 1 to enable
 int8 daylightSavingsBeginMonth;
 int8 daylightSavingsBeginDay;
 int8 daylightSavingsEndMonth;
 int8 daylightSavingsEndDay;
 int8 clearGpsData; // set = 1 to clear GpsData
 int32  latitudeRef;
 int32  latitudeDegrees;
 int32  latitudeMinutes;
 int32  latitudeSeconds;
 int32  longitudeRef;
 int32  longitudeDegrees;
 int32  longitudeMinutes; 
 int32  longitudeSeconds; 
 int8   altitudeRef; 
 int32  altFromMSL; 
 int8   RESERVED5;
 int8   setGpsLocation;  // = 1 to set
 int8   encryptJpgFiles; // set = 1 to enable
 int8   RESERVED6;
 float  bootLoaderVersion;
 int8   powerLock; // set = 1 to enable
 int8   stealthLedMode; // 0=OFF, 1=No LED indications after initialization, 2=No LED's, extended day, 3=NoLEDs, Extended Day, No Flash Pics
 int8   RESERVED7;
 float RESERVED8;
 float RESERVED9; 
 int32 V4SPARE0[2];
 int8  sdCardCleanup; // = 1 to set
 int8  enableTrafficBlocking; // = 1 to set
 int8  V4SPARE[22];
 int8  serverHostname[40];
 int8  serverPort[6];
 int8  networkAPN[40];
 int8  apnUserID[16];
 int8  apnPassword[16];
 int8  apnOperNum[9];
 int8  hardwareTag[10];
 int16 HourForLogUpload; // 0-23 (set > 23 to disable)
 int8  FlashlightMode; // = 1 to set
 int16 MaxDailyImageUploads;
 int8  disableVehicleDetection; // = 1 to set 
 int8  disableHumanDetection; // = 1 to set
 int8  initialLEDactiveMinutes;
 int32 RESERVED10;
 int8  synchCameraClockWithServer; // = 1 to set  
 int8  sendImageWithLogFileUpload; // = 1 to set
 int8  RESERVED11[545];
}SYS_CONFIG; //1024bytes

我对二进制文件并不熟悉,但根据作者的说法,成员是按照默认成员对齐方式布置的小端(即,有一个填充字节以保持“短”结构成员正确对齐在2字节边界上,这样每当“char”后面的“int”或“short”插入填充字节以保持正确对齐时。

我基本上写了一个Shiny应用程序,它允许用户编辑自己的二进制文件(遵循与示例中相同的结构)。唯一的麻烦是我没有运气这个二进制文件的运气。到目前为止,我已经尝试过使用readBin和writeBin,虽然readBin允许我查看文件的某些方面,但我似乎无法弄清楚这些数据的模式。

我不是在寻找有人为我编写代码,但请有人协助解释如何使用R编辑此特定二进制文件的某些方面。例如,假设我想改变“exifComment”以包含一些随机文本。如何找到二进制文件的该部分,然后应用并保存新数据?这在R?中是否可行?

提前致谢!

PS - 如果我遗漏了任何重要信息,我会道歉,但这就是我必须要处理的事情。

0 个答案:

没有答案