public void PosTest4() { Char[] chars = GetCharArray(10); Byte[] bytes = new Byte[20]; UnicodeEncoding uEncoding = new UnicodeEncoding(); int byteCount = uEncoding.GetBytes(chars, 0, 10, bytes, 0); int actualValue; actualValue = uEncoding.GetCharCount(bytes, 20, 0); Assert.Equal(0, actualValue); }
public void NegTest1() { Char[] chars = GetCharArray(10); Byte[] bytes = new Byte[20]; UnicodeEncoding uEncoding = new UnicodeEncoding(); int byteCount = uEncoding.GetBytes(chars, 0, 10, bytes, 0); int actualValue; Assert.Throws<ArgumentNullException>(() => { actualValue = uEncoding.GetCharCount(null, 0, 0); }); }
static public int GetCharCount__A_Byte(IntPtr l) { try { System.Text.UnicodeEncoding self = (System.Text.UnicodeEncoding)checkSelf(l); System.Byte[] a1; checkArray(l, 2, out a1); var ret = self.GetCharCount(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
public String Md5Encryption(String src) { System.Text.UnicodeEncoding encode = new System.Text.UnicodeEncoding(); System.Text.Decoder decode = encode.GetDecoder(); MD5 md5 = new MD5CryptoServiceProvider(); byte[] password = new byte[encode.GetByteCount(src)]; char[] char_password = new char[encode.GetCharCount(password)]; password = encode.GetBytes(src); password = md5.ComputeHash(password); int length = encode.GetByteCount(src); if (length > 16) { length = 16; } decode.GetChars(password, 0, length, char_password, 0); return(new String(char_password)); }
//public static string SerializeObjectToXML(T obj) public static string SerializeObjectToXML(Object obj) { try { //string xmlString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(obj.GetType()); string s = string.Empty; XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); SoapFormatter xsoap = new SoapFormatter(); xsoap.Serialize(memoryStream, obj); //xmlString = UTF8ByteArrayToString(memoryStream.ToArray()); UnicodeEncoding encoding = new UnicodeEncoding(); byte[] bytes = memoryStream.ToArray(); char[] chars = new char[encoding.GetCharCount(bytes, 0, (int)memoryStream.Length)]; return new string(chars); } catch (Exception ex) { ex.GetType(); return string.Empty; } }
private void UsingMemoryStream() { System.Threading.Thread.Sleep(5000); Console.WriteLine("Memory Stream Example"); int count; byte[] byteArray; char[] charArray; UnicodeEncoding uniEncoding = new UnicodeEncoding(); byte[] firstString = uniEncoding.GetBytes("Invalid file path character are: "); byte[] secondString = uniEncoding.GetBytes(Path.GetInvalidFileNameChars()); using (MemoryStream memStream = new MemoryStream(100)) { memStream.Write(firstString, 0, firstString.Length); count = 0; while(count < secondString.Length) { memStream.WriteByte(secondString[count++]); } Console.WriteLine("Capacity = {0}, Length = {1}, Position = {2}", memStream.Capacity, memStream.Length, memStream.Position); memStream.Seek(0, SeekOrigin.Begin); byteArray = new byte[memStream.Length]; count = memStream.Read(byteArray, 0, 20); while(count<memStream.Length) { byteArray[count++] = Convert.ToByte(memStream.ReadByte()); } charArray = new char[uniEncoding.GetCharCount(byteArray,0,count)]; uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0); Console.WriteLine(charArray); } }
public void ZeroLengthArrays () { UnicodeEncoding encoding = new UnicodeEncoding (); encoding.GetCharCount (new byte [0]); encoding.GetChars (new byte [0]); encoding.GetCharCount (new byte [0], 0, 0); encoding.GetChars (new byte [0], 0, 0); encoding.GetChars (new byte [0], 0, 0, new char [0], 0); encoding.GetByteCount (new char [0]); encoding.GetBytes (new char [0]); encoding.GetByteCount (new char [0], 0, 0); encoding.GetBytes (new char [0], 0, 0); encoding.GetBytes (new char [0], 0, 0, new byte [0], 0); encoding.GetByteCount (""); encoding.GetBytes (""); }
public void TestEncodingGetCharCount () { byte[] b = new byte[] {255, 254, 115, 0, 104, 0, 105, 0}; UnicodeEncoding encoding = new UnicodeEncoding (); Assert.AreEqual (3, encoding.GetCharCount (b, 2, b.Length - 2), "GetCharCount #1"); }
public bool Send(string mail) { try { UnicodeEncoding encoding = new UnicodeEncoding(); string data_string = string.Format("{0}:{1}:{2}", Process.GetCurrentProcess().Id, 3, mail); byte[] data_bytes = encoding.GetBytes(data_string); int byteCount = data_bytes.Length; if (byteCount > MAX_MESSAGE_SIZE) { Console.WriteLine(String.Format( "message size {0} bytes but is limited to {1} bytes, will be truncated", byteCount, MAX_MESSAGE_SIZE)); byteCount = MAX_MESSAGE_SIZE; } ensureMailSlot(); mailSlot.Write(data_bytes, 0, byteCount); mailSlot.Flush(); Console.WriteLine("sending " + data_string.Substring(0, encoding.GetCharCount(data_bytes, 0, byteCount))); } catch (IOException ioe) { this.Close(); Console.WriteLine(String.Format("{0} Exception caught.", ioe)); return false; } return true; }