Example #1
0
        public void PrintCharInfo()
        {
            System.Console.WriteLine("Max Value: {0}", System.Char.MaxValue);
            System.Console.WriteLine("Min Value: {0}", System.Char.MinValue);
            System.Globalization.UnicodeCategory category = System.Char.GetUnicodeCategory("e", 0);
            System.Console.WriteLine("Unicode category: {0}", System.Char.GetUnicodeCategory("4", 0));
            if (System.Char.IsDigit("5", 0))
            {
                System.Console.WriteLine("Digit!");
            }

            Char ch = '1';

            System.Console.WriteLine("char type: {0}", ch);

            //3 ways to convert int to char
            int aCode = 65;

            Char charValue = (Char)aCode; // A - the best way

            System.Console.WriteLine("{0}", charValue);

            Char charValue2 = Convert.ToChar(aCode); //middle way so-so

            System.Console.WriteLine("{0}", charValue2);

            Char charValue3 = ((IConvertible)aCode).ToChar(null);  // very bad way, because boxing

            System.Console.WriteLine("{0}", charValue3);
        }
Example #2
0
        public static string RemoveDiacritics(this string text)
        {
            string        stFormD = text.Normalize(NormalizationForm.FormD).ToString().ToLowerInvariant();
            StringBuilder sb      = new StringBuilder();

            for (int ich = 0; ich < stFormD.Length; ich++)
            {
                System.Globalization.UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
                if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(RemapInternationalCharToAscii(stFormD[ich]));
                }
            }
            return(sb.ToString().Normalize(NormalizationForm.FormD));
        }
Example #3
0
        public static string RemoveDiacritics(string input)
        {
            string        stFormD = input.Normalize(NormalizationForm.FormD);
            int           len     = stFormD.Length;
            StringBuilder sb      = new StringBuilder();

            for (int i = 0; i < len; i++)
            {
                System.Globalization.UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stFormD[i]);
                if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(stFormD[i]);
                }
            }
            return(sb.ToString().Normalize(NormalizationForm.FormC));
        }
Example #4
0
    public static string Method1(string s)
    {
        string        stFormD = s.Normalize(NormalizationForm.FormD);
        StringBuilder sb      = new StringBuilder();

        for (int ich = 0; ich < stFormD.Length; ich++)
        {
            System.Globalization.UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
            if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
            {
                sb.Append(stFormD[ich]);
            }
        }
        sb = sb.Replace('Đ', 'D');
        sb = sb.Replace('đ', 'd');
        return(sb.ToString().Normalize(NormalizationForm.FormD));
    }
Example #5
0
        public static string ConvertToUnsign(string str)
        {
            string        strFormD = str.Normalize(NormalizationForm.FormD);
            StringBuilder sb       = new StringBuilder();

            for (int i = 0; i < strFormD.Length; i++)
            {
                System.Globalization.UnicodeCategory uc =
                    System.Globalization.CharUnicodeInfo.GetUnicodeCategory(strFormD[i]);
                if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(strFormD[i]);
                }
            }
            sb = sb.Replace('Đ', 'D');
            sb = sb.Replace('đ', 'd');
            return((sb.ToString().Normalize(NormalizationForm.FormD)).Replace(" ", "-"));
        }
