Browse Source

Add ConvertFromBytesWithoutBom to convert BOM encoded texts without BOM

pull/3421/head
Alper Ebicoglu 6 years ago
parent
commit
619d7665c4
  1. 37
      framework/src/Volo.Abp.Core/Volo/Abp/Text/StringHelper.cs

37
framework/src/Volo.Abp.Core/Volo/Abp/Text/StringHelper.cs

@ -0,0 +1,37 @@
using System.Text;
namespace Volo.Abp.Text
{
public class StringHelper
{
/// <summary>
/// Converts a byte[] to string without BOM (byte order mark).
/// </summary>
/// <param name="bytes">The byte[] to be converted to string</param>
/// <param name="encoding">The encoding to get string. Default is UTF8</param>
/// <returns></returns>
public static string ConvertFromBytesWithoutBom(byte[] bytes, Encoding encoding = null)
{
if (bytes == null)
{
return null;
}
if (encoding == null)
{
encoding = Encoding.UTF8;
}
var hasBom = bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF;
if (hasBom)
{
return encoding.GetString(bytes, 3, bytes.Length - 3);
}
else
{
return encoding.GetString(bytes);
}
}
}
}
Loading…
Cancel
Save