using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace SPC.Kiosk.Common { #region ASCII 제어 문자 정의 /// /// Control Char /// public static class Ctrl_CHAR { /// /// NULL [00]0x00 /// public static char Null = (char)Ctrl_ASCII.Null; /// /// SOH [01]0x01 /// public static char SOH = (char)Ctrl_ASCII.SOH; /// /// STX [02]0x02 /// public static char STX = (char)Ctrl_ASCII.STX; /// /// ETX [03]0x03 /// public static char ETX = (char)Ctrl_ASCII.ETX; /// /// EOT [04]0x04 /// public static char EOT = (char)Ctrl_ASCII.EOT; /// /// ENQ [05]0x05 /// public static char ENQ = (char)Ctrl_ASCII.ENQ; /// /// ACK [06]0x06 /// public static char ACK = (char)Ctrl_ASCII.ACK; /// /// BEL [07]0x07 /// public static char BEL = (char)Ctrl_ASCII.BEL; /// /// BS [08]0x08 /// public static char BS = (char)Ctrl_ASCII.BS; /// /// HT [09]0x09 /// public static char HT = (char)Ctrl_ASCII.HT; /// /// LF [0A]0x10 /// public static char LF = (char)Ctrl_ASCII.LF; /// /// VT [0B]0x11 /// public static char VT = (char)Ctrl_ASCII.VT; /// /// FF [0C]0x12 /// public static char FF = (char)Ctrl_ASCII.FF; /// /// CR [0D]0x13 /// public static char CR = (char)Ctrl_ASCII.CR; /// /// SO [0E]0x14 /// public static char SO = (char)Ctrl_ASCII.SO; /// /// SI [0F]0x15 /// public static char SI = (char)Ctrl_ASCII.SI; /// /// DLE [10]0x16 /// public static char DLE = (char)Ctrl_ASCII.DLE; /// /// DC1 [11]0x17 /// public static char DC1 = (char)Ctrl_ASCII.DC1; /// /// DC2 [12]0x18 /// public static char DC2 = (char)Ctrl_ASCII.DC2; /// /// DC3 [13]0x19 /// public static char DC3 = (char)Ctrl_ASCII.DC3; /// /// DC4 [14]0x20 /// public static char DC4 = (char)Ctrl_ASCII.DC4; /// /// NAK [15]0x21 /// public static char NAK = (char)Ctrl_ASCII.NAK; /// /// SYN [16]0x22 /// public static char SYN = (char)Ctrl_ASCII.SYN; /// /// ETB [17]0x23 /// public static char ETB = (char)Ctrl_ASCII.ETB; /// /// CAN [18]0x24 /// public static char CAN = (char)Ctrl_ASCII.CAN; /// /// EM [19]0x25 /// public static char EM = (char)Ctrl_ASCII.EM; /// /// SUB [1A]0x26 /// public static char SUB = (char)Ctrl_ASCII.SUB; /// /// ESC [1B]0x27 /// public static char ESC = (char)Ctrl_ASCII.ESC; /// /// FS [1C]0x28 /// public static char FS = (char)Ctrl_ASCII.FS; /// /// GS [1D]0x29 /// public static char GS = (char)Ctrl_ASCII.GS; /// /// RS [1E]0x30 /// public static char RS = (char)Ctrl_ASCII.RS; /// /// US [1F]0x32 /// public static char US = (char)Ctrl_ASCII.US; } /// /// Control Byte /// public static class Ctrl_BYTE { /// /// NULL /// public static byte Null = (byte)Ctrl_ASCII.Null; /// /// SOH /// public static byte SOH = (byte)Ctrl_ASCII.SOH; /// /// STX /// public static byte STX = (byte)Ctrl_ASCII.STX; /// /// ETX /// public static byte ETX = (byte)Ctrl_ASCII.ETX; /// /// EOT /// public static byte EOT = (byte)Ctrl_ASCII.EOT; /// /// ENQ /// public static byte ENQ = (byte)Ctrl_ASCII.ENQ; /// /// ACK /// public static byte ACK = (byte)Ctrl_ASCII.ACK; /// /// BEL /// public static byte BEL = (byte)Ctrl_ASCII.BEL; /// /// BS /// public static byte BS = (byte)Ctrl_ASCII.BS; /// /// HT /// public static byte HT = (byte)Ctrl_ASCII.HT; /// /// LF /// public static byte LF = (byte)Ctrl_ASCII.LF; /// /// VT /// public static byte VT = (byte)Ctrl_ASCII.VT; /// /// FF /// public static byte FF = (byte)Ctrl_ASCII.FF; /// /// CR /// public static byte CR = (byte)Ctrl_ASCII.CR; /// /// SO /// public static byte SO = (byte)Ctrl_ASCII.SO; /// /// SI /// public static byte SI = (byte)Ctrl_ASCII.SI; /// /// DLE /// public static byte DLE = (byte)Ctrl_ASCII.DLE; /// /// DC1 /// public static byte DC1 = (byte)Ctrl_ASCII.DC1; /// /// DC2 /// public static byte DC2 = (byte)Ctrl_ASCII.DC2; /// /// DC3 /// public static byte DC3 = (byte)Ctrl_ASCII.DC3; /// /// DC4 /// public static byte DC4 = (byte)Ctrl_ASCII.DC4; /// /// NAK /// public static byte NAK = (byte)Ctrl_ASCII.NAK; /// /// SYN /// public static byte SYN = (byte)Ctrl_ASCII.SYN; /// /// ETB /// public static byte ETB = (byte)Ctrl_ASCII.ETB; /// /// CAN /// public static byte CAN = (byte)Ctrl_ASCII.CAN; /// /// EM /// public static byte EM = (byte)Ctrl_ASCII.EM; /// /// SUB /// public static byte SUB = (byte)Ctrl_ASCII.SUB; /// /// ESC /// public static byte ESC = (byte)Ctrl_ASCII.ESC; /// /// FS /// public static byte FS = (byte)Ctrl_ASCII.FS; /// /// GS /// public static byte GS = (byte)Ctrl_ASCII.GS; /// /// RS /// public static byte RS = (byte)Ctrl_ASCII.RS; /// /// US /// public static byte US = (byte)Ctrl_ASCII.US; } /// /// Cotrol Char String Type /// public static class Ctrl_STRING { /// /// NULL /// public static string Null = ""; /// /// SOH /// public static string SOH = Convert.ToString((char)Ctrl_ASCII.SOH); /// /// STX /// public static string STX = Convert.ToString((char)Ctrl_ASCII.STX); /// /// ETX /// public static string ETX = Convert.ToString((char)Ctrl_ASCII.ETX); /// /// EOT /// public static string EOT = Convert.ToString((char)Ctrl_ASCII.EOT); /// /// ENQ /// public static string ENQ = Convert.ToString((char)Ctrl_ASCII.ENQ); /// /// ACK /// public static string ACK = Convert.ToString((char)Ctrl_ASCII.ACK); /// /// BEL /// public static string BEL = Convert.ToString((char)Ctrl_ASCII.BEL); /// /// BS /// public static string BS = Convert.ToString((char)Ctrl_ASCII.BS); /// /// HT /// public static string HT = Convert.ToString((char)Ctrl_ASCII.HT); /// /// LF /// public static string LF = Convert.ToString((char)Ctrl_ASCII.LF); /// /// VT /// public static string VT = Convert.ToString((char)Ctrl_ASCII.VT); /// /// FF /// public static string FF = Convert.ToString((char)Ctrl_ASCII.FF); /// /// CR /// public static string CR = Convert.ToString((char)Ctrl_ASCII.CR); /// /// SO /// public static string SO = Convert.ToString((char)Ctrl_ASCII.SO); /// /// SI /// public static string SI = Convert.ToString((char)Ctrl_ASCII.SI); /// /// DLE /// public static string DLE = Convert.ToString((char)Ctrl_ASCII.DLE); /// /// DC1 /// public static string DC1 = Convert.ToString((char)Ctrl_ASCII.DC1); /// /// DC2 /// public static string DC2 = Convert.ToString((char)Ctrl_ASCII.DC2); /// /// DC3 /// public static string DC3 = Convert.ToString((char)Ctrl_ASCII.DC3); /// /// DC4 /// public static string DC4 = Convert.ToString((char)Ctrl_ASCII.DC4); /// /// NAK /// public static string NAK = Convert.ToString((char)Ctrl_ASCII.NAK); /// /// SYN /// public static string SYN = Convert.ToString((char)Ctrl_ASCII.SYN); /// /// ETB /// public static string ETB = Convert.ToString((char)Ctrl_ASCII.ETB); /// /// CAN /// public static string CAN = Convert.ToString((char)Ctrl_ASCII.CAN); /// /// EM /// public static string EM = Convert.ToString((char)Ctrl_ASCII.EM); /// /// SUB /// public static string SUB = Convert.ToString((char)Ctrl_ASCII.SUB); /// /// ESC /// public static string ESC = Convert.ToString((char)Ctrl_ASCII.ESC); /// /// FS /// public static string FS = Convert.ToString((char)Ctrl_ASCII.FS); /// /// GS /// public static string GS = Convert.ToString((char)Ctrl_ASCII.GS); /// /// RS /// public static string RS = Convert.ToString((char)Ctrl_ASCII.RS); /// /// US /// public static string US = Convert.ToString((char)Ctrl_ASCII.US); } #endregion public static class CommonConverter { #region For DB string /// /// 실수로 변환 /// /// 입력객체 /// 실수 public static float rDBToFloat(this object o) { float result; try { if (o == DBNull.Value || o == null) result = 0F; else { if (!float.TryParse(o.ToString(), out result)) result = 0F; } } catch { result = 0F; } return result; } /// /// 정수로 변환 /// /// /// public static int rDBToInt(this object o) { int result; try { if (o == DBNull.Value || o == null) result = 0; else { if (!Int32.TryParse(o.ToString(), out result)) result = 0; } } catch { result = 0; } return result; } /// /// 문자로 변환 /// /// /// public static char rDBToChar(this object o) { char result; try { if (o == DBNull.Value) result = Ctrl_CHAR.Null; else result = o.ToString()[0]; } catch { result = Ctrl_CHAR.Null; } return result; } /// /// 문자열로 변환 /// /// /// public static string rDBToString(this object o) { string result; try { if (o == DBNull.Value || o == null) result = string.Empty; else result = o.ToString(); } catch { result = string.Empty; } return result; } #endregion #region Hexa & Bit /// /// 숫자를 16진수 문자열로 /// /// /// public static string rToHexString(this int _inInt) { try { return (Convert.ToString(_inInt, 16)).ToUpper().PadLeft(2, '0'); } catch { return string.Empty; } } /// /// Byte를 16진수 문자열로 /// /// /// public static string rToHexString(this byte _inByte) { try { return ((int)_inByte).rToHexString(); } catch { return string.Empty; } } /// /// Char를 16진수 문자열로 /// /// /// public static string rToHexString(this char _inChar) { try { return ((int)_inChar).rToHexString(); } catch { return string.Empty; } } /// /// Char배열을 Hex문자열로 /// /// /// public static string rToHexString(this char[] _inChars) { string result = string.Empty; try { foreach (char _inChar in _inChars) { result += ((int)_inChar).rToHexString(); } } catch { result = string.Empty; } return result; } /// /// Byte배열을 Hex문자열로 /// /// /// public static string rToHexString(this byte[] _inBytes) { string result = string.Empty; try { foreach (byte _inByte in _inBytes) { result += ((int)_inByte).rToHexString(); } } catch { result = string.Empty; } return result; } /// /// Integer배열을 Hex문자열로 /// /// /// public static string rToHexString(this int[] _inInts) { string result = string.Empty; try { foreach (int _inInt in _inInts) { result += _inInt.rToHexString(); } } catch { result = string.Empty; } return result; } /// /// 정수를 Bit문자열로 /// /// /// public static string rToBitString(this int _inInt) { try { return (Convert.ToString(_inInt, 2)).PadLeft(8, '0'); } catch { return string.Empty; } } /// /// Byte를 Bit문자열로 /// /// /// public static string rToBitString(this byte _inByte) { try { return ((int)_inByte).rToBitString(); } catch { return string.Empty; } } /// /// Char를 Bit문자열로 /// /// /// public static string rToBitString(this char _inChar) { try { return ((int)_inChar).rToBitString(); } catch { return string.Empty; } } /// /// Char배열을 비트 문자열로 /// /// /// public static string rToBitString(this char[] _inChars) { return _inChars.rToByte().rToBitString(); } /// /// Byte배열을 Bit문자열로 /// /// /// public static string rToBitString(this byte[] _inBytes) { string result = string.Empty; try { for (int i = 0; i < _inBytes.Length; i++) { result += _inBytes[i].rToBitString(); } } catch { result = string.Empty; } return result; } /// /// Bit문자열을 Bool 배열로 /// /// /// public static bool[] rToBoolArray(this string _inString) { return _inString.ToCharArray().rToBoolArray(false); } /// /// Bit문자열을 Bool 배열로 /// /// /// 상위비트가 앞에 오도록 /// public static bool[] rToBoolArray(this string _inString, bool _Height2Low) { return _inString.ToCharArray().rToBoolArray(_Height2Low); } /// /// Bit문자열을 옵션에 따라 Boolean 배열로 /// /// 입력Char배열 /// 상위비트가 앞에 오도록 /// public static bool[] rToBoolArray(this char[] _getBit, bool _Height2Low) { bool[] result = null; try { if (_Height2Low) { result = new bool[_getBit.Length]; for (int i = 0; i < _getBit.Length; i++) { result[i] = _getBit[i] == '1'; } } else { result = new bool[_getBit.Length]; int pos = 0; for (int i = 7; i > -1; i--) { result[pos] = _getBit[i] == '1'; pos++; } } } catch { result = null; } return result; } /// /// 정수를 Bit값을 Boolean 배열로 (LowBit 가 0) /// /// /// public static bool[] rToBoolArray(this int _inInt) { bool[] result = null; try { result = _inInt.rToBitString().rToBoolArray(); } catch { result = null; } return result; } /// /// 정수의 Bit값을 Boolean 배열로 (HighBit 가 0) /// /// /// public static bool[] rToBoolArrayHL(this int _inInt) { bool[] result = null; try { result = _inInt.rToBitString().rToBoolArray(true); } catch { result = null; } return result; } /// /// Byte의 Byte값을 Boolean 배열로 (LowBit 가 0) /// /// /// public static bool[] rToBoolArray(this byte _inByte) { return ((int)_inByte).rToBoolArray(); } /// /// Byte의 Bit값을 Boolean 배열로 (HighBit 가 0) /// /// /// public static bool[] rToBoolArrayHL(this byte _inByte) { return ((int)_inByte).rToBoolArrayHL(); } /// /// Char값을 Boolean 배열로 (LowBit 가 0) /// /// /// public static bool[] rToBoolArray(this char _inChar) { return ((int)_inChar).rToBoolArray(); } /// /// Char값을 Boolean 배열로 (HighBit 가 0) /// /// /// public static bool[] rToBoolArrayHL(this char _inChar) { return ((int)_inChar).rToBoolArrayHL(); } /// /// 정수문자열을 정수로(int) /// /// /// public static int rToInteger(this string _inString) { try { if (_inString.rIsInteger()) return int.Parse(_inString); else return 0; } catch { return 0; } } /// /// 정수문자열을 정수로(long) /// /// /// public static long rToLongInteger(this string _inString) { try { if (_inString.rIsInteger()) return long.Parse(_inString); else return 0; } catch { return 0; } } /// /// 16진수 문자열을 정수로 /// /// /// public static int rHexStrToInt(this string _inString) { try { if (_inString.StartsWith("0x")) _inString = _inString.Substring(_inString.IndexOf("x") + 1); return Convert.ToInt16(_inString, 16); } catch { return 0; } } /// /// 16진수 문자열을 Byte로 /// /// /// public static byte rHexStrToByte(this string _inString) { return (byte)_inString.rHexStrToInt(); } /// /// 16진수 문자열을 Byte배열로 /// /// /// public static byte[] rHexStrToBytes(this string _inString) { byte[] result = null; try { if (_inString.Length % 2 == 1) _inString.PadLeft(_inString.Length + 1, '0'); result = new byte[_inString.Length / 2]; for (int i = 0; i < _inString.Length / 2; i++) { result[i] = (byte)_inString.Substring(i * 2, 2).rHexStrToInt(); } } catch { result = null; } return result; } /// /// 16진수 문자열을 Integer String으로 /// /// /// public static string rHexStringToIntStr(this string _inString) { string result = string.Empty; try { if (_inString.Length % 2 == 1) _inString.PadLeft(_inString.Length + 1, '0'); for (int i = 0; i < _inString.Length / 2; i++) { result += ((char)_inString.Substring(i * 2, 2).rHexStrToInt()).ToString(); } } catch { result = null; } return result; } /// /// 16진수 문자열을 CHar로 /// /// /// public static char rHexStrToChar(this string _inString) { return (char)_inString.rHexStrToInt(); } /// /// 비트문자열을 정수로 /// /// /// public static int rBitStrToInt(this string _inString) { try { return Convert.ToInt16(_inString, 2); } catch { return 0; } } /// /// 비트문자열을 Byte로 /// /// /// public static byte rBitStrToByte(this string _inString) { return (byte)_inString.rBitStrToInt(); } /// /// 비트배열을 정수로 /// /// 8개짜리 bool배열 /// /// public static int rToInteger(this bool[] _inBools, bool _Height2Low) { int result = 0; try { string _bits = String.Empty; if (_Height2Low) { for (int i = 0; i < _inBools.Length; i++) { _bits += _inBools[i] ? "1" : "0"; } } else { for (int i = 7; i > -1; i--) { _bits += _inBools[i] ? "1" : "0"; } } result = _bits.rBitStrToInt(); } catch { result = 0; } return result; } /// /// Highbit가 먼저인 Boolean 배열을 Integer로 /// /// /// public static int rHLtoInt(this bool[] _inBools) { return _inBools.rToInteger(true); } /// /// Lowbit가 먼저인 Boolean 배열을 Integer로 /// /// /// public static int rToInteger(this bool[] _inBools) { return _inBools.rToInteger(false); } /// /// Boolrean 배열을 Byte로 /// /// /// true=High가 첫번째,false=Lowbit가 첫번째 /// public static byte rToByte(this bool[] _inBools, bool _Height2Low) { return (byte)_inBools.rToInteger(_Height2Low); } /// /// Highbit가 먼저인 Boolean 배열을 Byte로 /// /// /// public static byte rHLtoByte(this bool[] _inBools) { return (byte)_inBools.rToInteger(true); } /// /// Lowbit가 먼저인 Boolrean 배열을 Byte로 /// /// /// public static byte rToByte(this bool[] _inBools) { return (byte)_inBools.rToInteger(false); } /// /// char 배열을 byte배열로 /// /// /// public static byte[] rToByte(this char[] _inChars) { byte[] result = null; try { result = new byte[_inChars.Length]; for (int i = 0; i < _inChars.Length; i++) { result[i] = (byte)_inChars[i]; } } catch { result = null; } return result; } /// /// byte배열을 char배열로 /// /// /// public static char[] rToChar(this byte[] _inByte) { char[] result = null; try { result = new char[_inByte.Length]; for (int i = 0; i < _inByte.Length; i++) { result[i] = (char)_inByte[i]; } } catch { result = null; } return result; } #endregion #region MakeLRC /// /// LRC Make /// /// /// public static string rMakeLRC(this string _Data) { return rMakeLRC(_Data, CodeType.Default); } /// /// LRC Make /// /// /// /// public static string rMakeLRC(this string _Data, CodeType _CodeType) { try { //long makeCheckSum = 0; //byte[] DataBytes = _Data.rToByteArray(); //foreach (byte abyte in DataBytes) makeCheckSum += abyte; //string checksum = makeCheckSum.ToString("X2"); return rMakeLRC(_Data.rToByteArray(_CodeType)).ToString(); } catch { return string.Empty; } } /// /// LRC Make /// /// /// public static byte rMakeLRC(this byte[] _Data) { try { byte LRC = _Data[0]; for (int i = 1; i < _Data.Length; i++) { LRC = (byte)(LRC ^ _Data[i]); } return LRC; } catch { return 0x00; } } /// /// LRC Make /// /// /// public static char rMakeLRC(this char[] _Data) { try { return (char)rMakeLRC(_Data.rToByte()); } catch { return (char)0x00; } } #endregion #region Checksum Make /// /// Checksum Make /// /// /// public static string rCheckSum(this string _Data) { return rCheckSum(_Data, CodeType.Default); } /// /// Checksum Make /// /// /// /// public static string rCheckSum(this string _Data, CodeType _CodeType) { try { //long makeCheckSum = 0; //byte[] DataBytes = _Data.rToByteArray(); //foreach (byte abyte in DataBytes) makeCheckSum += abyte; //string checksum = makeCheckSum.ToString("X2"); return rCheckSum(_Data.rToByteArray(_CodeType)).rToEncString(_CodeType); } catch { return string.Empty; } } /// /// Checksum Make /// /// /// public static byte[] rCheckSum(this byte[] _Data) { try { long makeCheckSum = 0; foreach (byte abyte in _Data) makeCheckSum += abyte; string checksum = makeCheckSum.ToString("X2"); return checksum.Substring(checksum.Length - 2, 2).rToByteArray(); } catch { return null; } } /// /// Checksum Make /// /// /// public static char[] rCheckSum(this char[] _Data) { try { return rCheckSum(_Data.rToByte()).rToChar(); } catch { return null; } } #endregion #region Encodings & Decoding /// /// 문자열을 Char배열로(Encodeing by Default) /// /// /// 코드페이지 오류시 null public static char[] rToEncChar(this string _inString) { return _inString.rToEncChar(CodeType.Default); } /// /// 문자열을 Char배열로 /// /// /// /// 코드페이지 오류시 null public static char[] rToEncChar(this string _inString, CodeType _CodeType) { return _inString.rToByteArray(_CodeType).rToChar(); } /// /// 문자열을 Byte배열로(Encodeing by Default) /// /// /// 코드페이지 오류시 null public static byte[] rToByteArray(this string _inString) { return _inString.rToByteArray(CodeType.Default); } /// /// 문자열을 Byte배열로 /// /// /// /// public static byte[] rToByteArray(this string _inString, CodeType _CodeType) { return _CodeType.rGetEncoding().GetBytes(_inString); } /// /// 문자열로 CodeType 형식 구하기 /// /// /// public static CodeType rGetCodeType(this Encoding _Encoding) { CodeType result = CodeType.Default; try { switch (_Encoding.ToString()) { case "ASCII": result = CodeType.ASCII; break; case "BigEndianUnicode": result = CodeType.BigEdianUnicode; break; case "Unicode": result = CodeType.Unicode; break; case "UTF7": result = CodeType.UTF7; break; case "UTF8": result = CodeType.UTF8; break; case "UTF32": result = CodeType.UTF32; break; case "Default": result = CodeType.Default; break; } } catch { } return result; } /// /// Code Type에 /// 맞는 Encoding 가져오기 /// /// /// Encoding public static Encoding rGetEncoding(this CodeType _CodeType) { Encoding result = Encoding.Default; try { switch (_CodeType) { case CodeType.ASCII: result = Encoding.ASCII; break; case CodeType.BigEdianUnicode: result = Encoding.BigEndianUnicode; break; case CodeType.Unicode: result = Encoding.Unicode; break; case CodeType.UTF7: result = Encoding.UTF7; break; case CodeType.UTF8: result = Encoding.UTF8; break; case CodeType.UTF32: result = Encoding.UTF32; break; case CodeType.Default: result = Encoding.Default; break; } } catch { result = Encoding.Default; } return result; } /// /// Char배열을 문자열로(Encodeing by Default) /// /// /// 코드페이지 오류시 빈문자열 public static string rToEncString(this char[] _inChars) { return _inChars.rToByte().rToEncString(CodeType.Default); } /// /// Char배열을 문자열로 /// /// /// /// 코드페이지 오류시 빈문자열 public static string rToEncString(this char[] _inChars, CodeType _CodeType) { return _inChars.rToByte().rToEncString(_CodeType); } /// /// Byte배열을 문자열로(Encodeing by Default) /// /// /// 코드페이지 오류시 빈문자열 public static string rToEncString(this byte[] _inBytes) { return _inBytes.rToEncString(CodeType.Default); } /// /// Byte배열을 문자열로 /// /// /// /// 코드페이지 오류시 빈문자열 public static string rToEncString(this byte[] _inBytes, CodeType _CodeType) { string result = null; try { switch (_CodeType) { case CodeType.ASCII: result = Encoding.ASCII.GetString(_inBytes); break; case CodeType.BigEdianUnicode: result = Encoding.BigEndianUnicode.GetString(_inBytes); break; case CodeType.Unicode: result = Encoding.Unicode.GetString(_inBytes); break; case CodeType.UTF7: result = Encoding.UTF7.GetString(_inBytes); break; case CodeType.UTF8: result = Encoding.UTF8.GetString(_inBytes); break; case CodeType.UTF32: result = Encoding.UTF32.GetString(_inBytes); break; case CodeType.Default: result = Encoding.Default.GetString(_inBytes); break; } } catch { result = string.Empty; } return result; } /// /// Base64 인코딩을 합니다 /// /// 입력문자열 /// 오류시 빈문자열 public static string rBase64Encode(this string _inString) { return _inString.rBase64Encode(CodeType.Default); } /// /// Base64 인코딩을 합니다 /// /// 입력문자열 /// /// 코드페이지 오류시 빈문자열 public static string rBase64Encode(this string _inString, CodeType _CodeType) { try { return Convert.ToBase64String(_inString.rToByteArray(_CodeType)); } catch { return string.Empty; } } /// /// Base64 인코딩을 합니다 /// /// 엔코딩한 문자열 Byte배열 /// 오류시 빈문자열 public static string rBase64Encode(this byte[] _inBytes) { try { return Convert.ToBase64String(_inBytes); } catch { return string.Empty; } } /// /// Base64 인코딩을 합니다 /// /// 엔코딩한 문자열 Byte배열 /// 오류시 빈문자열 public static string rBase64Encode(this char[] _inChars) { try { return _inChars.rToByte().rBase64Encode(); } catch { return string.Empty; } } /// /// Base64 인코딩을 합니다 /// /// /// public static char[] rBase64EncodeChars(this byte[] _inBytes) { try { return rBase64Encode(_inBytes).ToCharArray(); } catch { return null; } } /// /// Base64 인코딩을 합니다 /// /// /// public static char[] rBase64EncodeChars(this string _inString) { return _inString.rBase64Encode(CodeType.Default).ToCharArray(); } /// /// Base64 인코딩을 합니다 /// /// /// /// public static char[] rBase64EncodeChars(this string _inString, CodeType _CodeType) { return _inString.rBase64Encode(_CodeType).ToCharArray(); } /// /// Base64 인코딩을 합니다 /// /// /// public static byte[] rBase64EncodeBytes(this string _inString) { return _inString.rBase64EncodeChars().rToByte(); } /// /// Base64 인코딩을 합니다 /// /// /// /// public static byte[] rBase64EncodeBytes(this string _inString, CodeType _CodeType) { return _inString.rBase64EncodeChars(_CodeType).rToByte(); } /// /// Base64 디코딩을 합니다 /// /// 입력문자열 /// 오류시 빈문자열 public static byte[] rBase64DecodeBytes(this string _inString) { try { return Convert.FromBase64String(_inString); } catch { return null; } } /// /// Base64 디코딩을 합니다 /// /// 입력문자열 /// 코드페이지 오류시 빈문자열 public static byte[] rBase64DecodeBytes(this byte[] _inBytes) { return _inBytes.rToChar().rBase64DecodeBytes(); } /// /// Base64 디코딩을 합니다 /// /// /// public static byte[] rBase64DecodeBytes(this char[] _inChars) { try { return Convert.FromBase64CharArray(_inChars, 0, _inChars.Length); } catch { return null; } } /// /// Base64 디코딩을 합니다 /// /// /// public static string rBase64Decode(this string _inString) { return _inString.rBase64Decode(CodeType.Default); } /// /// Base64 디코딩을 합니다 /// /// /// /// public static string rBase64Decode(this string _inString, CodeType _CodeType) { return _inString.rBase64DecodeBytes().rToEncString(_CodeType); } /// /// Base64 디코딩을 합니다 /// /// /// public static string rBase64Decode(this byte[] _inBytes) { return _inBytes.rToChar().rBase64Decode(); } /// /// Base64 디코딩을 합니다 /// /// /// public static string rBase64Decode(this char[] _inChars) { return _inChars.rBase64Decode(CodeType.Default); } /// /// Base64 디코딩을 합니다 /// /// /// /// public static string rBase64Decode(this byte[] _inBytes, CodeType _CodeType) { return _inBytes.rToChar().rBase64Decode(_CodeType); } /// /// Base64 디코딩을 합니다 /// /// /// /// public static string rBase64Decode(this char[] _inChars, CodeType _CodeType) { try { return Convert.FromBase64CharArray(_inChars, 0, _inChars.Length).rToEncString(_CodeType); } catch { return null; } } #endregion #region 컨트롤 문자를 컨트롤 문자명으로 찾기 /// /// Find Cotrol ANSI by Cotrol Code Name /// /// /// public static Ctrl_ASCII rGetControlANSI(this string _inCode) { try { return (Ctrl_ASCII)Array.IndexOf(Enum.GetNames(typeof(Ctrl_ASCII)), _inCode.ToUpper().Replace("<", "").Replace(">", "")); } catch { return Ctrl_ASCII.Null; } } /// /// Find Cotrol ANSI by Cotrol Code Name /// /// /// public static char rGetControlChar(this string _inCode) { return (char)(_inCode.Replace("<", "").Replace(">", "").rGetControlANSI()); } /// /// Find Cotrol ANSI by Cotrol Code Name /// /// /// public static byte rGetControlByte(this string _inCode) { return (byte)(_inCode.Replace("<", "").Replace(">", "").rGetControlANSI()); } /// /// Find Cotrol ANSI by Cotrol Code Name /// /// /// public static string rGetControlString(this string _inCode) { try { return _inCode.Equals(string.Empty) ? string.Empty : Convert.ToString(_inCode.Replace("<", "").Replace(">", "").rGetControlChar()); } catch { return string.Empty; } } /// /// ASCII Control Code to CodeName /// 문자에 대한 ASCII control 코드명 구하기 /// /// /// public static string rReplaceANSIToTag(this string _inString) { try { _inString = _inString.Replace("\0", ""); foreach (string ctrlCode in Enum.GetNames(typeof(Ctrl_ASCII))) { if (!ctrlCode.Equals(string.Empty)) _inString = _inString.Replace(ctrlCode.rGetControlString(), string.Format("<{0}>", ctrlCode)); } return _inString; } catch { return string.Empty; } } /// /// CodeName to ASCII Control Code /// 문자열을 바이트 표시 문자열로 변환 /// /// /// public static string rReplaceToANSI(this string _inString) { try { foreach (string ctrlCode in Enum.GetNames(typeof(Ctrl_ASCII))) { if (!ctrlCode.Equals(string.Empty)) _inString = _inString.Replace(string.Format("<{0}>", ctrlCode), ctrlCode.rGetControlString()); } return _inString; } catch { return string.Empty; } } /// /// 바이트 표시 형식 문자열을 바이트 배열로 반환 /// /// /// public static byte[] rReplaceToByte(this string _inString) { try { string strbuff = _inString; foreach (string ctrlCode in Enum.GetNames(typeof(Ctrl_ASCII))) { if (!ctrlCode.Equals(string.Empty)) strbuff = strbuff.Replace(string.Format("<{0}>", ctrlCode), ctrlCode.rGetControlString()); } string[] inChars = strbuff.Split('<'); byte[] result = new byte[0]; byte[] makeBlock = new byte[0]; foreach (string aChar in inChars) { if (aChar.StartsWith("0x")) { string[] inChars2 = aChar.Split('>'); foreach (string aChar2 in inChars2) { if (!aChar2.Equals(string.Empty)) { if (aChar2.StartsWith("0x")) { makeBlock = new byte[] { aChar2.Replace("0x", "").rHexStrToByte() }; } else { makeBlock = aChar2.rToByteArray(); } int addPos = result.Length; Array.Resize(ref result, result.Length + makeBlock.Length); Array.Copy(makeBlock, 0, result, addPos, makeBlock.Length); } } } else { makeBlock = aChar.rToByteArray(); int addPos = result.Length; Array.Resize(ref result, result.Length + makeBlock.Length); Array.Copy(makeBlock, 0, result, addPos, makeBlock.Length); } } return result; } catch { return null; } } #endregion #region byte수를 문자열로 표시 /// /// 바이트수를 문자열로 표시 /// /// 바이트수 /// public static string rByteCountToString(this int _inInt) { return rByteCountToString((long)_inInt); } /// /// 바이트수를 문자열로 표시 /// /// 바이트수 /// public static string rByteCountToString(this long _inLong) { return rByteCountToString((double)_inLong); } /// /// 바이트수를 문자열로 표시 /// /// /// public static string rByteCountToString(this double _inDeouble) { string result = string.Empty; try { result = string.Format("{0}Bytes", _inDeouble.ToString("#,##0")); if (_inDeouble > 512d) { if (_inDeouble > (1024d * 512d)) { if (_inDeouble > (1024d * 1024d * 512d)) { if (_inDeouble > (1024d * 1024d * 1024d * 512d)) { result = string.Format("{0}TB", (_inDeouble / 1024d / 1024d / 1024d / 1024d).ToString("#,##0.00")); } else { result = string.Format("{0}GB", (_inDeouble / 1024d / 1024d / 1024d).ToString("#,##0.00")); } } else { result = string.Format("{0}MB", (_inDeouble / 1024d / 1024d).ToString("#,##0.00")); } } else { result = string.Format("{0}KB", (_inDeouble / 1024d).ToString("#,##0.00")); } } } catch { } return result; } #endregion /// /// 문자열 중 숫자만 뽑기 /// /// /// public static string rGetOnlyNum(this string _inString) { string result = string.Empty; try { if (_inString != null && _inString.Length > 0) { char[] getChars = _inString.ToCharArray(); foreach (char getChar in getChars) { if (getChar.rIsNum()) { result += getChar; } } } } catch { result = string.Empty; } return result; } } }