Example #6
0
        /// <summary>
        /// Gets a user-friendly representation of the character
        /// </summary>
        /// <param name="c">A character</param>
        /// <returns>The string representation</returns>
        private static string CharToString(char c)
        {
            System.Globalization.UnicodeCategory cat = char.GetUnicodeCategory(c);
            switch (cat)
            {
            case System.Globalization.UnicodeCategory.ModifierLetter:
            case System.Globalization.UnicodeCategory.NonSpacingMark:
            case System.Globalization.UnicodeCategory.SpacingCombiningMark:
            case System.Globalization.UnicodeCategory.EnclosingMark:
            case System.Globalization.UnicodeCategory.SpaceSeparator:
            case System.Globalization.UnicodeCategory.LineSeparator:
            case System.Globalization.UnicodeCategory.ParagraphSeparator:
            case System.Globalization.UnicodeCategory.Control:
            case System.Globalization.UnicodeCategory.Format:
            case System.Globalization.UnicodeCategory.Surrogate:
            case System.Globalization.UnicodeCategory.PrivateUse:
            case System.Globalization.UnicodeCategory.OtherNotAssigned:
                return(CharToString_NonPrintable(c));

            default:
                return(c.ToString());
            }
        }
        private void WriteObject(string name, ProtectedString value, bool bIsEntryString)
        {
            Debug.Assert(name != null);
            Debug.Assert(value != null); if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            m_xmlWriter.WriteStartElement(ElemString);
            m_xmlWriter.WriteStartElement(ElemKey);
            m_xmlWriter.WriteString(StrUtil.SafeXmlString(name));
            m_xmlWriter.WriteEndElement();
            m_xmlWriter.WriteStartElement(ElemValue);

            bool bProtected = value.IsProtected;

            if (bIsEntryString)
            {
                // Adjust memory protection setting (which might be different
                // from the database default, e.g. due to an import which
                // didn't specify the correct setting)
                if (name == PwDefs.TitleField)
                {
                    bProtected = m_pwDatabase.MemoryProtection.ProtectTitle;
                }
                else if (name == PwDefs.UserNameField)
                {
                    bProtected = m_pwDatabase.MemoryProtection.ProtectUserName;
                }
                else if (name == PwDefs.PasswordField)
                {
                    bProtected = m_pwDatabase.MemoryProtection.ProtectPassword;
                }
                else if (name == PwDefs.UrlField)
                {
                    bProtected = m_pwDatabase.MemoryProtection.ProtectUrl;
                }
                else if (name == PwDefs.NotesField)
                {
                    bProtected = m_pwDatabase.MemoryProtection.ProtectNotes;
                }
            }

            if (bProtected && (m_format == KdbxFormat.Default))
            {
                m_xmlWriter.WriteAttributeString(AttrProtected, ValTrue);

                byte[] pbEncoded = value.ReadXorredString(m_randomStream);
                if (pbEncoded.Length > 0)
                {
                    m_xmlWriter.WriteBase64(pbEncoded, 0, pbEncoded.Length);
                }
            }
            else
            {
                string strValue = value.ReadString();

                // If names should be localized, we need to apply the language-dependent
                // string transformation here. By default, language-dependent conversions
                // should be applied, otherwise characters could be rendered incorrectly
                // (code page problems).
                if (m_bLocalizedNames)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (char ch in strValue)
                    {
                        char chMapped = ch;

                        // Symbols and surrogates must be moved into the correct code
                        // page area
                        if (char.IsSymbol(ch) || char.IsSurrogate(ch))
                        {
                            System.Globalization.UnicodeCategory cat =
                                CharUnicodeInfo.GetUnicodeCategory(ch);
                            // Map character to correct position in code page
                            chMapped = (char)((int)cat * 32 + ch);
                        }
                        else if (char.IsControl(ch))
                        {
                            if (ch >= 256)                            // Control character in high ANSI code page
                            {
                                // Some of the control characters map to corresponding ones
                                // in the low ANSI range (up to 255) when calling
                                // ToLower on them with invariant culture (see
                                // http://lists.ximian.com/pipermail/mono-patches/2002-February/086106.html )
#if !KeePassLibSD
                                chMapped = char.ToLowerInvariant(ch);
#else
                                chMapped = char.ToLower(ch);
#endif
                            }
                        }

                        sb.Append(chMapped);
                    }

                    strValue = sb.ToString();                     // Correct string for current code page
                }

                if ((m_format == KdbxFormat.PlainXml) && bProtected)
                {
                    m_xmlWriter.WriteAttributeString(AttrProtectedInMemPlainXml, ValTrue);
                }

                m_xmlWriter.WriteString(StrUtil.SafeXmlString(strValue));
            }

            m_xmlWriter.WriteEndElement();             // ElemValue
            m_xmlWriter.WriteEndElement();             // ElemString
        }
Example #8
0
 public override bool IsRead(int codePoint)
 {
     System.Globalization.UnicodeCategory ci = char.GetUnicodeCategory((char)codePoint);
     return(ci == charType);
 }