/// <summary> /// Decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array. /// </summary> /// <param name="bytes">The byte array containing the sequence of bytes to decode</param> /// <param name="byteIndex">The index of the first byte to decode</param> /// <param name="byteCount">The number of bytes to decode</param> /// <param name="chars">The character array to contain the resulting set of characters</param> /// <param name="charIndex">The index at which to start writing the resulting set of characters</param> /// <returns>The actual number of characters written into <paramref name="chars"/></returns> /// <seealso cref="System.Text.Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32)"/> public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { byteCount = mEncoding.CalculateCharByteCount(bytes, ref byteIndex, byteCount); // Remove our String Storage calculations int chars_written = mDec.GetChars(bytes, byteIndex, byteCount, chars, charIndex); return(chars_written); }
public IHttpActionResult CheckTransactionPassword(long account_number, string transaction_password) { try { string password = db.AccountHolders.Where(a => a.account_number == account_number).Select(a => a.transaction_password).ToList()[0]; System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); System.Text.Decoder utf8Decode = encoder.GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(password); int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); char[] decoded_char = new char[charCount]; utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); string decrypt_transaction_password = new String(decoded_char); if (decrypt_transaction_password == transaction_password) { return(Ok("Passwords Match")); } else { return(Ok("Error")); } } catch (Exception e) { return(Ok("Error")); } }
public void SendMessage(string msg) { try { // New code to send strings NetworkStream networkStream = new NetworkStream(m_clientSocket); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); streamWriter.WriteLine(msg); streamWriter.Flush(); //------------------------------------------------------------------------------------------ byte[] data = new byte[500]; m_clientSocket.Receive(data); char[] chars = new char[data.Length + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(data, 0, data.Length, chars, 0); System.String szData = new System.String(chars); richTextRxMessage.Text = richTextRxMessage.Text + szData.ToString(); //------------------------------------------------------------------------------------------ /* Use the following code to send bytes * byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ()); * if(m_clientSocket != null){ * m_clientSocket.Send (byData); * } */ } catch (SocketException se) { MessageBox.Show(se.Message); } }
public void OnDataReceived(IAsyncResult asyn) { SocketPacket socketData = (SocketPacket)asyn.AsyncState; try { int rx = socketData.currentSocket.EndReceive(asyn); char[] chars = new char[rx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars( socketData.dataBuffer, 0, rx, chars, 0 ); string data = new String(chars); if (OnData != null) { OnData(this, new DataEventArgs(data, socketData)); } WaitForData(); } catch (ObjectDisposedException) { // System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { // MessageBox.Show (se.Message ); } }
public void OnReceive(IAsyncResult oAsyncResult) { try { SocketPacket oSocketID = (SocketPacket)oAsyncResult.AsyncState; //Stop Recieve int iRecieve = 0; iRecieve = oSocketID.oSocket.EndReceive(oAsyncResult); //Build the message char[] chars = new char[iRecieve + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int iCharLen = d.GetChars(oSocketID.bDataBuffer, 0, iRecieve, chars, 0); //string sData = new string(chars); Packet_Exchange.Packets.add_to_que(chars[0]); //Wait again WaitForData(oSocketWorker); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\n OnReceive: Socket has been closed\n"); } catch (SocketException ex) { System.Diagnostics.Debugger.Log(0, "1", ex.Message); } }
public string ReadAnsiString() { const int SZ_BUFF = 0x100; byte * prem = _base; byte * pbuff = stackalloc byte[SZ_BUFF + 1]; char * pchar = stackalloc char[SZ_BUFF]; System.Text.StringBuilder build = new System.Text.StringBuilder(); pbuff[SZ_BUFF] = 0; // sentinel lock (dec){ dec.Reset(); int cByts, cChrs; do { // prem → pbuff mem.ReadMemory(prem, pbuff, SZ_BUFF); prem += SZ_BUFF; byte *scn = pbuff; while (*scn != 0) { scn++; // null 文字の位置迄 } cByts = (int)(scn - pbuff); // pbuff → pchar cChrs = dec.GetChars(pbuff, cByts, pchar, SZ_BUFF, false); // pchar → build build.Append(new string(pchar, 0, cChrs)); }while(cByts == SZ_BUFF); } return(build.ToString()); }
// This the call back function which will be invoked when the socket // detects any client writing of data on the stream public void OnDataReceived(IAsyncResult asyn) { try { SocketPacket socketData = (SocketPacket)asyn.AsyncState; int iRx = 0; // Complete the BeginReceive() asynchronous call by EndReceive() method // which will return the number of characters written to the stream // by the client iRx = socketData.m_currentSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); richTextBoxReceivedMsg.AppendText(szData); // Continue the waiting for data on the Socket WaitForData(socketData.m_currentSocket); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { MessageBox.Show(se.Message); } }
// This the call back function which will be invoked when the socket // detects any client writing of data on the stream public void OnDataReceived(IAsyncResult asyn) { SocketPacket socketData = (SocketPacket)asyn.AsyncState; try { // Complete the BeginReceive() asynchronous call by EndReceive() method // which will return the number of characters written to the stream // by the client int iRx = socketData.m_currentSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; // Extract the characters as a buffer System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); UpdateLogControl("Client " + socketData.m_clientNumber + ": " + szData); //Send back a reply to the client //string replyMsg = ""; //UpdateLogControl("Server Reply: " + replyMsg); //Convert the reply to byte array //byte[] byData = System.Text.Encoding.ASCII.GetBytes(replyMsg); //Socket workerSocket = (Socket)socketData.m_currentSocket; //workerSocket.Send(byData); WaitForData(socketData.m_currentSocket, socketData.m_clientNumber); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { CheckDisconnect(se, socketData.m_clientNumber); } }
public void OnDataReceived(IAsyncResult asyn) { int ClientID = tempint; try { SocketPacket socketData = (SocketPacket)asyn.AsyncState; LenghtOfDataRecieved = socketData.RealmserverSocket.EndReceive(asyn); char[] chars = new char[LenghtOfDataRecieved + 1]; System.Text.Decoder decode = System.Text.Encoding.UTF8.GetDecoder(); int charLen = decode.GetChars(socketData.dataBuffer, 0, LenghtOfDataRecieved, chars, 0); System.String szData = new System.String(chars); if (LenghtOfDataRecieved != 0) { ProcessRecieved(socketData.dataBuffer, ClientID); } Thread.Sleep(50); WaitForData(socketData.RealmserverSocket, ClientID); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { ColoredConsole.ConsoleWriteErrorWithOut(se.Message); } }
private int IntReadChars(char[] buffer, int index, int count, System.Text.Encoding encode, System.Text.Decoder decoder) { int cpS = 1; if (encode is System.Text.UnicodeEncoding) { cpS = 2; } else if (encode is System.Text.UTF32Encoding) { cpS = 4; } int rem = count; while (rem > 0) { int read = System.Math.Min(rem * cpS, _bufferLength); read = this._stream.Read(this._buffer, 0, read); if (read == 0) { return(count - rem); } read = decoder.GetChars(this._buffer, 0, read, buffer, index); rem -= read; index += read; } return(count); }
public string base64Decode(object obj) { try { string data = obj != null?obj.ToString() : string.Empty; if (string.IsNullOrEmpty(data)) { return(string.Empty); } System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); System.Text.Decoder utf8Decode = encoder.GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(data); int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); char[] decoded_char = new char[charCount]; utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); string result = new String(decoded_char); return(result); } catch (Exception e) { throw new Exception("Error in base64Decode" + e.Message); } }
//--------------------------------------- public string slice(int pos, int len) { System.Text.Decoder dec = System.Text.Encoding.Default.GetDecoder(); char[] text = new char[len]; dec.GetChars(theBuff, pos, len, text, 0); return(new string(text)); }
public void OnDataReceived(IAsyncResult asyn) { try { CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState; //end receive... int iRx = 0; iRx = theSockId.thisSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); //txtDataRx.Text = txtDataRx.Text + szData; txtDataRx.Invoke(new MethodInvoker(delegate { txtDataRx.Text = txtDataRx.Text + szData; })); WaitForData(m_socWorker); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { MessageBox.Show(se.Message); } }
public void OnDataReceived(IAsyncResult asyn) { try { CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState; //end receive... int iRx = 0; iRx = theSockId.thisSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); txtDataRx.Text = szData; if (txtDataRx.Text == "r") { pictureBox1.Left += 12; } if (txtDataRx.Text == "l") { pictureBox1.Left -= 12; } WaitForData(); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { MessageBox.Show(se.Message); } }
public void OnDataReceived(IAsyncResult asyn) { try { SocketPacket theSockId = (SocketPacket)asyn.AsyncState; int iRx = theSockId.thisSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0); System.String szData = theSockId.string_RemoteEndPoint + ":" + (new System.String(chars)); textBox_MSG.Invoke(new UpdateText(updateText), szData); WaitForData(); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (System.Exception se) { //MessageBox.Show("服务器断开连接,请检查服务器然后重新连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop); this.textBox_MSG.AppendText(se.Message + "\r\n"); button2.PerformClick(); } }
private string ReceiveData(int BytesReceive, int BufferSize, Socket SocketData) { string Message = string.Empty; if ((BufferSize - BytesReceive) != 0) { while (BytesReceive < BufferSize) { byte[] Buffer = new byte[(BufferSize - BytesReceive)]; int ByteRest = SocketData.Receive(Buffer, (BufferSize - BytesReceive), SocketFlags.None ); char[] charLenght = new char[ByteRest]; System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder(); decoder.GetChars( Buffer, 0, ByteRest, charLenght, 0 ); BytesReceive += (BufferSize - BytesReceive); Message += new string(charLenght); } } return(Message); }
void OnDataReceived(IAsyncResult asyn) { try { SocketPacket packet = (SocketPacket)asyn.AsyncState; int end = packet.TCPSocket.EndReceive(asyn); char[] chars = new char[end + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); d.GetChars(packet.DataBuffer, 0, end, chars, 0); System.String data = new System.String(chars); ReceiveData(data); WaitForData(); } catch (ObjectDisposedException) { Console.WriteLine("WARNING: Socket closed unexpectedly"); } catch (SocketException se) { if (!_TCPSocket.Connected) { if (OnDisconnected != null) { OnDisconnected(se); } } } }
/// <summary> /// Decodes any byte array into a string /// </summary> /// <param name="byteArray">raw byte array to be decoded</param> /// <param name="encoding">byte array encoding type</param> /// <returns>decoded string</returns> public string DecodeBytesToString(byte[] byteArray) { char[] chars = new char[byteArray.Length + 1]; System.Text.Decoder d = this.encoding.GetDecoder(); int charLen = d.GetChars(byteArray, 0, byteArray.Length, chars, 0); return(new System.String(chars)); }
public static string GetNameFromByte(byte[] _name, int _iRx) { System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); char[] chars = new char[_iRx]; d.GetChars(_name, 0, _iRx, chars, 0); return(new string(chars)); }
public void ExportPurchaseInvoice(string encrift) { List <Purchase> purchase = new List <Purchase>(); byte[] b = Convert.FromBase64String(encrift); System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); System.Text.Decoder utf8Decoder = encoder.GetDecoder(); int charCount = utf8Decoder.GetCharCount(b, 0, b.Length); char[] decodedChar = new char[charCount]; utf8Decoder.GetChars(b, 0, b.Length, decodedChar, 0); string result = new string(decodedChar); JavaScriptSerializer js = new JavaScriptSerializer(); Purchase[] PurchaseList = js.Deserialize <Purchase[]>(result); foreach (var a in PurchaseList) { Purchase aPurchase = new Purchase(); aPurchase.PurchaseNo = a.PurchaseNo; aPurchase.PurchaseDate = a.PurchaseDate; aPurchase.PurchaseSupplierInvoiceNo = a.PurchaseSupplierInvoiceNo; aPurchase.ProductCode = db.productDetails.First(p => p.ProductDetailsID == a.PurchaseProductID).Code; aPurchase.ProductName = db.productDetails.First(p => p.ProductDetailsID == a.PurchaseProductID).ProductName; aPurchase.PurchaseProductPrice = a.PurchaseProductPrice; aPurchase.PurchaseQuantity = a.PurchaseQuantity; aPurchase.PurchaseTotal = a.PurchaseTotal; aPurchase.TotalAmount = a.TotalAmount; purchase.Add(aPurchase); } ReportDataSource reportDataSource = new ReportDataSource(); reportDataSource.Name = "PurchaseDataSet"; reportDataSource.Value = purchase; string mimeType = string.Empty; string encodeing = string.Empty; string fileNameExtension = "pdf"; Warning[] warnings; string[] streams; LocalReport localReport = new LocalReport(); localReport.ReportPath = Server.MapPath("~/Report/Purchase/PurchaseReport.rdlc"); localReport.DataSources.Add(reportDataSource); byte[] bytes = localReport.Render("PDF", null, out mimeType, out encodeing, out fileNameExtension, out streams, out warnings); Response.Buffer = true; Response.Clear(); Response.ContentType = mimeType; Response.AddHeader("content-disposition", "attachment;filename=file." + fileNameExtension); Response.BinaryWrite(bytes); Response.Flush(); }
private string ConvertBytesToString(byte[] bytes, int iRx) { char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); d.GetChars(bytes, 0, iRx, chars, 0); string szData = new string(chars); return(szData); }
public void SavePaneSettings(string outputFilePath) { byte[] currentRegPaneSettings = (byte[])Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\HolodeckEE").GetValue("PaneSettings"); System.IO.StreamWriter outputFile = new System.IO.StreamWriter(outputFilePath); System.Text.Decoder decoder = System.Text.Encoding.Unicode.GetDecoder(); char[] decodedPaneSettings = new char[currentRegPaneSettings.Length]; decoder.GetChars(currentRegPaneSettings, 0, currentRegPaneSettings.Length, decodedPaneSettings, 0); outputFile.Write(decodedPaneSettings); outputFile.Close(); }
/// <summary> /// Encodes the given bytes as <see cref="char"/>'s using the specified options using <see cref="System.Text.Encoding.GetChars"/>. /// </summary> /// <param name="encoding">The optional encoding to use, if none is specified the Default will be used.</param> /// <param name="toEncode">The data to encode, if null an <see cref="ArgumentNullException"/> will be thrown.</param> /// <param name="offset">The offset to start at</param> /// <param name="count">The amount of bytes to use in the encoding</param> /// <returns>The encoded data</returns> public static char[] GetChars(this System.Text.Decoder decoder, byte[] toEncode, int offset, int count) { //Use default.. if (decoder == null) { decoder = System.Text.Encoding.Default.GetDecoder(); } return(decoder.GetChars(toEncode, offset, count)); }
public string getFileName() { char [] charBuf = new char[6]; System.Text.ASCIIEncoding ae = new System.Text.ASCIIEncoding(); System.Text.Decoder d = ae.GetDecoder(); d.GetChars(m_FileName, 0, 6, charBuf, 0); return(new string( charBuf )); }
public void OnDataReceived(IAsyncResult asyn) { try { Communications.SocketPacket socketData = (Communications.SocketPacket)asyn.AsyncState; int iRx = 0; // Complete the BeginReceive() asynchronous call by EndReceive() method // which will return the number of characters written to the stream // by the client iRx = socketData.m_currentSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); EndPoint remoteEndPoint = socketData.m_currentSocket.RemoteEndPoint; if (LastEndPointStarted == null) { LastEndPointStarted = remoteEndPoint; } if (LastEndPointStarted == remoteEndPoint) { // My attempts to defeat concurrent thread write collisions. I was seeing 2 end points trying to write // into the socket and that concatenated the bits at the bit level, so no byte received was even remotely // correct. I put in this equality test to force the last end point we started working with to be the // winner. The loser's message is ignored. ReceivedMsg.AppendText(szData); //when we are sure we received the entire message //pass the Object received into parameter //after removing the flag indicating the end of message String EOMDelimiter = m_Globals.m_communications.EOMDelimiter; if (ReceivedMsg.GetText().Contains(EOMDelimiter)) { LastEndPointStarted = null; String SanitizedMessage = Regex.Replace(ReceivedMsg.GetText(), EOMDelimiter, ""); ProcessCommMessage(SanitizedMessage, socketData.m_currentSocket.RemoteEndPoint); } } // Continue the waiting for data on the Socket WaitForData(socketData.m_currentSocket); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { MessageBox.Show(se.Message); } }
public static string BytesToString(byte[] bytes) { System.Text.Decoder decode = System.Text.Encoding.UTF8.GetDecoder(); long length = decode.GetCharCount(bytes, 0, bytes.Length); char[] chars = new char[length]; decode.GetChars(bytes, 0, bytes.Length, chars, 0); string result = new String(chars); return(result); }
public static string Decrypt(this string str) { System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); System.Text.Decoder utf8Decode = encoder.GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(str); int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); char[] decoded_char = new char[charCount]; utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); return(new string(decoded_char)); }
protected string GetDataAsString(byte[] data) { System.Text.Decoder utf8Decoder = System.Text.Encoding.UTF8.GetDecoder(); int charCount = utf8Decoder.GetCharCount(data, 0, (data.Length)); char[] recievedChars = new char[charCount]; utf8Decoder.GetChars(data, 0, data.Length, recievedChars, 0); string recievedString = new String(recievedChars); return(recievedString); }
public string getCharacterComplement() { char [] charBuf = new char[8]; System.Text.ASCIIEncoding ae = new System.Text.ASCIIEncoding(); System.Text.Decoder d = ae.GetDecoder(); d.GetChars(m_CharacterComplement, 0, 8, charBuf, 0); return(new string( charBuf )); }
public string GetTypeface() { char[] charBuf = new char[16]; System.Text.ASCIIEncoding ae = new System.Text.ASCIIEncoding(); System.Text.Decoder d = ae.GetDecoder(); d.GetChars(Typeface, 0, 16, charBuf, 0); return(new string(charBuf)); }