我在两个应用程序之间共享一个sql-server 2010数据库。我们控制的一个应用程序,另一个应用程序是首先创建数据库的第三方应用程序。我们的应用程序是基于第三方Webmail应用程序构建的CRM。
数据库包含varchar列,并且是latin-1编码的。第三方应用程序是用PHP编写的,并不关心正确编码数据,因此它将utf-8编码的字节填充到varchar列中,在那里它们被解释为latin-1并且看起来像垃圾。
我们的CRM应用程序是用.Net编写的,它自动检测数据库排序规则与内存中字符串的编码不同,因此当.Net写入数据库时,它会转换字节以匹配数据库编码。 / p>
所以...从我们的应用程序写入数据库的数据在数据库中看起来是正确的,但来自第三方应用程序的数据却没有。
当我们的应用程序写入FirstName =Céline时,它将作为Céline存储在数据库中
当webmail应用程序写入FirstName =Céline时,它将作为Cé行存储在数据库中
我们的CRM应用需要显示在任一系统中创建的联系人。所以我正在写一个EncodingSniffer类,它查找标记字符,表明它编码不好的字符串并转换它们。
目前我有:
private static string[] _flaggedChars = new string[] { "é" };
非常适合将Cé行显示为Céline,但我需要添加到列表中。
有没有人知道一种资源可以获得utf-8特殊字符可以解释为iso-8859-1的所有可能方式?
由于
澄清: 因为我在.Net工作。当从数据库加载到内存中时,该字符串将转换为Unicode UTF-16。因此,无论它是否在数据库中正确编码。它现在表示为UTF16字节。我需要能够分析UTF-16字节,并确定它们是否因为utf-8字节被塞入iso-8859-1数据库而被搞砸....明显是泥巴吗?
这是我到目前为止所拥有的。它已经清理了大多数错误编码的字符,但我仍然遇到É问题 例如:Éric通过webmail存储在数据库中,但是在检测到错误的编码并将其更改回来后,它显示为 ?ric 看着有2500个联系人的用户,其中有数百个有编码问题,É是唯一无法正确显示的...
public static Regex CreateRegex()
{
string specials = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö";
List<string> flags = new List<string>();
foreach (char c in specials)
{
string interpretedAsLatin1 = Encoding.GetEncoding("iso-8859-1").GetString(Encoding.UTF8.GetBytes(c.ToString())).Trim();//take the specials, treat them as utf-8, interpret them as latin-1
if (interpretedAsLatin1.Length > 0)//utf-8 chars made up of 2 bytes, interpreted as two single byte latin-1 chars.
flags.Add(interpretedAsLatin1);
}
string regex = string.Empty;
foreach (string s in flags)
{
if (regex.Length > 0)
regex += '|';
regex += s;
}
return new Regex("(" + regex + ")");
}
public static string CheckUTF(string data)
{
Match match = CreateRegex().Match(data);
if (match.Success)
return Encoding.UTF8.GetString(Encoding.GetEncoding("iso-8859-1").GetBytes(data));//from iso-8859-1 (latin-1) to utf-8
else
return data;
}
所以:É正在转换为195'Ã',8240'‰'
答案 0 :(得分:0)
您应该尝试将字节字符串解码为UTF-8,如果出现错误,请改为使用ISO-8859-1。
编码为ISO-8859-1的文本很少&#34;发生&#34;也是有效的UTF-8 ...除非它的ISO-8859-1实际上只包含ASCII,但在那种情况下,你根本就没有问题。所以这种方法相当稳健。
在实际语言中忽略哪些字符比其他字符更频繁地出现,这是一个天真的分析,假设每个字符以相同的频率出现。让我们试着找出有效的ISO-8859-1被误认为UTF-8的频率,从而导致mojibake。我还假设不会发生C1控制字符(U + 0080到U + 009F)。
对于字节字符串中的任何给定字节。如果字节接近字符串的末尾,那么您甚至更有可能检测到格式错误的UTF-8,因为已知某些字节序列不够长,无法生成有效的UTF-8。但假设字节不在字符串末尾附近:
在长字符串的每个字节处,检测到错误的UTF-8的概率为0.33 + 0.09 + 0.07 + 0.04 = 0.53。
因此,对于长字符串,ISO-8859-1将通过UTF-8解码器静默传递的概率非常小:每增加一个字符大约减半:
该分析当然假设随机的ISO-8859-1字符。在实践中,误检率不会那么好(主要是因为实际文本中的大多数字节实际上是ASCII),但它仍然会非常好。
答案 1 :(得分:0)
感谢@Michael完成99%以上的工作!
这是迈克尔脚本的PowerShell版本,适用于任何有帮助的人。这也是@ Qubei建议的Windows-1252
代码页/编码来解决É
问题;虽然允许您修改这些编码,以防您的数据通过不同的编码组合而变得腐败。
#based on c# in question: https://stackoverflow.com/questions/10484833/detecting-bad-utf-8-encoding-list-of-bad-characters-to-sniff
function Convert-CorruptCodePageString {
[CmdletBinding(DefaultParameterSetName = 'ByInputText')]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByInputText')]
[string]$InputText
,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByInputObject')]
[PSObject]$InputObject
,
[Parameter(Mandatory = $true, ParameterSetName = 'ByInputObject')]
[string]$Property
,
[Parameter()]
[System.Text.Encoding]$SourceEncoding = [System.Text.Encoding]::GetEncoding('Windows-1252')
,
[Parameter()]
[System.Text.Encoding]$DestinationEncoding = [system.Text.Encoding]::UTF8
,
[Parameter()]
[string]$DodgyChars = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö'
)
begin {
[string]$InvalidCharRegex = ($DodgyChars.ToCharArray() | %{
[byte[]]$dodgyCharBytes = $DestinationEncoding.GetBytes($_.ToString())
$SourceEncoding.GetString($dodgyCharBytes,0,$dodgyCharBytes.Length).Trim()
}) -join '|'
}
process {
if ($PSCmdlet.ParameterSetName -eq 'ByInputText') {
$InputObject = $null
} else {
$InputText = $InputObject."$Property"
}
[bool]$IsLikelyCorrupted = $InputText -match $InvalidCharRegex
if ($IsLikelyCorrupted) { #only bother to decrupt if we think it's corrupted
[byte[]]$bytes = $SourceEncoding.GetBytes($InputText)
[string]$outputText = $DestinationEncoding.GetString($bytes,0,$bytes.Length)
} else {
[string]$outputText = $InputText
}
[pscustomobject]@{
InputString = $InputText
OutputString = $outputText
InputObject = $InputObject
IsLikelyCorrupted = $IsLikelyCorrupted
}
}
}
<强>演示强>
#demo of using a simple string without the function (may cause corruption since this doesn't check if the characters being replaced are those likely to have been corrupted / thus is more likely to cause corruption in many strings).
$x = 'Strømmen'
$bytes = [System.Text.Encoding]::GetEncoding('Windows-1252').GetBytes($x)
[system.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
#demo using the function
$x | Convert-CorruptCodePageString
#demo of checking all records in a table for an issue / reporting those with issues
#amend SQL Query, MyDatabaseInstance, and MyDatabaseCatlogue to point to your DB / query the relevant table
Invoke-SQLQuery -Query 'Select [Description], [RecId] from [DimensionFinancialTag] where [Description] is not null and [Description] > ''''' -DbInstance $MyDatabaseInstance -DbCatalog $MyDatabaseCatalog |
Convert-CorruptCodePageString -Property 'Description' |
?{$_.IsLikelyCorrupted} |
ft @{N='RecordId';E={$_.InputObject.RecId}}, InputString, OutputString
我的演示中使用的附加功能
我不是Invoke-SqlCmd
cmdlet的粉丝,所以我自己滚动了。
function Invoke-SQLQuery {
[CmdletBinding(DefaultParameterSetName = 'ByQuery')]
param (
[Parameter(Mandatory = $true)]
[string]$DbInstance
,
[Parameter(Mandatory = $true)]
[string]$DbCatalog
,
[Parameter(Mandatory = $true, ParameterSetName = 'ByQuery')]
[string]$Query
,
[Parameter(Mandatory = $true, ParameterSetName = 'ByPath')]
[string]$Path
,
[Parameter(Mandatory = $false)]
[hashtable]$Params = @{}
,
[Parameter(Mandatory = $false)]
[int]$CommandTimeoutSeconds = 30 #this is the SQL default
,
[Parameter(Mandatory = $false)]
[System.Management.Automation.Credential()]
[System.Management.Automation.PSCredential]$Credential=[System.Management.Automation.PSCredential]::Empty
)
begin {
write-verbose "Call to 'Execute-SQLQuery'"
$connectionString = ("Server={0};Database={1}" -f $DbInstance,$DbCatalog)
if ($Credential -eq [System.Management.Automation.PSCredential]::Empty) {
$connectionString = ("{0};Integrated Security=True" -f $connectionString)
} else {
$connectionString = ("{0};User Id={1};Password={2}" -f $connectionString, $Credential.UserName, $Credential.GetNetworkCredential().Password)
$PSCmdlet.Name
}
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
}
process {
#create the command & assign the connection
$cmd = new-object -TypeName 'System.Data.SqlClient.SqlCommand'
$cmd.Connection = $connection
#load in our query
switch ($PSCmdlet.ParameterSetName) {
'ByQuery' {$cmd.CommandText = $Query; break;}
'ByPath' {$cmd.CommandText = Get-Content -Path $Path -Raw; break;}
default {throw "ParameterSet $($PSCmdlet.ParameterSetName) not recognised by Invoke-SQLQuery"}
}
#assign parameters as required
#NB: these don't need declare statements in our query; so a query of 'select @demo myDemo' would be sufficient for us to pass in a parameter with name @demo and have it used
#we can also pass in parameters that don't exist; they're simply ignored (sometimes useful if writing generic code that has optional params)
$Params.Keys | %{$cmd.Parameters.AddWithValue("@$_", $Params[$_]) | out-null}
$reader = $cmd.ExecuteReader()
while (-not ($reader.IsClosed)) {
$table = new-object 'System.Data.DataTable'
$table.Load($reader)
write-verbose "TableName: $($table.TableName)" #NB: table names aren't always available
$table | Select-Object -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors
}
}
end {
$connection.Close()
}
}