public byte[] GetBytes(string str) { try { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return(bytes); } catch (Exception ex) { ER_Tools._WriteEventLog(string.Format("Caught exception: {0} \r\n Stack Trace: {1}", ex.Message, ex.StackTrace), EventLogType.exception); throw; } }
public string GetString(byte[] bytes) { try { int arraylength = bytes.Length % 2 != 0 ? bytes.Length + 1 : bytes.Length; char[] chars = new char[arraylength / sizeof(char)]; System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); return(new string(chars)); } catch (Exception ex) { ER_Tools._WriteEventLog(string.Format("Caught exception: {0} \r\n Stack Trace: {1}", ex.Message, ex.StackTrace), EventLogType.error); return("Error getting string bytes. " + ex.ToString());; } }