c5b00ebdb0f349e6f09cc093cab0e913ceca3697.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. namespace IControls_FireManager
  7. {
  8. // 데이터를 변환하는 것은 여기 클래스에서 전담한다.
  9. public static class _Convert
  10. {
  11. // 구분자 (데이터 구분)
  12. public static char[] Split_Key = { ';' };
  13. // 구분자 (데이터 내부 구분)
  14. public static char[] Split_Data = { '=' };
  15. // 구분자 (데이터 결과 구분)
  16. public static char[] Result_Char = { ',' };
  17. // 구분자 (데이터 결과 구분)
  18. public static string Result_String = ",";
  19. // 구분자 (IP Address 구분)
  20. public static char[] Split_IPAddress = { '.' };
  21. // 구분자 (PortNum 구분)
  22. public static char[] Split_PortNum = { '-' };
  23. // 구분자 (일정 구분)
  24. public static char Split_Time = '|' ;
  25. public static char[] Split_Times = { '|' };
  26. public static char UI_Split_Time = '\n';
  27. public static char[] UI_Split_Times = { '\n' };
  28. // 데이터를 입력하면 분리해서 변환
  29. // 예시 : "test1,test2" -> test1 과 test2 로
  30. public static string[] String_to_ArrayString(ArrayList Datas)
  31. {
  32. string[] not = { };
  33. if (Datas == null || Datas.Count == 0) return not;
  34. string Temp = null;
  35. foreach (string Data in Datas)
  36. {
  37. Temp = Temp + Data + _Text.SemiColon;
  38. }
  39. Temp = Temp.TrimEnd(Split_Key);
  40. string[] Temps = Temp.Split(Split_Key);
  41. return Temps;
  42. }
  43. // 데이터를 입력하면 분리해서 변환
  44. // 예시 : "test1,test2" -> test1 과 test2 로
  45. public static string[] String_to_ArrayString(string Datas)
  46. {
  47. string[] not = { };
  48. if (Datas == null || Datas.Length == 0) return not;
  49. string Temp = Datas.Trim();
  50. string[] Temps = Temp.Split(Split_Key);
  51. return Temps;
  52. }
  53. // 데이터를 입력하면 분리해서 변환 (단, 구분자를 직접 지정할수 있음)
  54. // 예시 : "test1,test2" -> test1 과 test2 로
  55. public static string[] String_to_ArrayString(char [] spilt, string Datas)
  56. {
  57. string[] not = { };
  58. if (Datas == null || Datas.Length == 0) return not;
  59. string Temp = Datas.Trim();
  60. string[] Temps = Temp.Split(spilt);
  61. return Temps;
  62. }
  63. // 데이터를 입력하면 분리해서 변환 (단, 구분자를 직접 지정할수 있음)
  64. // 예시 : "test1,test2" -> test1 과 test2 로
  65. public static ArrayList String_to_ArrayLIst(char[] spilt, string Datas)
  66. {
  67. ArrayList Result = new ArrayList();
  68. if (Datas == null || Datas.Length == 0) return Result;
  69. string Temp = Datas.Trim();
  70. string[] Temps = Temp.Split(spilt);
  71. foreach (string Item in Temps)
  72. Result.Add(Item);
  73. return Result;
  74. }
  75. // 맨처음 문자를 추출한다
  76. // 예시 : "test1;test2" -> test1
  77. public static string Get_First_ArrayString(string Datas)
  78. {
  79. if (Datas == null || Datas.Length == 0) return _Text.Null;
  80. string Temp = Datas.Trim();
  81. string[] Temps = Temp.Split(Split_Key);
  82. return Temps[0];
  83. }
  84. // 데이터를 입력하면 분리해서 변환
  85. // 예시 : "Key1=Data1;Key2=Data2", true -> "Key1,Key2"
  86. // 예시 : "Key1=Data1;Key2=Data2", false -> "Data1,Data2"
  87. // NullValueEnable : false 면 널값은 포함하지 않는다
  88. public static string String_to_Key_Data(string KeyDatas, bool Key, bool NullValueEnable)
  89. {
  90. string Temp = KeyDatas.Trim();
  91. string[] Temps = Temp.Split(Split_Key);
  92. string result = null;
  93. foreach (string data in Temps)
  94. {
  95. string[] temps = data.Split(Split_Data);
  96. if (NullValueEnable == false)// 데이터가 널값인 경우도 포함할지 여부
  97. {
  98. if (temps[0].Length != 0 && temps[1].Length != 0) // 키 혹은 데이타가 널값이라면 추가하지 못한다
  99. {
  100. // 키
  101. if (Key == true)
  102. {
  103. result = result + temps[0] + Result_String;
  104. }
  105. // 데이타
  106. else
  107. {
  108. result = result + temps[1] + Result_String;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. // 키
  115. if (Key == true)
  116. {
  117. result = result + temps[0] + Result_String;
  118. }
  119. // 데이타
  120. else
  121. {
  122. result = result + temps[1] + Result_String;
  123. }
  124. }
  125. }
  126. if(result != null) result = result.TrimEnd(Result_Char);
  127. return result;
  128. }
  129. // 데이타베이스에 삽입하는 경우 int 자료값이 0이라면 삽입 코드 혹은 갱신 코드로 들어가지 않도록 강제로 null 로 변환
  130. public static string IntZero_to_Null(int Data)
  131. {
  132. string result = null;
  133. if (Data == 0) result = null;
  134. else result = Data.ToString();
  135. return result;
  136. }
  137. // 데이타베이스에 읽어오는 경우 string 자료값이 null 이라면 Convert 에러가 나오지 않도록 강제로 0 로 변환
  138. public static int Null_to_IntZero(object Data)
  139. {
  140. try
  141. {
  142. int result = 0;
  143. string Temp = Data.ToString();
  144. if (Temp != null) Temp = Temp.Trim();
  145. if (Temp == null || Temp.Length == 0) result = 0;
  146. else result = Convert.ToInt32(Temp);
  147. return result;
  148. }
  149. catch
  150. {
  151. return 0;
  152. }
  153. }
  154. public static bool Exception_Into_object(object Data1, object Data2, object Data3, object Data4, object Data5)
  155. {
  156. try
  157. {
  158. if ((Data1 == null || Data1.ToString().Trim().Length == 0)
  159. && (Data2 == null || Data2.ToString().Trim().Length == 0)
  160. && (Data3 == null || Data3.ToString().Trim().Length == 0)
  161. && (Data4 == null || Data4.ToString().Trim().Length == 0)
  162. && (Data5 == null || Data5.ToString().Trim().Length == 0))
  163. return false;
  164. else
  165. {
  166. int t1 = Int32.Parse(Data1.ToString().Trim());
  167. int t2 = Int32.Parse(Data2.ToString().Trim());
  168. int t3 = Int32.Parse(Data3.ToString().Trim());
  169. int t4 = Int32.Parse(Data4.ToString().Trim());
  170. int t5 = Int32.Parse(Data5.ToString().Trim());
  171. }
  172. return true;
  173. }
  174. catch
  175. {
  176. return false;
  177. }
  178. }
  179. // 스트링 자료가 널값이라면 None 으로 치환하는 경우
  180. public static string Null_to_None(string Data)
  181. {
  182. string result = Data;
  183. if (result == null || result.Length == 0)
  184. result = _Text.None;
  185. return result;
  186. }
  187. // 스트링 자료가 널값이라면 Blank 으로 치환하는 경우
  188. public static string Null_to_Blank(string Data)
  189. {
  190. string result = Data;
  191. if (result == null || result.Length == 0)
  192. result = _Text.Blank;
  193. return result;
  194. }
  195. // 스트링으로 된 데이타를 두자리 스트링으로 변환
  196. public static string String_to_Int_TwoType(string Data)
  197. {
  198. string result = null;
  199. if (Data == null || Data.Length == 0 || Data.Length > 4) return "00";
  200. int IntData = Int32.Parse(Data);
  201. // 3자리 이상으로 데이타가 넘어오면 앞3자리 삭제
  202. if (IntData > 999)
  203. result = IntData.ToString().Remove(3);
  204. // 2자리 이상으로 데이타가 넘어오면 앞2자리 삭제
  205. else if (IntData > 99)
  206. result = IntData.ToString().Remove(2);
  207. else if (IntData == 0)
  208. result = "00";
  209. else if (IntData > 9 )
  210. result = IntData.ToString();
  211. else
  212. result = "0" + IntData.ToString();
  213. return result;
  214. }
  215. // 스트링으로 된 데이타를 세자리 스트링으로 변환
  216. public static string String_to_Int_ThreeType(string Data)
  217. {
  218. string result = null;
  219. if (Data == null || Data.Length == 0 || Data.Length > 4) return "000";
  220. int IntData = Int32.Parse(Data);
  221. // 3자리 이상으로 데이타가 넘어오면 앞3자리 삭제
  222. if (IntData > 999)
  223. result = IntData.ToString().Remove(1);
  224. else if (IntData == 0)
  225. result = "000";
  226. else if (IntData > 9)
  227. result = IntData.ToString();
  228. else
  229. result = "00" + IntData.ToString();
  230. return result;
  231. }
  232. // 스트링 데이터 -> 바이트 배열로 변환
  233. public static byte[] String_to_Byte(string Data)
  234. {
  235. byte[] ByteData = new byte[Data.Length];
  236. for (int i = 0; i < Data.Length; i++)
  237. {
  238. ByteData[i] = Convert.ToByte(Data[i]);
  239. }
  240. return ByteData;
  241. }
  242. // 정수 -> 2byte 변환
  243. public static byte Int_to_Byte(int Data, bool Head)
  244. {
  245. byte result1 = 0x00;
  246. byte result2 = 0x00;
  247. // 한자리 혹은 두자리
  248. if (Data <= 255)
  249. {
  250. result1 = 0x00;
  251. result2 = Convert.ToByte(Data); // int -> byte
  252. }
  253. else
  254. {
  255. // Ex :2748 -> 0xABC
  256. string temp = Data.ToString("X");
  257. // 세자리
  258. if (temp.Length == 3)
  259. {
  260. string temp1 = temp[0].ToString();
  261. string temp2 = temp[1].ToString() + temp[2].ToString() ;
  262. result1 = Convert.ToByte(temp1, 16); // string -> byte
  263. result2 = Convert.ToByte(temp2, 16); // string -> byte
  264. }
  265. // 4자리
  266. else if (temp.Length == 4)
  267. {
  268. string temp1 = temp[0].ToString() + temp[1].ToString();
  269. string temp2 = temp[2].ToString() + temp[3].ToString();
  270. result1 = Convert.ToByte(temp1, 16); // string -> byte
  271. result2 = Convert.ToByte(temp2, 16); // string -> byte
  272. }
  273. }
  274. // 앞자리
  275. if (Head == true)
  276. {
  277. return result1;
  278. }
  279. // 뒷자리
  280. else
  281. {
  282. return result2;
  283. }
  284. }
  285. // 바이트 -> 정수
  286. public static string GetBytesInt32(long argument)
  287. {
  288. byte[] byteArray = BitConverter.GetBytes(argument);
  289. string result = "";
  290. for (int i = byteArray.Length - 1; i >= 0; i--)
  291. {
  292. result = result + byteArray[i].ToString("X");
  293. }
  294. result = "0x" + result;
  295. return result;
  296. }
  297. // 4자리수 만들기
  298. public static string To_4byte(string Data)
  299. {
  300. try
  301. {
  302. string result = null;
  303. if (Data == null || Data.Length == 0 || Data == "0" || Data == "00" || Data == "000" || Data == "0000")
  304. return "0000";
  305. int temp_result = Int32.Parse(Data);
  306. if (temp_result > 0 && temp_result <= 9999)
  307. {
  308. if (temp_result < 10)
  309. {
  310. result = "000" + temp_result.ToString();
  311. }
  312. else if (temp_result < 100)
  313. {
  314. result = "00" + temp_result.ToString();
  315. }
  316. else if (temp_result < 1000)
  317. {
  318. result = "0" + temp_result.ToString();
  319. }
  320. else
  321. result = temp_result.ToString();
  322. }
  323. return result;
  324. }
  325. catch
  326. {
  327. return "0000";
  328. }
  329. }
  330. // 2자리수로 만들기
  331. public static string To_2byte(string Data)
  332. {
  333. try
  334. {
  335. string result = null;
  336. if (Data == null || Data.Length == 0 || Data == "0" || Data == "00" || Data == "000" || Data == "0000")
  337. return "00";
  338. int temp_result = Int32.Parse(Data);
  339. if (temp_result >= 0 && temp_result <= 99)
  340. {
  341. if (temp_result == 0)
  342. {
  343. result = "00";
  344. }
  345. else if (temp_result < 10)
  346. {
  347. result = "0" + temp_result.ToString();
  348. }
  349. else
  350. result = temp_result.ToString();
  351. }
  352. return result;
  353. }
  354. catch
  355. {
  356. return "00";
  357. }
  358. }
  359. // 인코딩
  360. public static byte[] EncodingData(Coding coding, string Data)
  361. {
  362. byte[] data = null;
  363. switch (coding)
  364. {
  365. case Coding.Default:
  366. data = Encoding.Default.GetBytes(Data);
  367. break;
  368. case Coding.UTF8:
  369. data = Encoding.UTF8.GetBytes(Data);
  370. break;
  371. case Coding.ASCII:
  372. data = Encoding.ASCII.GetBytes(Data);
  373. break;
  374. }
  375. return data;
  376. }
  377. // 디코딩
  378. public static string DecodingData(Coding coding, byte[] Data)
  379. {
  380. string data = null;
  381. switch (coding)
  382. {
  383. case Coding.Default:
  384. data = Encoding.Default.GetString(Data);
  385. break;
  386. case Coding.UTF8:
  387. data = Encoding.UTF8.GetString(Data);
  388. break;
  389. case Coding.ASCII:
  390. data = Encoding.ASCII.GetString(Data);
  391. break;
  392. }
  393. return data;
  394. }
  395. // 초를 분 / 초 단위로 표시한다
  396. // 초를 시간/ 분 / 초 단위로 표시한다
  397. public static string Second_To_TimeInfo(int Second)
  398. {
  399. string result = null;
  400. int temp_second = 0;
  401. int temp_minute = 0;
  402. int temp_hour = 0;
  403. // 잘못된 정보
  404. if(Second < 0)
  405. return null;
  406. // 60 초 미만이라면
  407. if (Second >= 0 && Second < 60)
  408. {
  409. result = string.Format("{0}초", Second);
  410. }
  411. // 1시간 미만이라면
  412. else if (Second >= 60 && Second < 3600)
  413. {
  414. temp_minute = Second / 60;
  415. temp_second = Second - (temp_minute * 60);
  416. result = string.Format("{0}분 {1}초", temp_minute, temp_second);
  417. }
  418. else
  419. {
  420. temp_hour = Second / 3600;
  421. temp_minute = (Second - (temp_hour * 3600)) / 60;
  422. temp_second = (Second - (temp_hour * 3600) - (temp_minute * 60));
  423. result = string.Format("{0}시간 {1}분 {2}초", temp_hour, temp_minute, temp_second);
  424. }
  425. return result;
  426. }
  427. // 리스트내에 동일한 데이타만 존재하면 true 리턴
  428. public static bool Check_ArrayList_SameValue(ArrayList arraylist)
  429. {
  430. string Compare = (string)arraylist[0];
  431. foreach (string Data in arraylist)
  432. {
  433. if (Data != Compare)
  434. return false;
  435. }
  436. return true;
  437. }
  438. // 시간정보를 콤마로 구분하도록 변환
  439. public static string SplitTime_To_Comma(string Data)
  440. {
  441. if (Data == null || Data.Length == 0)
  442. return "";
  443. else
  444. {
  445. return Data.Replace(Split_Time.ToString(), Result_String);
  446. }
  447. }
  448. }
  449. }