private bool SendToInstrument(string cmdStr) { bool status = true; try { Stream stm = socket.GetStream(); ASCIIEncoding asen = new ASCIIEncoding(); byte[] ba = asen.GetBytes(cmdStr); //Add the termintaion character byte[] term = { 0x0a };// { 0x0d, 0x0a }; byte[] buf = new byte[asen.GetByteCount(cmdStr) + term.GetLength(0)]; Array.Copy(ba, buf, asen.GetByteCount(cmdStr)); Array.Copy(term, 0, buf, asen.GetByteCount(cmdStr), term.GetLength(0)); stm.Write(buf, 0, buf.Length); } catch (Exception e) { output.ShowError(e.Message); status = false; } return(status); }
private static byte[] GetMultipartFormData(Dictionary <string, object> postParameters, string boundary) { Stream formDataStream = new System.IO.MemoryStream(); bool needsCLRF = false; ASCIIEncoding encoding = new ASCIIEncoding(); foreach (var param in postParameters) { if (needsCLRF) { formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n")); } needsCLRF = true; if (param.Value is CB.CloudFile) { CB.CloudFile fileToUpload = (CB.CloudFile)param.Value; // Add just the first part of this param, since we will write the file data directly to the Stream string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n", boundary, param.Key, fileToUpload.FileName ?? param.Key, fileToUpload.ContentType ?? "application/octet-stream"); formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header)); // Write the file data directly to the Stream, rather than serializing it to a string. formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length); } else { string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}", boundary, param.Key, param.Value); formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData)); } } // Add the end of the request. Start with a newline string footer = "\r\n--" + boundary + "--\r\n"; formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer)); // Dump the Stream into a byte[] formDataStream.Position = 0; byte[] formData = new byte[formDataStream.Length]; formDataStream.Read(formData, 0, formData.Length); formDataStream.Close(); return(formData); }
public override byte[] GetEncoded() { byte[] localArray = new byte[this.PacketLength]; byte[] arr = base.GetEncoded(); int idx = arr.Length; arr.CopyTo(localArray, 0); ASCIIEncoding ascii = new ASCIIEncoding(); Encoding w1251 = Encoding.GetEncoding(1251); SupportOperations.ToBigEndian(RequestId).CopyTo(localArray, idx); idx += 8; ascii.GetBytes(DestinationAddr).CopyTo(localArray, idx); idx += ascii.GetByteCount(DestinationAddr); localArray[idx++] = 0; localArray[idx++] = (byte)ServiceState; SupportOperations.ToBigEndian(ServiceTypeId).CopyTo(localArray, idx); idx += 8; w1251.GetBytes(ServiceClass).CopyTo(localArray, idx); idx += w1251.GetByteCount(ServiceClass); localArray[idx++] = 0; w1251.GetBytes(ServiceDescr).CopyTo(localArray, idx); idx += w1251.GetByteCount(ServiceDescr); localArray[idx++] = 0; SupportOperations.ToBigEndian(ServiceCost).CopyTo(localArray, idx); idx += 4; localArray[idx++] = (byte)ServicePeriod; localArray[idx++] = (byte)ActivationType; ascii.GetBytes(ActivationAddr).CopyTo(localArray, idx); idx += ascii.GetByteCount(ActivationAddr); localArray[idx++] = 0; w1251.GetBytes(ActivationMessage).CopyTo(localArray, idx); idx += w1251.GetByteCount(ActivationMessage); localArray[idx++] = 0; SupportOperations.ToBigEndian(ServiceId).CopyTo(localArray, idx); idx += 8; return(localArray); }
public void PosTest2() { ASCIIEncoding ascii; char[] chars; int charIndex, count; byte[] bytes; int byteIndex; ascii = new ASCIIEncoding(); InitializeCharacterArray(out chars); charIndex = _generator.GetInt32(-55) % chars.Length; count = _generator.GetInt32(-55) % (chars.Length - charIndex) + 1; int minLength = ascii.GetByteCount(chars, charIndex, count); int length = minLength + _generator.GetInt32(-55) % (Int16.MaxValue - minLength); bytes = new byte[length]; for (int i = 0; i < bytes.Length; ++i) { bytes[i] = 0; } byteIndex = _generator.GetInt32(-55) % (bytes.Length - minLength + 1); DoPosTest(ascii, chars, charIndex, count, bytes, byteIndex); }
public void PosTest2() { ASCIIEncoding ascii; string source; int charIndex, count; byte[] bytes; int byteIndex; ascii = new ASCIIEncoding(); source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH); charIndex = _generator.GetInt32(-55) % source.Length; count = _generator.GetInt32(-55) % (source.Length - charIndex) + 1; int minLength = ascii.GetByteCount(source.Substring(charIndex, count)); int length = minLength + _generator.GetInt32(-55) % (Int16.MaxValue - minLength); bytes = new byte[length]; for (int i = 0; i < bytes.Length; ++i) { bytes[i] = 0; } byteIndex = _generator.GetInt32(-55) % (bytes.Length - minLength + 1); DoPosTest(ascii, source, charIndex, count, bytes, byteIndex); }
public override byte[] GetEncoded() { byte[] localArray = new byte[this.PacketLength]; byte[] arr = base.GetEncoded(); int idx = arr.Length; arr.CopyTo(localArray, 0); ASCIIEncoding ascii = new ASCIIEncoding(); ascii.GetBytes(ServiceType).CopyTo(localArray, idx); idx += ascii.GetByteCount(ServiceType); localArray[idx] = 0; idx++; Source.GetEncoded().CopyTo(localArray, idx); idx += Source.GetEncoded().Length; Destination.GetEncoded().CopyTo(localArray, idx); idx += Destination.GetEncoded().Length; localArray[idx++] = EsmClass; localArray[idx++] = RegisteredDelivery; localArray[idx++] = (byte)DataCoding; foreach (OptionalParameter op in OptionalParamList) { op.GetEncoded().CopyTo(localArray, idx); idx += op.GetEncoded().Length; } return(localArray); }
public void Filename_9600_Space_8() { string portName; int baudRate = 9600; int parity = (int)Parity.Space; int dataBits = 8; string fileName = portName = GetTestFilePath(); FileStream testFile = File.Open(fileName, FileMode.Create); ASCIIEncoding asciiEncd = new ASCIIEncoding(); string testStr = "Hello World"; testFile.Write(asciiEncd.GetBytes(testStr), 0, asciiEncd.GetByteCount(testStr)); testFile.Close(); try { VerifyCtor(portName, baudRate, parity, dataBits, typeof(ArgumentException), ThrowAt.Open); } catch (Exception) { throw; } finally { File.Delete(fileName); } }
private void button3_Click(object sender, EventArgs e) { String serializedMap = Program.map.serializeMap(); SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "text file|*.txt"; saveFileDialog1.Title = "Save a map File"; saveFileDialog1.ShowDialog(); // If the file name is not an empty string open it for saving. if (saveFileDialog1.FileName != "") { // Saves the Image via a FileStream created by the OpenFile method. System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile(); ASCIIEncoding ascii = new ASCIIEncoding(); int byteCount = ascii.GetByteCount(serializedMap.ToCharArray(), 0, serializedMap.Length); Byte[] bytes = new Byte[byteCount]; int bytesEncodedCount = ascii.GetBytes(serializedMap.ToCharArray(), 0, serializedMap.Length, bytes, 0); fs.Write(bytes); fs.Close(); } }
public void ValidateEnumerator() { string alphabetName = utilityObj.xmlUtil.GetTextValue( Constants.DnaDerivedSequenceNode, Constants.AlphabetNameNode); IAlphabet alphabet = Utility.GetAlphabet(alphabetName); string expectedSequence = (utilityObj.xmlUtil.GetTextValue( Constants.DnaDerivedSequenceNode, Constants.ExpectedDerivedSequence)); Sequence sequence = new Sequence(alphabet, encodingObj.GetBytes(expectedSequence)); //Validate Count Assert.AreEqual(encodingObj.GetByteCount(expectedSequence), sequence.Count); ApplicationLog.WriteLine(string.Concat( "Sequence BVT: Validation of Count operation completed successfully.")); //Validate Enumerator. string sequenceString = ""; IEnumerator <byte> enumFromSequence = sequence.GetEnumerator(); while (enumFromSequence.MoveNext()) { sequenceString += ((char)enumFromSequence.Current); } Assert.AreEqual(sequenceString, expectedSequence); ApplicationLog.WriteLine(string.Concat( "Sequence BVT: Validation of Enumerator operation completed successfully.")); }
protected void Send(string toSend) { ASCIIEncoding aSCIIEncoding = new ASCIIEncoding(); uint num = (uint)aSCIIEncoding.GetByteCount(toSend); if (this.TxTerm != null) { num += (uint)this.TxTerm.GetLength(0); } byte[] array = new byte[num]; byte[] bytes = aSCIIEncoding.GetBytes(toSend); int i; for (i = 0; i <= bytes.GetUpperBound(0); i++) { array[i] = bytes[i]; } if (this.TxTerm != null) { int j = 0; while (j <= this.TxTerm.GetUpperBound(0)) { array[i] = (byte)this.TxTerm[j]; j++; i++; } } base.Send(array); }
public static void Main() { Byte[] bytes; // Unicode characters. Char[] chars = new Char[] { '\u0023', // # '\u0025', // % '\u03a0', // Pi '\u03a3' // Sigma }; ASCIIEncoding ascii = new ASCIIEncoding(); int byteCount = ascii.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; int bytesEncodedCount = ascii.GetBytes(chars, 1, 2, bytes, 0); Console.WriteLine( "{0} bytes used to encode characters.", bytesEncodedCount ); Console.Write("Encoded bytes: "); foreach (Byte b in bytes) { Console.Write("[{0}]", b); } Console.WriteLine(); }
public byte[] Encrypt(string data) { ASCIIEncoding asciiencoding = new ASCIIEncoding(); asciiencoding.GetByteCount(data); byte[] bytes = asciiencoding.GetBytes(data); return(this._sp.Encrypt(bytes, false)); }
private void DoPosTest(string source, int expectedValue) { ASCIIEncoding ascii; int actualValue; ascii = new ASCIIEncoding(); actualValue = ascii.GetByteCount(source); Assert.Equal(expectedValue, actualValue); }
private void DoNegAOORTest(char[] chars, int startIndex, int count) { ASCIIEncoding ascii = new ASCIIEncoding(); Assert.Throws <ArgumentOutOfRangeException>(() => { ascii.GetByteCount(chars, startIndex, count); }); }
private void DoPosTest(char[] chars, int startIndex, int count, int expectedValue) { ASCIIEncoding ascii; int actualValue; ascii = new ASCIIEncoding(); actualValue = ascii.GetByteCount(chars, startIndex, count); Assert.Equal(expectedValue, actualValue); }
public static void SendMessage(TcpClient socket, string message) { ASCIIEncoding encoder = new ASCIIEncoding(); var result = socket.GetStream(); var byteCount = encoder.GetByteCount(message); var bytes = new byte[byteCount]; var encodedMesage = encoder.GetBytes(message, bytes); result.Write(bytes, 0, byteCount); }
public void NegTest1() { ASCIIEncoding ascii; string source = null; ascii = new ASCIIEncoding(); Assert.Throws<ArgumentNullException>(() => { ascii.GetByteCount(source); }); }
public static void Main() { String chars = "ASCII Encoding Example"; ASCIIEncoding ascii = new ASCIIEncoding(); int byteCount = ascii.GetByteCount(chars); Console.WriteLine( "{0} bytes needed to encode string.", byteCount ); }
public void NegTest1() { ASCIIEncoding ascii; string source = null; ascii = new ASCIIEncoding(); Assert.Throws <ArgumentNullException>(() => { ascii.GetByteCount(source); }); }
public void NegTest1() { ASCIIEncoding ascii; char[] chars = null; ascii = new ASCIIEncoding(); Assert.Throws<ArgumentNullException>(() => { ascii.GetByteCount(chars, 0, 0); }); }
public void NegTest1() { ASCIIEncoding ascii; char[] chars = null; ascii = new ASCIIEncoding(); Assert.Throws <ArgumentNullException>(() => { ascii.GetByteCount(chars, 0, 0); }); }
private IntPtr GetNativeAnsiString(string str) { ASCIIEncoding strEncoder = new ASCIIEncoding(); int size = strEncoder.GetByteCount(str); IntPtr pStr = Memory.Zalloc(size + 1); byte[] buffer = strEncoder.GetBytes(str); Marshal.Copy(buffer, 0, pStr, size); return(pStr); }
public void GetByteCountTest() { ASCIIEncoding target = new ASCIIEncoding(); // TODO: Initialize to an appropriate value char[] chars = null; // TODO: Initialize to an appropriate value int index = 0; // TODO: Initialize to an appropriate value int count = 0; // TODO: Initialize to an appropriate value int expected = 0; // TODO: Initialize to an appropriate value int actual; actual = target.GetByteCount(chars, index, count); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
public EncoderParameter(Encoder encoder, string value) { this.encoder = encoder; ASCIIEncoding ascii = new ASCIIEncoding(); int asciiByteCount = ascii.GetByteCount(value); byte[] bytes = new byte[asciiByteCount]; ascii.GetBytes(value, 0, value.Length, bytes, 0); this.valuesCount = bytes.Length; this.type = EncoderParameterValueType.ValueTypeAscii; this.valuePtr = Marshal.AllocHGlobal(valuesCount); Marshal.Copy(bytes, 0, this.valuePtr, valuesCount); }
public static void Main() { // Unicode characters. Char[] chars = new Char[] { '\u0023', // # '\u0025', // % '\u03a0', // Pi '\u03a3' // Sigma }; ASCIIEncoding ascii = new ASCIIEncoding(); int byteCount = ascii.GetByteCount(chars, 1, 2); Console.WriteLine( "{0} bytes needed to encode characters.", byteCount ); }
public static bool ValidateACIIEncoding(string text) { ASCIIEncoding encoding = Encoding.GetEncoding(Encoding.ASCII.CodePage, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback) as ASCIIEncoding; try { encoding.GetByteCount(text); } catch (EncoderFallbackException encoderFallbackException1) { EncoderFallbackException encoderFallbackException = encoderFallbackException1; CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] index = new object[] { encoderFallbackException.Index }; throw new FormatException(string.Format(invariantCulture, "Character at index {0} is not valid ASCII", index), encoderFallbackException); } return(true); }
private void FlushOutqueue() { if (m_wwwCurrentSending == null && m_outQueue.Count > 0) { var messages = string.Join(",", m_outQueue.ToArray()); char[] msg = string.Format("[{0}]", messages).ToCharArray(); Array.Resize(ref m_lastSentBytes, AsciiEncoding.GetByteCount(msg)); AsciiEncoding.GetBytes(msg, 0, msg.Length, m_lastSentBytes, 0); StartSend(m_lastSentBytes); m_outQueue.Clear(); } }
public static string checksum(string str) { // the $ and * should NOT be included in computation, see http://joe.mehaffey.com/mag-errata.htm ASCIIEncoding enc = new ASCIIEncoding(); uint l = (uint)enc.GetByteCount(str); byte[] s = enc.GetBytes(str); int sum = 0; int len = s.GetLength(0); for (int i = 1; i < len; i++) { sum ^= s[i]; } sum = sum & 0xFF; return(string.Format("*{0:X2}\r\n", sum)); }
public override byte[] GetEncoded() { byte[] localArray = new byte[PacketLength]; int idx = base.GetEncoded().Length; base.GetEncoded().CopyTo(localArray, 0); ASCIIEncoding ascii = new ASCIIEncoding(); ascii.GetBytes(m_messageId).CopyTo(localArray, idx); idx += ascii.GetByteCount(m_messageId); localArray[idx] = 0;//нуль - терминатор m_Source.GetEncoded().CopyTo(localArray, idx + 1); return(localArray); }
public void PosTest2() { ASCIIEncoding ascii; byte[] bytes; int byteIndex, byteCount; char[] chars; int charIndex; string source; ascii = new ASCIIEncoding(); source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH); bytes = new byte[ascii.GetByteCount(source)]; ascii.GetBytes(source, 0, source.Length, bytes, 0); byteIndex = _generator.GetInt32(-55) % bytes.Length; byteCount = _generator.GetInt32(-55) % (bytes.Length - byteIndex) + 1; chars = new char[byteCount + _generator.GetInt32(-55) % c_MAX_STRING_LENGTH]; charIndex = _generator.GetInt32(-55) % (chars.Length - byteCount + 1); DoPosTest(ascii, bytes, byteIndex, byteCount, chars, charIndex); }
public void NegTest7() { ASCIIEncoding ascii; string source; int charIndex, count; byte[] bytes; int byteIndex; ascii = new ASCIIEncoding(); source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH); charIndex = _generator.GetInt32(-55) % source.Length; count = _generator.GetInt32(-55) % (source.Length - charIndex) + 1; int minLength = ascii.GetByteCount(source.Substring(charIndex, count)); bytes = new byte[minLength]; byteIndex = bytes.Length + _generator.GetInt32(-55) % (int.MaxValue - bytes.Length); DoNegAOORTest(ascii, source, charIndex, count, bytes, byteIndex); }
public void NegTest7() { ASCIIEncoding ascii; char[] chars; int charIndex, count; byte[] bytes; int byteIndex; ascii = new ASCIIEncoding(); InitializeCharacterArray(out chars); charIndex = _generator.GetInt32(-55) % chars.Length; count = _generator.GetInt32(-55) % (chars.Length - charIndex) + 1; int minLength = ascii.GetByteCount(chars, charIndex, count); bytes = new byte[minLength]; byteIndex = bytes.Length + _generator.GetInt32(-55) % (int.MaxValue - bytes.Length); DoNegAOORTest(ascii, chars, charIndex, count, bytes, byteIndex); }
public void PortName_FileName() { string fileName = "PortNameEqualToFileName.txt"; FileStream testFile = File.Open(fileName, FileMode.Create); ASCIIEncoding asciiEncd = new ASCIIEncoding(); string testStr = "Hello World"; testFile.Write(asciiEncd.GetBytes(testStr), 0, asciiEncd.GetByteCount(testStr)); testFile.Close(); Debug.WriteLine("Verifying setting PortName={0}", fileName); VerifyException(fileName, ThrowAt.Open, typeof(ArgumentException), typeof(InvalidOperationException)); Debug.WriteLine("Verifying setting PortName={0}", Environment.CurrentDirectory + fileName); VerifyException(Environment.CurrentDirectory + fileName, ThrowAt.Open, typeof(ArgumentException), typeof(InvalidOperationException)); File.Delete(fileName); }
public void NegTest8() { ASCIIEncoding ascii; string source; int charIndex, count; byte[] bytes; int byteIndex; ascii = new ASCIIEncoding(); source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH); charIndex = _generator.GetInt32(-55) % source.Length; count = _generator.GetInt32(-55) % (source.Length - charIndex) + 1; int minLength = ascii.GetByteCount(source.Substring(charIndex, count)); bytes = new byte[_generator.GetInt32(-55) % minLength]; byteIndex = 0; Assert.Throws<ArgumentException>(() => { ascii.GetBytes(source, charIndex, count, bytes, byteIndex); }); }
public void NegTest8() { ASCIIEncoding ascii; char[] chars; int charIndex, count; byte[] bytes; int byteIndex; ascii = new ASCIIEncoding(); InitializeCharacterArray(out chars); charIndex = _generator.GetInt32(-55) % chars.Length; count = _generator.GetInt32(-55) % (chars.Length - charIndex) + 1; int minLength = ascii.GetByteCount(chars, charIndex, count); bytes = new byte[_generator.GetInt32(-55) % minLength]; byteIndex = 0; Assert.Throws<ArgumentException>(() => { ascii.GetBytes(chars, charIndex, count, bytes, byteIndex); }); }
private void DoNegAOORTest(char[] chars, int startIndex, int count) { ASCIIEncoding ascii = new ASCIIEncoding(); Assert.Throws<ArgumentOutOfRangeException>(() => { ascii.GetByteCount(chars, startIndex, count); }); }