CaluConvert.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. #include "StdAfx.h"
  2. #include ".\caluconvert.h"
  3. BYTE *g_pSource = NULL; // 全局,用于动态创建内存;
  4. CCaluConvert::CCaluConvert(void)
  5. {
  6. }
  7. CCaluConvert::~CCaluConvert(void)
  8. {
  9. }
  10. void CCaluConvert::CONVERTS(
  11. char *szDestValue,
  12. const int &DestLen,
  13. const BYTE *szSourceValue,
  14. const int &SourceLen,
  15. const int &iIndex,
  16. const int &iCutLen,
  17. const int &iSBit,
  18. const int &iEBit,
  19. const double &dConfs,
  20. const double &dCrrect,
  21. const DWORD &dwCaluType)
  22. {
  23. if (dwCaluType/10000 != iCutLen)
  24. {
  25. sprintf_s(szDestValue,DestLen,"%s","截取长度与类型不匹配!");
  26. return;
  27. }
  28. int iBit_Over = iCutLen*8 -1;
  29. if ( iSBit > iBit_Over || iEBit > iBit_Over)
  30. {
  31. sprintf_s(szDestValue,DestLen,"%s","起始位与终止位超过有效位界限!");
  32. return;
  33. }
  34. if ( iSBit > iEBit)
  35. {
  36. sprintf_s(szDestValue,DestLen,"%s","起始位高于终止位!");
  37. return;
  38. }
  39. g_pSource = new BYTE[iCutLen];
  40. memcpy(g_pSource,szSourceValue + iIndex, iCutLen);
  41. switch(iCutLen)
  42. {
  43. case BYTE_1:
  44. _CONVERTORDER_1BYTE(szDestValue,DestLen, g_pSource, iSBit, iEBit, dConfs,dCrrect, dwCaluType);
  45. break;
  46. case BYTE_2:
  47. _CONVERTORDER_2BYTE(szDestValue,DestLen, g_pSource, iSBit, iEBit, dConfs,dCrrect, dwCaluType);
  48. break;
  49. case BYTE_3:
  50. _CONVERTORDER_3BYTE(szDestValue,DestLen, g_pSource, iSBit, iEBit, dConfs,dCrrect, dwCaluType);
  51. break;
  52. case BYTE_4:
  53. _CONVERTORDER_4BYTE(szDestValue,DestLen, g_pSource, iSBit, iEBit, dConfs,dCrrect, dwCaluType);
  54. break;
  55. case BYTE_5:
  56. _CONVERTORDER_5BYTE(szDestValue,DestLen, g_pSource, iSBit, iEBit, dConfs,dCrrect, dwCaluType);
  57. break;
  58. case BYTE_6:
  59. _CONVERTORDER_6BYTE(szDestValue,DestLen, g_pSource, iSBit, iEBit, dConfs,dCrrect, dwCaluType);
  60. break;
  61. case BYTE_7:
  62. _CONVERTORDER_7BYTE(szDestValue,DestLen, g_pSource, iSBit, iEBit, dConfs,dCrrect, dwCaluType);
  63. break;
  64. case BYTE_8:
  65. _CONVERTORDER_8BYTE(szDestValue,DestLen, g_pSource, iSBit, iEBit, dConfs,dCrrect, dwCaluType);
  66. break;
  67. default: // 默认->字符串,直接返回原串;
  68. break;
  69. }
  70. //end delete;
  71. delete []g_pSource;
  72. g_pSource = NULL;
  73. // 校正值(放在各函数内部,还是在此处理更优化); 做一个内联函数;
  74. ReturnCorrectValue(szDestValue,DestLen,dConfs,dCrrect);
  75. }
  76. // 1byte
  77. void CCaluConvert::_CONVERTORDER_1BYTE(char *&szValue,const int &VLen,BYTE *&szSource,const int &iSBit,const int &iEBit,const double &dConfs,const double &dCorrect,const DWORD &dwCaluType)
  78. {
  79. switch(dwCaluType)
  80. {
  81. case BYTE_1_RealBit:
  82. //_BYTE_1_RealBit(szValue, VLen, szSource, iSBit,iEBit);
  83. RealBit(szValue,VLen,szSource,BYTE_1,iSBit,iEBit);
  84. break;
  85. case BYTE_1_VirtBit:
  86. //_BYTE_1_VirtBit(szValue, VLen, szSource, iSBit,iEBit);
  87. VirtBit(szValue,VLen,szSource,BYTE_1,iSBit,iEBit);
  88. break;
  89. case BYTE_1_Literal: // 返回本身,不作任何处理;
  90. //_BYTE_1_Literal(szValue, VLen, szSource);
  91. Literal(szValue,szSource,1);
  92. break;
  93. case BYTE_1_Decimal:
  94. _BYTE_1_Decimal(szValue,VLen,szSource,dConfs);
  95. break;
  96. case BYTE_1_Literal_Decimal:
  97. //_BYTE_1_Literal_Decimal(szValue,VLen,szSource,dConfs);
  98. Literal_Decimal(szValue,VLen,szSource,dConfs);
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. // 2byte
  105. void CCaluConvert::_CONVERTORDER_2BYTE(char *&szValue,const int &VLen,BYTE *&szSource,const int &iSBit,const int &iEBit,const double &dConfs,const double &dCorrect,const DWORD &dwCaluType)
  106. {
  107. switch(dwCaluType)
  108. {
  109. case BYTE_2_RealBit:
  110. //_BYTE_2_RealBit(szValue, VLen, szSource, iSBit, iEBit);
  111. RealBit(szValue,VLen,szSource,BYTE_2,iSBit,iEBit);
  112. break;
  113. case BYTE_2_VirtBit:
  114. //_BYTE_2_VirtBit(szValue, VLen, szSource, iSBit,iEBit);
  115. VirtBit(szValue,VLen,szSource,BYTE_2,iSBit,iEBit);
  116. break;
  117. case BYTE_2_Literal:
  118. //_BYTE_2_Literal(szValue, VLen, szSource);
  119. Literal(szValue,szSource,2);
  120. break;
  121. case BYTE_2_Decimal:
  122. _BYTE_2_Decimal(szValue, VLen, szSource,dConfs);
  123. break;
  124. case BYTE_2_Literal_Decimal:
  125. //_BYTE_1_Literal_Decimal(szValue,VLen,szSource,dConfs);
  126. Literal_Decimal(szValue,VLen,szSource,dConfs);
  127. break;
  128. case BYTE_2_MSByte:
  129. _BYTE_2_MSByte(szValue, VLen, szSource,dConfs);
  130. break;
  131. case BYTE_2_LSByte:
  132. _BYTE_2_LSByte(szValue, VLen, szSource,dConfs);
  133. break;
  134. case BYTE_2_1_RealBit:
  135. _BYTE_2_1_RealBit(szValue,VLen,szSource,iSBit,iEBit);
  136. break;
  137. case BYTE_2_1_VirtBit:
  138. _BYTE_2_1_VirtBit(szValue,VLen,szSource,iSBit,iEBit);
  139. break;
  140. case BYTE_2_1_Literal:
  141. _BYTE_2_1_Literal(szValue,VLen,szSource);
  142. break;
  143. case BYTE_2_1_Decimal:
  144. _BYTE_2_1_Decimal(szValue,VLen,szSource,dConfs);
  145. break;
  146. case BYTE_2_1_Literal_Decimal:
  147. _BYTE_2_1_Literal_Decimal(szValue,VLen,szSource,dConfs);
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. // 3byte
  154. void CCaluConvert::_CONVERTORDER_3BYTE(char *&szValue,const int &VLen,BYTE *&szSource,const int &iSBit,const int &iEBit,const double &dConfs,const double &dCorrect,const DWORD &dwCaluType)
  155. {
  156. switch(dwCaluType)
  157. {
  158. case BYTE_3_RealBit:
  159. //_BYTE_3_RealBit(szValue, VLen, szSource, iSBit, iEBit);
  160. RealBit(szValue,VLen,szSource,BYTE_3,iSBit,iEBit);
  161. break;
  162. case BYTE_3_VirtBit:
  163. //_BYTE_3_VirtBit(szValue, VLen, szSource, iSBit, iEBit);
  164. VirtBit(szValue,VLen,szSource,BYTE_3,iSBit,iEBit);
  165. break;
  166. case BYTE_3_Literal:
  167. Literal(szValue,szSource,3);
  168. break;
  169. case BYTE_3_Decimal:
  170. _BYTE_3_Decimal(szValue,VLen,szSource,dConfs);
  171. break;
  172. case BYTE_3_Literal_Decimal:
  173. Literal_Decimal(szValue,VLen,szSource,dConfs);
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. // 4byte
  180. void CCaluConvert::_CONVERTORDER_4BYTE(char *&szValue,const int &VLen,BYTE *&szSource,const int &iSBit,const int &iEBit,const double &dConfs,const double &dCorrect,const DWORD &dwCaluType)
  181. {
  182. switch(dwCaluType)
  183. {
  184. case BYTE_4_RealBit:
  185. //_BYTE_4_RealBit(szValue, VLen, szSource, iSBit, iEBit);
  186. RealBit(szValue,VLen,szSource,BYTE_4,iSBit,iEBit);
  187. break;
  188. case BYTE_4_VirtBit:
  189. //_BYTE_4_VirtBit(szValue, VLen, szSource, iSBit, iEBit);
  190. VirtBit(szValue,VLen,szSource,BYTE_4,iSBit,iEBit);
  191. break;
  192. case BYTE_4_Literal:
  193. Literal(szValue,szSource,4);
  194. break;
  195. case BYTE_4_Decimal:
  196. _BYTE_4_Decimal(szValue, VLen, szSource,dConfs);
  197. break;
  198. case BYTE_4_Literal_Decimal:
  199. Literal_Decimal(szValue,VLen,szSource,dConfs);
  200. break;
  201. case BYTE_4_Float:
  202. _BYTE_4_Float(szValue, VLen, szSource);
  203. default:
  204. break;
  205. }
  206. }
  207. // 5byte
  208. void CCaluConvert::_CONVERTORDER_5BYTE(char *&szValue,const int &VLen,BYTE *&szSource,const int &iSBit,const int &iEBit,const double &dConfs,const double &dCorrect,const DWORD &dwCaluType)
  209. {
  210. switch(dwCaluType)
  211. {
  212. case BYTE_5_RealBit:
  213. //_BYTE_5_RealBit(szValue, VLen, szSource, iSBit, iEBit);
  214. RealBit(szValue,VLen,szSource,BYTE_5,iSBit,iEBit);
  215. break;
  216. case BYTE_5_VirtBit:
  217. //_BYTE_5_VirtBit(szValue, VLen, szSource, iSBit, iEBit);
  218. VirtBit(szValue,VLen,szSource,BYTE_5,iSBit,iEBit);
  219. break;
  220. case BYTE_5_Literal:
  221. Literal(szValue, szSource,5);
  222. break;
  223. case BYTE_5_Decimal:
  224. _BYTE_5_Decimal(szValue, VLen, szSource,dConfs);
  225. break;
  226. case BYTE_5_Literal_Decimal:
  227. Literal_Decimal(szValue,VLen,szSource,dConfs);
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. // 6byte
  234. void CCaluConvert::_CONVERTORDER_6BYTE(char *&szValue,const int &VLen,BYTE *&szSource,const int &iSBit,const int &iEBit,const double &dConfs,const double &dCorrect,const DWORD &dwCaluType)
  235. {
  236. switch(dwCaluType)
  237. {
  238. case BYTE_6_RealBit:
  239. //_BYTE_6_RealBit(szValue, VLen, szSource, iSBit, iEBit);
  240. RealBit(szValue,VLen,szSource,BYTE_6,iSBit,iEBit);
  241. break;
  242. case BYTE_6_VirtBit:
  243. //_BYTE_6_VirtBit(szValue, VLen, szSource, iSBit, iEBit);
  244. VirtBit(szValue,VLen,szSource,BYTE_6,iSBit,iEBit);
  245. break;
  246. case BYTE_6_Literal:
  247. Literal(szValue, szSource,6);
  248. break;
  249. case BYTE_6_Decimal:
  250. _BYTE_6_Decimal(szValue, VLen, szSource,dConfs);
  251. break;
  252. case BYTE_6_Literal_Decimal:
  253. Literal_Decimal(szValue,VLen,szSource,dConfs);
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. // 7byte
  260. void CCaluConvert::_CONVERTORDER_7BYTE(char *&szValue,const int &VLen,BYTE *&szSource,const int &iSBit,const int &iEBit,const double &dConfs,const double &dCorrect,const DWORD &dwCaluType)
  261. {
  262. switch(dwCaluType)
  263. {
  264. case BYTE_7_RealBit:
  265. //_BYTE_7_RealBit(szValue, VLen, szSource, iSBit, iEBit);
  266. RealBit(szValue,VLen,szSource,BYTE_7,iSBit,iEBit);
  267. break;
  268. case BYTE_7_VirtBit:
  269. //_BYTE_7_VirtBit(szValue, VLen, szSource, iSBit, iEBit);
  270. VirtBit(szValue,VLen,szSource,BYTE_7,iSBit,iEBit);
  271. break;
  272. case BYTE_7_Literal:
  273. Literal(szValue, szSource,7);
  274. break;
  275. case BYTE_7_Decimal:
  276. _BYTE_7_Decimal(szValue, VLen, szSource,dConfs);
  277. break;
  278. case BYTE_7_Literal_Decimal:
  279. Literal_Decimal(szValue,VLen,szSource,dConfs);
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. // 8byte
  286. void CCaluConvert::_CONVERTORDER_8BYTE(char *&szValue,const int &VLen, BYTE *&szSource,const int &iSBit,const int &iEBit,const double &dConfs,const double &dCorrect,const DWORD &dwCaluType)
  287. {
  288. switch(dwCaluType)
  289. {
  290. case BYTE_8_RealBit:
  291. //_BYTE_8_RealBit(szValue, VLen, szSource, iSBit, iEBit);
  292. RealBit(szValue,VLen,szSource,BYTE_8,iSBit,iEBit);
  293. break;
  294. case BYTE_8_VirtBit:
  295. //_BYTE_8_VirtBit(szValue, VLen, szSource, iSBit, iEBit);
  296. VirtBit(szValue,VLen,szSource,BYTE_8,iSBit,iEBit);
  297. break;
  298. case BYTE_8_Literal:
  299. Literal(szValue, szSource,8);
  300. break;
  301. case BYTE_8_Decimal:
  302. _BYTE_8_Decimal(szValue, VLen, szSource,dConfs);
  303. break;
  304. case BYTE_8_Literal_Decimal:
  305. Literal_Decimal(szValue,VLen,szSource,dConfs);
  306. break;
  307. case BYTE_8_4_Float:
  308. _BYTE_8_4_Float(szValue,VLen,szSource);
  309. break;
  310. default:
  311. break;
  312. }
  313. }
  314. /************************************************************************/
  315. void CCaluConvert::RealBit(char *szValue, const int &VLen,BYTE *szSource, const int &SLen, const int &iSBit,const int &iEBit)
  316. {
  317. string strBinary;
  318. for( int i = 0; i < SLen; i++)
  319. strBinary += ReturnBinaryReverseString(szSource[i]);
  320. switch(SLen)
  321. {
  322. case BYTE_1:
  323. GetRealBitValue<BYTE_1>(szValue,VLen,strBinary,iSBit,iEBit);
  324. break;
  325. case BYTE_2:
  326. GetRealBitValue<BYTE_2>(szValue,VLen,strBinary,iSBit,iEBit);
  327. break;
  328. case BYTE_3:
  329. GetRealBitValue<BYTE_3>(szValue,VLen,strBinary,iSBit,iEBit);
  330. break;
  331. case BYTE_4:
  332. GetRealBitValue<BYTE_4>(szValue,VLen,strBinary,iSBit,iEBit);
  333. break;
  334. case BYTE_5:
  335. GetRealBitValue<BYTE_5>(szValue,VLen,strBinary,iSBit,iEBit);
  336. break;
  337. case BYTE_6:
  338. GetRealBitValue<BYTE_6>(szValue,VLen,strBinary,iSBit,iEBit);
  339. break;
  340. case BYTE_7:
  341. GetRealBitValue<BYTE_7>(szValue,VLen,strBinary,iSBit,iEBit);
  342. break;
  343. case BYTE_8:
  344. GetRealBitValue<BYTE_8>(szValue,VLen,strBinary,iSBit,iEBit);
  345. break;
  346. }
  347. }
  348. void CCaluConvert::VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &SLen, const int &iSBit, const int &iEBit)
  349. {
  350. string strBinary;
  351. for( int i = 0; i < SLen; i++)
  352. //strBinary += ReturnBinaryReverseString(szSource[i]);
  353. strBinary += ReturnBinaryString(szSource[SLen-i-1]);
  354. switch(SLen)
  355. {
  356. case BYTE_1:
  357. GetVirtBitValue<BYTE_1>(szValue,VLen,strBinary,iSBit,iEBit);
  358. break;
  359. case BYTE_2:
  360. GetVirtBitValue<BYTE_2>(szValue,VLen,strBinary,iSBit,iEBit);
  361. break;
  362. case BYTE_3:
  363. GetVirtBitValue<BYTE_3>(szValue,VLen,strBinary,iSBit,iEBit);
  364. break;
  365. case BYTE_4:
  366. GetVirtBitValue<BYTE_4>(szValue,VLen,strBinary,iSBit,iEBit);
  367. break;
  368. case BYTE_5:
  369. GetVirtBitValue<BYTE_5>(szValue,VLen,strBinary,iSBit,iEBit);
  370. break;
  371. case BYTE_6:
  372. GetVirtBitValue<BYTE_6>(szValue,VLen,strBinary,iSBit,iEBit);
  373. break;
  374. case BYTE_7:
  375. GetVirtBitValue<BYTE_7>(szValue,VLen,strBinary,iSBit,iEBit);
  376. break;
  377. case BYTE_8:
  378. GetVirtBitValue<BYTE_8>(szValue,VLen,strBinary,iSBit,iEBit);
  379. break;
  380. }
  381. }
  382. //***************************1 字节*****************************************
  383. void CCaluConvert::_BYTE_1_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  384. {
  385. int iSour = szSource[0];
  386. // 位顺序: 高->低 (7~0);
  387. bitset<8> bitSor(iSour);
  388. char szBinary[9] = {0};
  389. for ( int i = 0; i < 8; i++)
  390. szBinary[i] = bitSor[i];
  391. if ( iSBit == iEBit)
  392. {
  393. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  394. return;
  395. }
  396. else
  397. {
  398. for (int i = 0; i < 8; i++)
  399. if ( i < iSBit || i > iEBit)
  400. bitSor.reset(i);
  401. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  402. }
  403. }
  404. void CCaluConvert::_BYTE_1_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  405. {
  406. int iSour = szSource[0];
  407. // 位顺序: 高->低 (7~0);
  408. bitset<8> bitSor(iSour);
  409. char szBinary[9] = {0};
  410. for ( int i = 0; i < 8; i++)
  411. szBinary[i] = bitSor[i];
  412. iSour = 0;
  413. if ( iSBit == iEBit)
  414. {
  415. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  416. return;
  417. }
  418. else
  419. {
  420. for (int i = 0; i <= iEBit - iSBit; i++)
  421. iSour += szBinary[iSBit+i] * pow(2.0,i);
  422. _itoa_s(iSour,szValue,VLen,10);
  423. }
  424. }
  425. void CCaluConvert::_BYTE_1_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  426. {
  427. double dRet = szSource[0];
  428. sprintf_s(szValue,VLen,"%f",dRet);
  429. }
  430. //***************************2 字节*****************************************
  431. void CCaluConvert::_BYTE_2_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  432. {
  433. // 高在前,低在后;
  434. WORD wValue = ReturnASCIIWORD(szSource);
  435. bitset<16> bitSor(wValue);
  436. char szBinary[17] = {0};
  437. for ( int i = 0; i < 16; i++)
  438. szBinary[i] = bitSor[i];
  439. if ( iSBit == iEBit)
  440. {
  441. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  442. return;
  443. }
  444. else
  445. {
  446. for (int i = 0; i < 16; i++)
  447. if ( i < iSBit || i > iEBit)
  448. bitSor.reset(i);
  449. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  450. }
  451. }
  452. void CCaluConvert::_BYTE_2_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  453. {
  454. // 高在前,低在后;
  455. WORD wValue = ReturnASCIIWORD(szSource);
  456. bitset<16> bitSor(wValue);
  457. char szBinary[17] = {0};
  458. for ( int i = 0; i < 16; i++)
  459. szBinary[i] = bitSor[i];
  460. if ( iSBit == iEBit)
  461. {
  462. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  463. return;
  464. }
  465. else
  466. {
  467. int iSour = 0;
  468. for (int i = 0; i <= iEBit - iSBit; i++)
  469. iSour += szBinary[iSBit+i] * pow(2.0,i);
  470. _itoa_s(iSour,szValue,VLen,10);
  471. }
  472. }
  473. void CCaluConvert::_BYTE_2_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  474. {
  475. // 高在前,低在后;
  476. WORD wValue = ReturnASCIIWORD(szSource);
  477. sprintf_s(szValue,VLen,"%d",wValue);
  478. }
  479. void CCaluConvert::_BYTE_2_MSByte(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  480. {
  481. BYTE byValue = szSource[0];
  482. sprintf_s(szValue,VLen,"%f",byValue);
  483. }
  484. void CCaluConvert::_BYTE_2_LSByte(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  485. {
  486. BYTE byValue = szSource[1];
  487. sprintf_s(szValue,VLen,"%f",byValue);
  488. }
  489. void CCaluConvert::_BYTE_2_1_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  490. {
  491. // 高在前,低在后;
  492. BYTE byValue = ReturnASCIIByte(szSource);
  493. bitset<8> bitSor(byValue);
  494. char szBinary[9] = {0};
  495. for ( int i = 0; i < 8; i++)
  496. szBinary[i] = bitSor[i];
  497. if ( iSBit == iEBit)
  498. {
  499. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  500. return;
  501. }
  502. else
  503. {
  504. for (int i = 0; i < 8; i++)
  505. if ( i < iSBit || i > iEBit)
  506. bitSor.reset(i);
  507. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  508. }
  509. }
  510. void CCaluConvert::_BYTE_2_1_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  511. {
  512. // 高在前,低在后;
  513. BYTE byValue = ReturnASCIIByte(szSource);
  514. bitset<8> bitSor(byValue);
  515. //bitset<8> bitSor(*szSource);
  516. char szBinary[9] = {0};
  517. for ( int i = 0; i < 8; i++)
  518. szBinary[i] = bitSor[i];
  519. int iSour = 0;
  520. if ( iSBit == iEBit)
  521. {
  522. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  523. return;
  524. }
  525. else
  526. {
  527. for (int i = 0; i <= iEBit - iSBit; i++)
  528. iSour += szBinary[iSBit+i] * pow(2.0,i);
  529. _itoa_s(iSour,szValue,VLen,10);
  530. }
  531. }
  532. void CCaluConvert::_BYTE_2_1_Literal(char *szValue, const int &VLen, BYTE *szSource)
  533. {
  534. BYTE byValue = ReturnASCIIByte(szSource);
  535. szValue[0] = byValue;
  536. //memcpy(szValue,&byValue,BYTE_1);
  537. }
  538. void CCaluConvert::_BYTE_2_1_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  539. {
  540. BYTE byValue = ReturnASCIIByte(szSource);
  541. double dRet = byValue;
  542. sprintf_s(szValue,VLen,"%f",dRet);
  543. }
  544. void CCaluConvert::_BYTE_2_1_Literal_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  545. {
  546. BYTE byValue = ReturnASCIIByte(szSource);
  547. double dRet = atof((char*)&byValue);
  548. sprintf_s(szValue,VLen,"%f",dRet);
  549. }
  550. //***************************3 字节*****************************************
  551. void CCaluConvert::_BYTE_3_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  552. {
  553. // 高在前,低在后;
  554. DWORD dwValue = szSource[0] << 16 | szSource[1] << 8 | szSource[2];
  555. bitset<32> bitSor(dwValue);
  556. char szBinary[33] = {0};
  557. for ( int i = 0; i < 32; i++)
  558. szBinary[i] = bitSor[i];
  559. if ( iSBit == iEBit)
  560. {
  561. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  562. return;
  563. }
  564. else
  565. {
  566. for (int i = 0; i < 32; i++)
  567. if ( i < iSBit || i > iEBit)
  568. bitSor.reset(i);
  569. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  570. }
  571. }
  572. void CCaluConvert::_BYTE_3_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  573. {
  574. DWORD dwValue = szSource[0] << 16 | szSource[1] << 8 | szSource[2];
  575. bitset<32> bitSor(dwValue);
  576. char szBinary[33] = {0};
  577. for ( int i = 0; i < 32; i++)
  578. szBinary[i] = bitSor[i];
  579. if ( iSBit == iEBit)
  580. {
  581. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  582. return;
  583. }
  584. else
  585. {
  586. int iSour = 0;
  587. for (int i = 0; i <= iEBit - iSBit; i++)
  588. iSour += szBinary[iSBit+i] * pow(2.0,i);
  589. _itoa_s(iSour,szValue,VLen,10);
  590. }
  591. }
  592. void CCaluConvert::_BYTE_3_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  593. {
  594. DWORD dwValue = szSource[0] << 16 | szSource[1] << 8 | szSource[2];
  595. sprintf_s(szValue,VLen,"%d",dwValue);
  596. }
  597. //***************************4 字节*****************************************
  598. void CCaluConvert::_BYTE_4_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  599. {
  600. DWORD dwValue = ReturnASCIIDWORD(szSource);
  601. bitset<32> bitSor(dwValue);
  602. char szBinary[33] = {0};
  603. for ( int i = 0; i < 32; i++)
  604. szBinary[i] = bitSor[i];
  605. if ( iSBit == iEBit)
  606. {
  607. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  608. return;
  609. }
  610. else
  611. {
  612. for (int i = 0; i < 32; i++)
  613. if ( i < iSBit || i > iEBit)
  614. bitSor.reset(i);
  615. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  616. }
  617. }
  618. void CCaluConvert::_BYTE_4_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  619. {
  620. DWORD dwValue = ReturnASCIIDWORD(szSource);
  621. bitset<32> bitSor(dwValue);
  622. char szBinary[33] = {0};
  623. for ( int i = 0; i < 32; i++)
  624. szBinary[i] = bitSor[i];
  625. if ( iSBit == iEBit)
  626. {
  627. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  628. return;
  629. }
  630. else
  631. {
  632. int iSour = 0;
  633. for (int i = 0; i <= iEBit - iSBit; i++)
  634. iSour += szBinary[iSBit+i] * pow(2.0,i);
  635. _itoa_s(iSour,szValue,VLen,10);
  636. }
  637. }
  638. void CCaluConvert::_BYTE_4_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  639. {
  640. DWORD dwValue = ReturnASCIIDWORD(szSource);
  641. sprintf_s(szValue,VLen,"%d",dwValue);
  642. }
  643. void CCaluConvert::_BYTE_4_Float(char *szValue, const int &VLen,BYTE *szSource)
  644. {
  645. float fValue = *(float*)szSource;
  646. sprintf_s(szValue,VLen,"%f",fValue);
  647. }
  648. //***************************5 字节*****************************************
  649. void CCaluConvert::_BYTE_5_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  650. {
  651. string strBinary;
  652. for( int i = 0; i < BYTE_5; i++)
  653. strBinary += ReturnBinaryReverseString(szSource[i]);
  654. bitset<40> bitSor(strBinary);
  655. if ( iSBit == iEBit)
  656. {
  657. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  658. return;
  659. }
  660. else
  661. {
  662. for (int i = 0; i < 40; i++)
  663. if ( i < iSBit || i > iEBit)
  664. bitSor.reset(i);
  665. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  666. }
  667. }
  668. void CCaluConvert::_BYTE_5_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  669. {
  670. string strBinary;
  671. for( int i = 0; i < BYTE_5; i++)
  672. strBinary += ReturnBinaryReverseString(szSource[i]);
  673. bitset<40> bitSor(strBinary);
  674. int dwSour = 0;
  675. if ( iSBit == iEBit)
  676. {
  677. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  678. return;
  679. }
  680. else
  681. {
  682. string::reverse_iterator rit;
  683. rit = strBinary.rbegin() + iSBit;
  684. for (int i = 0; i <= iEBit - iSBit; i++,rit++)
  685. dwSour += (*rit - 48) * pow(2.0,i);
  686. _itoa_s(dwSour,szValue,VLen,10);
  687. }
  688. }
  689. void CCaluConvert::_BYTE_5_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  690. {
  691. VirtBit(szValue, VLen, szSource, BYTE_5, 0, 8*BYTE_5 -1);
  692. }
  693. //***************************6 字节*****************************************
  694. void CCaluConvert::_BYTE_6_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  695. {
  696. string strBinary;
  697. for( int i = 0; i < BYTE_6; i++)
  698. strBinary += ReturnBinaryReverseString(szSource[i]);
  699. bitset<48> bitSor(strBinary);
  700. if ( iSBit == iEBit)
  701. {
  702. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  703. return;
  704. }
  705. else
  706. {
  707. for (int i = 0; i < 48; i++)
  708. if ( i < iSBit || i > iEBit)
  709. bitSor.reset(i);
  710. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  711. }
  712. }
  713. void CCaluConvert::_BYTE_6_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  714. {
  715. string strBinary;
  716. for( int i = 0; i < BYTE_6; i++)
  717. strBinary += ReturnBinaryReverseString(szSource[i]);
  718. bitset<48> bitSor(strBinary);
  719. int dwSour = 0;
  720. if ( iSBit == iEBit)
  721. {
  722. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  723. return;
  724. }
  725. else
  726. {
  727. string::reverse_iterator rit;
  728. rit = strBinary.rbegin() + iSBit;
  729. for (int i = 0; i <= iEBit - iSBit; i++,rit++)
  730. dwSour += (*rit - 48) * pow(2.0,i);
  731. _itoa_s(dwSour,szValue,VLen,10);
  732. }
  733. }
  734. void CCaluConvert::_BYTE_6_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  735. {
  736. VirtBit(szValue,VLen,szSource,BYTE_6,0,8*BYTE_6-1);
  737. }
  738. //***************************7 字节*****************************************
  739. void CCaluConvert::_BYTE_7_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  740. {
  741. string strBinary;
  742. for( int i = 0; i < BYTE_7; i++)
  743. strBinary += ReturnBinaryReverseString(szSource[i]);
  744. bitset<56> bitSor(strBinary);
  745. if ( iSBit == iEBit)
  746. {
  747. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  748. return;
  749. }
  750. else
  751. {
  752. for (int i = 0; i < 56; i++)
  753. if ( i < iSBit || i > iEBit)
  754. bitSor.reset(i);
  755. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  756. }
  757. }
  758. void CCaluConvert::_BYTE_7_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  759. {
  760. string strBinary;
  761. for( int i = 0; i < BYTE_7; i++)
  762. strBinary += ReturnBinaryReverseString(szSource[i]);
  763. bitset<56> bitSor(strBinary);
  764. int dwSour = 0;
  765. if ( iSBit == iEBit)
  766. {
  767. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  768. return;
  769. }
  770. else
  771. {
  772. string::reverse_iterator rit;
  773. rit = strBinary.rbegin() + iSBit;
  774. for (int i = 0; i <= iEBit - iSBit; i++,rit++)
  775. dwSour += (*rit - 48) * pow(2.0,i);
  776. _itoa_s(dwSour,szValue,VLen,10);
  777. }
  778. }
  779. void CCaluConvert::_BYTE_7_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  780. {
  781. VirtBit(szValue,VLen,szSource,BYTE_7,0,8*BYTE_7-1);
  782. }
  783. //***************************8 字节*****************************************
  784. void CCaluConvert::_BYTE_8_RealBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  785. {
  786. string strBinary;
  787. for( int i = 0; i < BYTE_8; i++)
  788. strBinary += ReturnBinaryReverseString(szSource[i]);
  789. bitset<64> bitSor(strBinary);
  790. if ( iSBit == iEBit)
  791. {
  792. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  793. return;
  794. }
  795. else
  796. {
  797. for (int i = 0; i < 64; i++)
  798. if ( i < iSBit || i > iEBit)
  799. bitSor.reset(i);
  800. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  801. }
  802. }
  803. void CCaluConvert::_BYTE_8_VirtBit(char *szValue, const int &VLen, BYTE *szSource, const int &iSBit, const int &iEBit)
  804. {
  805. string strBinary;
  806. for( int i = 0; i < BYTE_8; i++)
  807. strBinary += ReturnBinaryReverseString(szSource[i]);
  808. bitset<64> bitSor(strBinary);
  809. int dwSour = 0;
  810. if ( iSBit == iEBit)
  811. {
  812. sprintf_s(szValue,VLen,"%d",(int)bitSor[iSBit]);
  813. return;
  814. }
  815. else
  816. {
  817. string::reverse_iterator rit;
  818. rit = strBinary.rbegin() + iSBit;
  819. for (int i = 0; i <= iEBit - iSBit; i++,rit++)
  820. dwSour += (*rit - 48) * pow(2.0,i);
  821. _itoa_s(dwSour,szValue,VLen,10);
  822. }
  823. }
  824. void CCaluConvert::_BYTE_8_Decimal(char *szValue, const int &VLen, BYTE *szSource, const double &dConfs)
  825. {
  826. VirtBit(szValue,VLen,szSource,BYTE_8,0,8*BYTE_8-1);
  827. }
  828. void CCaluConvert::_BYTE_8_4_Float(char *szValue, const int &VLen, BYTE *szSource)
  829. {
  830. ReturnFloat_8Byte(szValue,VLen,szSource);
  831. }
  832. /************************************************************************/
  833. BYTE CCaluConvert::ReturnASCIIByte(BYTE hBy,BYTE lBy)
  834. {
  835. BYTE byValue;
  836. byValue = (ASCII_to_Byte(hBy) << 4) | ASCII_to_Byte(lBy);
  837. return byValue;
  838. }
  839. BYTE CCaluConvert::ReturnASCIIByte(BYTE *bySource)
  840. {
  841. BYTE byValue;
  842. byValue = (ASCII_to_Byte(bySource[0]) << 4) | ASCII_to_Byte(bySource[1]);
  843. /*BYTE byValue,hBy,lBy;
  844. hBy = bySource[0];
  845. lBy = bySource[1];
  846. byValue = (ASCII_to_Byte(hBy) << 4) | ASCII_to_Byte(lBy);*/
  847. return byValue;
  848. }
  849. BYTE CCaluConvert::ReturnASCIIByte(BYTE *bySource,const int &iIndex)
  850. {
  851. BYTE byValue;
  852. byValue = (ASCII_to_Byte(bySource[iIndex]) << 4) | ASCII_to_Byte(bySource[iIndex+1]);
  853. /*BYTE byValue,hBy,lBy;
  854. hBy = bySource[iIndex];
  855. lBy = bySource[iIndex+1];
  856. byValue = (ASCII_to_Byte(hBy) << 4) | ASCII_to_Byte(lBy);*/
  857. return byValue;
  858. }
  859. DWORD CCaluConvert::ReturnASCIIDWORD(BYTE *bySource)
  860. {
  861. DWORD dwValue;
  862. dwValue = ReturnASCIIWORD(bySource,0) << 16 | ReturnASCIIWORD(bySource,2);
  863. /*WORD hWd,lWd;
  864. hWd = ReturnASCIIWORD(bySource,0);
  865. lWd = ReturnASCIIWORD(bySource,2);
  866. dwValue = hWd << 16 | lWd;*/
  867. return dwValue;
  868. }
  869. DWORD CCaluConvert::ReturnASCIIDWORD(BYTE *bySource,const int &iIndex)
  870. {
  871. DWORD dwValue;
  872. dwValue = ReturnASCIIWORD(bySource+iIndex,0) << 16 | ReturnASCIIWORD(bySource+iIndex+1,2);
  873. return dwValue;
  874. }
  875. void CCaluConvert::ReturnBinaryString(char (&szValue)[9], BYTE bySource)
  876. {
  877. bitset<8> bitSor(bySource);
  878. for ( int i = 0; i < 8; i++)
  879. szValue[i] = bitSor[i];
  880. }
  881. string CCaluConvert::ReturnBinaryString( BYTE bySource)
  882. {
  883. string strBinary;
  884. strBinary.resize(8);
  885. bitset<8> bitSor(bySource);
  886. for ( int i = 0; i < 8; i++)
  887. strBinary[i] = bitSor[i] + 48;
  888. return strBinary;
  889. }
  890. void CCaluConvert::ReturnBinaryReverseString(char (&szValue)[9], BYTE bySource)
  891. {
  892. bitset<8> bitSor(bySource);
  893. for ( int i = 0; i < 8; i++)
  894. szValue[i] = bitSor[7-i];
  895. }
  896. string CCaluConvert::ReturnBinaryReverseString( BYTE bySource)
  897. {
  898. string strBinary;
  899. strBinary.resize(8);
  900. bitset<8> bitSor(bySource);
  901. for ( int i = 0; i < 8; i++)
  902. strBinary[i] = bitSor[7-i] + 48;
  903. return strBinary;
  904. }
  905. template <int nCount>
  906. void CCaluConvert::GetRealBitValue(char *szValue, const int &VLen,string &strBinary,const int &iSBit, const int &iEBit)
  907. {
  908. bitset<8*nCount> bitSor(strBinary);
  909. if ( iSBit == iEBit)
  910. {
  911. for (int i = 0; i < 8*nCount; i++)
  912. if ( i < iSBit || i > iEBit)
  913. bitSor.reset(i);
  914. _itoa_s(bitSor.to_ulong(),szValue,VLen,10);
  915. return;
  916. }
  917. else
  918. {
  919. for (int i = 0; i < 8*nCount; i++)
  920. if ( i < iSBit || i > iEBit)
  921. bitSor.reset(i);
  922. _itoa_s(bitSor.to_ulong(),szValue,VLen,10); // to_ulong只返回32位有效数,超过将出错;
  923. }
  924. }
  925. template <int nCount>
  926. void CCaluConvert::GetVirtBitValue(char *szValue, const int &VLen,string &strBinary,const int &iSBit, const int &iEBit)
  927. {
  928. bitset<8*nCount> bitSor(strBinary);
  929. if ( iSBit == iEBit)
  930. {
  931. sprintf_s(szValue,VLen,"%c",strBinary[iSBit]);
  932. return;
  933. }
  934. else
  935. {
  936. int iBitLen = iEBit - iSBit;
  937. #if 0
  938. __int64 _i64_Sour = 0;
  939. string::reverse_iterator rit;
  940. rit = strBinary.rbegin() + iSBit;
  941. for (int i = 0; i <= iBitLen; i++,rit++)
  942. _i64_Sour += (*rit - 48) * pow(2.0,i);
  943. //_itoa_s(_i64_Sour,szValue,VLen,10);
  944. //_ultoa_s(_i64_Sour,szValue,VLen,10);
  945. sprintf_s(szValue,VLen,"%llu",_i64_Sour);
  946. #else
  947. int int_dat = 0;
  948. long double ldValue = 0.0;
  949. for ( int i = 0; i <= iBitLen; i++)
  950. {
  951. int_dat = strBinary[iSBit+i] - 48;
  952. if (int_dat == 0)
  953. continue;
  954. ldValue += int_dat*pow(2.0,i);
  955. }
  956. sprintf_s(szValue,VLen,"%llf",ldValue);
  957. #endif
  958. }
  959. }
  960. void CCaluConvert::ReturnFloat_8Byte(char *szValue, const int &VLen, BYTE *szSource)
  961. {
  962. union
  963. {
  964. float fNum ;
  965. BYTE ch[4];
  966. }uBtoF;
  967. BYTE ch[8] = {0};
  968. memcpy(ch, szSource, 8);
  969. uBtoF.ch[0] = ReturnASCIIByte(szSource[0],szSource[1]);
  970. uBtoF.ch[1] = ReturnASCIIByte(szSource[2],szSource[3]);
  971. uBtoF.ch[2] = ReturnASCIIByte(szSource[4],szSource[5]);
  972. uBtoF.ch[3] = ReturnASCIIByte(szSource[6],szSource[7]);
  973. sprintf_s(szValue,VLen,"%f",uBtoF.fNum);
  974. }
  975. /************************************************************************/
  976. void CCaluConvert::swapUpperLower(char *szMsg)
  977. {
  978. for (int i = 0; i< (int)strlen(szMsg); i++)
  979. {
  980. if ((szMsg[i] >= 'A') && (szMsg[i] <= 'Z'))
  981. szMsg[i] = szMsg[i] + 32;
  982. else if ((szMsg[i] >= 'a') && (szMsg[i] <= 'z'))
  983. szMsg[i] = szMsg[i] - 32;
  984. else
  985. szMsg[i] = szMsg[i];
  986. }
  987. }
  988. void CCaluConvert::upperAllByte(char *szMsg)
  989. {
  990. for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
  991. {
  992. if ((szMsg[i] >= 'a') && (szMsg[i] <= 'z'))
  993. szMsg[i] = szMsg[i] - 32;
  994. else
  995. szMsg[i] = szMsg[i];
  996. }
  997. }
  998. void CCaluConvert::lowerAllByte(char *szMsg)
  999. {
  1000. for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
  1001. {
  1002. if ((szMsg[i]>='A') && (szMsg[i]<='Z'))
  1003. szMsg[i] = szMsg[i] + 32;
  1004. else
  1005. szMsg[i] = szMsg[i];
  1006. }
  1007. }
  1008. void CCaluConvert::ByteToTwoByte( char *szMsg, char *szConvMsg )
  1009. {
  1010. char ch[3];
  1011. for (int i=0; i < static_cast<int>(strlen(szMsg)); i++)
  1012. {
  1013. memset(ch, 0, 3);
  1014. _itoa_s(static_cast<int>(szMsg[i]), ch, 3, 16);
  1015. szConvMsg[i*2] = ch[0];
  1016. szConvMsg[i*2 + 1] = ch[1];
  1017. }
  1018. }
  1019. void CCaluConvert::TwoByteToByte(char *szMsg, char *szConvMsg )
  1020. {
  1021. for (int i=0; i< static_cast<int>(strlen(szMsg)/2); i++)
  1022. szConvMsg[i] = szMsg[i*2] << 4 | szMsg[i*2 + 1];
  1023. }
  1024. char CCaluConvert::ByteToAscii(BYTE btSrc)
  1025. {
  1026. char chDest;
  1027. if( btSrc < 10 )
  1028. {
  1029. chDest = static_cast<char>(btSrc % 10 + '0');
  1030. chDest = lowercase2uppercase(chDest);
  1031. return chDest;
  1032. }
  1033. else
  1034. {
  1035. chDest = ByteToAscii( btSrc / 10 ) + static_cast<char>( btSrc % 10 + '0' );
  1036. chDest = lowercase2uppercase(chDest);
  1037. return chDest;
  1038. }
  1039. }
  1040. char CCaluConvert::lowercase2uppercase(BYTE btSrc)
  1041. {
  1042. if( btSrc >= 'a' && btSrc <= 'z' )
  1043. return btSrc - 'a' + 'A';
  1044. return btSrc;
  1045. }
  1046. CString CCaluConvert::GetDoubleText(double d)
  1047. {
  1048. CString strRtn;
  1049. CString strFmt;
  1050. int g_nPrecision = -1;
  1051. if(g_nPrecision == 0)
  1052. strFmt = "%g";
  1053. else
  1054. {
  1055. long lV = static_cast<long>(fabs(d));
  1056. if(lV >= 1)
  1057. {
  1058. int nIntBit = 0;
  1059. while(lV >= 1)
  1060. {
  1061. lV = lV/10;
  1062. nIntBit++;
  1063. }
  1064. if(nIntBit < g_nPrecision)
  1065. strFmt.Format("%c%d.%df", '%', nIntBit, (g_nPrecision-nIntBit));
  1066. else
  1067. strFmt.Format("%c%d.0f", '%', nIntBit);
  1068. }
  1069. else
  1070. {
  1071. double dTemp = fabs(d);
  1072. if(dTemp > 0.00000000001)
  1073. {
  1074. int nDot0Bit = 0;
  1075. while(dTemp < 0.1)
  1076. {
  1077. dTemp = dTemp*10;
  1078. nDot0Bit++;
  1079. }
  1080. strFmt.Format("%c1.%df", '%', nDot0Bit + g_nPrecision);
  1081. }
  1082. else
  1083. {
  1084. strFmt = "%g";
  1085. }
  1086. }
  1087. }
  1088. strRtn.Format(strFmt, d);
  1089. return strRtn;
  1090. }
  1091. char CCaluConvert::Hex16(char WillChangeNum[]) //该函数把四位二进制转换成十六进制数;
  1092. {
  1093. int i;
  1094. i = (WillChangeNum[3]) + (WillChangeNum[2] * 10) +
  1095. (WillChangeNum[1] * 100) + (WillChangeNum[0] * 1000);
  1096. switch(i)
  1097. {
  1098. case 0:
  1099. return 0;
  1100. case 1:
  1101. return 1;
  1102. case 10:
  1103. return 2;
  1104. case 11:
  1105. return 3;
  1106. case 100:
  1107. return 4;
  1108. case 101:
  1109. return 5;
  1110. case 110:
  1111. return 6;
  1112. case 111:
  1113. return 7;
  1114. case 1000:
  1115. return 8;
  1116. case 1001:
  1117. return 9;
  1118. case 1010:
  1119. return 0x0A;
  1120. case 1011:
  1121. return 0x0B;
  1122. case 1100:
  1123. return 0x0C;
  1124. case 1101:
  1125. return 0x0D;
  1126. case 1110:
  1127. return 0x0E;
  1128. case 1111:
  1129. return 0x0F;
  1130. }
  1131. return -1;
  1132. }
  1133. void CCaluConvert::strReverse( char *str )
  1134. {
  1135. int l = (int)strlen(str);
  1136. for( int i = 0; i < l; i++ )
  1137. {
  1138. for(int i = 0; i < l; i++)
  1139. {
  1140. if( str[i] >= 'A' && str[i] <= 'Z' )
  1141. str[i] += 32;
  1142. else if(str[i] >= 'a' && str[i] <= 'z')
  1143. str[i] -= 32;
  1144. }
  1145. }
  1146. }
  1147. char CCaluConvert::twoHexByte2Word(char Hbyte,char Lbyte)
  1148. {
  1149. char Numb1;
  1150. char Numb2;
  1151. if (Hbyte >= 'A')
  1152. Numb1 = (toupper(Hbyte)-'0'-7)*16;
  1153. else
  1154. Numb1 = (Hbyte - '0')*16;
  1155. if (Lbyte >= 'A')
  1156. Numb2 = (toupper(Lbyte) - '0' - 7);
  1157. else
  1158. Numb2 = (Lbyte - '0');
  1159. return (Numb1 + Numb2);
  1160. }
  1161. void CCaluConvert::Str2HexStr(char *szHexString,char *szString,int *iHexStringLen)
  1162. {
  1163. int iLoop;
  1164. int iCount;
  1165. iLoop = 0;
  1166. iCount = 0;
  1167. szHexString[0] = '\0';
  1168. while(1)
  1169. {
  1170. if (szString[iLoop] == '\0' || szString[iLoop + 1] == '\0')
  1171. break;
  1172. szHexString[iCount] = static_cast<char>(twoHexByte2Word(szString[iLoop],szString[iLoop + 1]));
  1173. iLoop ++;
  1174. iLoop ++;
  1175. iCount ++;
  1176. }
  1177. szHexString[iCount] = '\0';
  1178. *iHexStringLen = iCount;
  1179. }