当前位置: 首页>>代码示例>>C#>>正文


C# System.Text.UTF8Encoding.GetByteCount方法代码示例

本文整理汇总了C#中System.Text.UTF8Encoding.GetByteCount方法的典型用法代码示例。如果您正苦于以下问题:C# System.Text.UTF8Encoding.GetByteCount方法的具体用法?C# System.Text.UTF8Encoding.GetByteCount怎么用?C# System.Text.UTF8Encoding.GetByteCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.UTF8Encoding的用法示例。


在下文中一共展示了System.Text.UTF8Encoding.GetByteCount方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CheckforAndHandleBOM

		/// <summary>
		/// This will look at the start of the file for the BOM and process it
		/// if it's found.
		/// </summary>
		private void CheckforAndHandleBOM()
		{
			byte[] utf32be = new byte[] { 0x00, 0x00, 0xfe, 0xff };
			byte[] utf32le = new byte[] { 0xff, 0xfe, 0x00, 0x00 };
			byte[] utf16be = new byte[] { 0xfe, 0xff };
			byte[] utf16le = new byte[] { 0xff, 0xfe };
			byte[] utf8    = new byte[] { 0xef, 0xbb, 0xbf };

			if (IsCurrentData(utf32le) || IsCurrentData(utf32be))
			{
				m_foundBOM = true;
				m_BOMLength = utf32be.Length;
				//m_BOMEncoding = System.Text.Encoding.???;	// not currently defined for 32
				m_position = m_BOMLength;
			}
			else if (IsCurrentData(utf8))
			{
				m_foundBOM = true;
				m_BOMLength = utf8.Length;
				m_BOMEncoding = System.Text.Encoding.UTF8;
				m_position = m_BOMLength;
			}
			else if (IsCurrentData(utf16le))
			{
				m_foundBOM = true;
				m_BOMLength = utf16le.Length;
				m_BOMEncoding = System.Text.Encoding.Unicode;
				m_position = m_BOMLength;
			}
			else if (IsCurrentData(utf16be))
			{
				m_foundBOM = true;
				m_BOMLength = utf16be.Length;
				m_BOMEncoding = System.Text.Encoding.BigEndianUnicode;
				m_position = m_BOMLength;
			}

			if (m_foundBOM)	// has one
			{
				if (m_BOMEncoding == System.Text.Encoding.UTF8)
				{
					// no extra processing needed for UTF8 - this is the default format
				}
				else if (m_BOMEncoding == System.Text.Encoding.Unicode ||
					m_BOMEncoding == System.Text.Encoding.BigEndianUnicode)
				{
					System.Text.UTF8Encoding utf8Encoder = new System.Text.UTF8Encoding(false, true);
					try
					{
						// decode the wide Unicode byte data to wide chars
						System.Text.Decoder uniDecoder = m_BOMEncoding.GetDecoder();
						int charCount = uniDecoder.GetCharCount(m_FileData, 2, m_FileData.Length-2);
						char[] chars = new Char[charCount];
						int charsDecodedCount = uniDecoder.GetChars(m_FileData, 2, m_FileData.Length-2, chars, 0);

						// decode the wide chars to utf8 bytes
						int newLength = utf8Encoder.GetByteCount(chars);
						m_FileData = new byte[newLength];
						utf8Encoder.GetBytes(chars, 0, chars.Length, m_FileData, 0);

						// log msg for user to see
						if (m_Log != null)
							m_Log.AddWarning(String.Format(Sfm2XmlStrings.FileConvertedFrom0To1,
								m_BOMEncoding.WebName, utf8Encoder.WebName));
					}
					catch (System.Exception e)
					{
						if (m_Log != null)
						{
							m_Log.AddFatalError(String.Format(Sfm2XmlStrings.CannotConvertFileFrom0To1,
								m_BOMEncoding.WebName, utf8Encoder.WebName));
							m_Log.AddFatalError(String.Format(Sfm2XmlStrings.Exception0, e.Message));
						}
						m_position = 0;
						m_FileData = new byte[0];	// don't process anything
					}
				}
				else
				{
					m_position = 0;
					m_FileData = new byte[0];	// don't process anything
					if (m_Log != null)
						m_Log.AddFatalError(Sfm2XmlStrings.CannotProcessUtf32Files);
				}
			}
		}
开发者ID:sillsdev,项目名称:WorldPad,代码行数:90,代码来源:FileReader.cs


注:本文中的System.Text.UTF8Encoding.GetByteCount方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。