Ejemplo n.º 1
0
 static string GetSHA1Hash(string val)
 {
     System.Text.Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
     byte[] data             = System.Text.Encoding.UTF8.GetBytes(val);
     System.Security.Cryptography.SHA1 sha = new System.Security.Cryptography.SHA1Managed();
     byte[] res = sha.ComputeHash(data);
     return(System.BitConverter.ToString(res).Replace("-", "").ToUpper());
 }
Ejemplo n.º 2
0
        public static byte[] StringToBytes(string thestring)
        {
            System.Text.Encoder encode = System.Text.Encoding.UTF8.GetEncoder();
            char[] chars  = thestring.ToCharArray();
            long   length = encode.GetByteCount(chars, 0, chars.Length, true);

            byte[] bytes = new byte[length];
            encode.GetBytes(chars, 0, chars.Length, bytes, 0, true);
            return(bytes);
        }
Ejemplo n.º 3
0
        public void LoadPaneSettings(string inputFilePath)
        {
            System.IO.StreamReader inputFile = new System.IO.StreamReader(inputFilePath);
            string fileContent = inputFile.ReadToEnd();

            char[] newRegPaneSettings   = fileContent.ToCharArray();
            System.Text.Encoder encoder = System.Text.Encoding.Unicode.GetEncoder();
            byte[] encodedPaneSettings  = new byte[newRegPaneSettings.Length * 2];
            encoder.GetBytes(newRegPaneSettings, 0, newRegPaneSettings.Length, encodedPaneSettings, 0, true);
            Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\HolodeckEE", true).SetValue("PaneSettings", encodedPaneSettings);
        }
Ejemplo n.º 4
0
 public static string GetMd5Sum(string str)
 {
     System.Text.Encoder encoder = System.Text.Encoding.Unicode.GetEncoder();
     byte[] bytes = new byte[str.Length * 2];
     encoder.GetBytes(str.ToCharArray(), 0, str.Length, bytes, 0, true);
     byte[] buffer2 = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(bytes);
     System.Text.StringBuilder builder = new System.Text.StringBuilder();
     for (int i = 0; i < buffer2.Length; i++)
     {
         builder.Append(buffer2[i].ToString("X2"));
     }
     return(builder.ToString());
 }
Ejemplo n.º 5
0
        public static byte[] EncodeMessage(string content)
        {
            byte[] cont = new byte[content.Length];

            System.Text.ASCIIEncoding coidn = new System.Text.ASCIIEncoding();
            System.Text.Encoder       enc   = coidn.GetEncoder();
            int  charsconv = 0;
            int  bytesconv = 0;
            bool conv      = false;

            enc.Convert(content.ToCharArray(), 0, content.Length, cont, 0, cont.Length, true, out charsconv, out bytesconv, out conv);

            return(cont);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Converte una stringa (UTF8) nel rispettivo array di byte.
 /// </summary>
 /// <param name="dataString">Stringa da convertire</param>
 /// <returns>Array di byte corrispondente alla stringa passata in input</returns>
 public static byte[] GetBytesFromString(string dataString)
 {
     byte[] data;
     if (Device.RuntimePlatform == Device.UWP)
     {
         data = System.Text.Encoding.UTF8.GetBytes(dataString);
     }
     else
     {
         System.Text.Encoder enc = System.Text.Encoding.UTF8.GetEncoder();
         char[] chars            = dataString.ToCharArray();
         data = new byte[enc.GetByteCount(chars, chars.GetLowerBound(0), chars.Length, false)];
         enc.GetBytes(chars, chars.GetLowerBound(0), chars.Length, data, 0, false);
     }
     return(data);
 }
        private void grabHttpPacket(XmlElement packetElement)
        {
            string packetString = packetElement.InnerText;

            System.Text.UTF8Encoding utf8    = new System.Text.UTF8Encoding();
            System.Text.Encoder      utf8Enc = utf8.GetEncoder();
            char [] packetChars = packetString.ToCharArray();
            int     len         = packetChars.Length;

            byte [] packetBytes = new byte[len];
            utf8Enc.GetBytes(packetChars, 0, len, packetBytes, 0, true);

            HTTPMessage m = HTTPMessage.ParseByteArray(packetBytes);

            packets.Add(m);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// URL编码
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        private static string UrlEncode(string str, string encode)
        {
            int factor = 0;

            if (encode == "UTF-8")
            {
                factor = 3;
            }
            if (encode == "GB2312")
            {
                factor = 2;
            }
            //不需要编码的字符

            string okChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.=";

            System.Text.Encoder encoder = System.Text.Encoding.GetEncoding(encode).GetEncoder();
            char[] c1 = str.ToCharArray();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //一个字符一个字符的编码

            for (int i = 0; i < c1.Length; i++)
            {
                //不需要编码

                if (okChar.IndexOf(c1[i]) > -1)
                {
                    sb.Append(c1[i]);
                }
                else
                {
                    byte[] c2 = new byte[factor];
                    int    charUsed, byteUsed; bool completed;

                    encoder.Convert(c1, i, 1, c2, 0, factor, true, out charUsed, out byteUsed, out completed);

                    foreach (byte b in c2)
                    {
                        if (b != 0)
                        {
                            sb.AppendFormat("%{0:X}", b);
                        }
                    }
                }
            }
            return(sb.ToString().Trim());
        }
Ejemplo n.º 9
0
            public void SendMessage(ServerMessage message)
            {
                // serialize message to JSON
                char[] data = JsonConvert.SerializeObject(message).ToCharArray();

                // encode string into UTF-8
                System.Text.Encoder encoder = System.Text.Encoding.UTF8.GetEncoder();
                int bufferSize = encoder.GetByteCount(data, 0, data.Length, true);

                char[] separator = XRefresh.Server.SEPARATOR.ToCharArray();
                byte[] buffer    = new byte[bufferSize + separator.Length];
                encoder.GetBytes(data, 0, data.Length, buffer, 0, true);
                for (int i = 0; i < separator.Length; i++)
                {
                    buffer[bufferSize + i] = (byte)separator[i];
                }

                // send as UTF-8 string
                socket.Send(buffer);
            }
Ejemplo n.º 10
0
        static int _m_GetEncoder(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.Text.Encoding gen_to_be_invoked = (System.Text.Encoding)translator.FastGetCSObj(L, 1);



                {
                    System.Text.Encoder gen_ret = gen_to_be_invoked.GetEncoder(  );
                    translator.Push(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Ejemplo n.º 11
0
        public virtual string PrefixForByteCount(string s, int maxbytecount)
        {
            if (s.Length < 1)
            {
                return("");
            }
            int prefixcharcount = maxbytecount;

            if (prefixcharcount > s.Length)
            {
                prefixcharcount = s.Length;
            }
            System.Text.Encoder encode = System.Text.Encoding.UTF8.GetEncoder();
            char[] chars  = s.ToCharArray(0, prefixcharcount);
            long   length = encode.GetByteCount(chars, 0, prefixcharcount, true);

            while (length > maxbytecount)
            {
                prefixcharcount--;
                length = encode.GetByteCount(chars, 0, prefixcharcount, true);
            }
            return(s.Substring(0, prefixcharcount));
        }
Ejemplo n.º 12
0
 public Encoder(StringStorageEncoding enc)
 {
     mEncoding = enc; mEnc = enc.mBaseEncoding.GetEncoder();
 }
Ejemplo n.º 13
0
        //--------------------------------------------------------
        //--------------------------------------------------------
        public static void PrintTextWin32(TextBox textIn)
        {
            IntPtr hPort;

            string strPort;

            // strPort = @"\\Server\HP5si_PCL";
            // strPort = @"COM1:";
            strPort = @"\My Documents\TextPrint.dat";

            hPort = WinIO.CreateFile(strPort,
                                     WinIO.ACCESS.WRITE, WinIO.FILE_SHARE.WRITE, 0,
                                     WinIO.FILE_ACTION.OPEN_ALWAYS, WinIO.FILE_ATTRIBUTE.NORMAL, 0);

            // ?? Need to call SetCommState to set baud rate, etc. ??
            if (hPort == WinIO.INVALID_FILE_HANDLE)
            {
                MessageBox.Show("Error opening port", "PrintDirect");
            }
            else
            {
                // Split input data into separate lines of text.
                char []   achNewLine = new char[] { '\n' };
                String [] astrSplit;
                astrSplit = textIn.Text.Split(achNewLine);

                int i;
                int cstr = astrSplit.Length;

                // Check for largest string in document.
                int cchMax = 0;
                for (i = 0; i < cstr; i++)
                {
                    if (astrSplit[i].Length > cchMax)
                    {
                        cchMax = astrSplit[i].Length;
                    }
                }
                cchMax = cchMax + 5; // Add some padding.

                // Allocate conversion buffers.
                byte[] byteData       = new Byte[cchMax];
                char[] chData         = new Char[cchMax];
                int    cbWritten      = 0;
                System.Text.Encoder d = System.Text.Encoding.UTF8.GetEncoder();

                for (i = 0; i < cstr; i++)
                {
                    int cch = astrSplit[i].Length;
                    if (cch > 0)
                    {
                        //MessageBox.Show(i.ToString() + " " + astrSplit[i]);
                        chData = astrSplit[i].ToCharArray();
                        int charLen = d.GetBytes(chData, 0, cch, byteData, 0, true);
                        WinIO.WriteFile(hPort, byteData, charLen, ref cbWritten, IntPtr.Zero);
                    }

                    // Put a <CR><LF> at the end of the line.
                    byte[] byteCrLf = new byte[] { CR };
                    WinIO.WriteFile(hPort, byteCrLf, 2, ref cbWritten, IntPtr.Zero);
                }
                // Put a <FF> at the end of the document.
                byte[] byteFF = new byte[] { 0x0c };
                WinIO.WriteFile(hPort, byteFF, 1, ref cbWritten, IntPtr.Zero);
                WinIO.CloseHandle(hPort);
            }
        }