Fastdb 之中文字元截取錯誤的問題,fastdb截取
Fastdb C#版本中,如果定義欄位類型為 CLI.FieldType.cli_asciiz,使用的過程中插入中文字元集會出現亂碼的情況,
追查code發現是在對字串緩衝區CopyBufferData的過程中直接fastdb直接使用了s.length擷取了字元個數,而不是擷取位元組數,由於中文佔位兩個位元組,所以導致資料copy不全,從而出現亂碼。
不多說,修正代碼如下:
protected int bytelengh(string str)
{
//使用Unicode編碼的方式將字串轉換為位元組數組,它將所有字串(包括英文中文)全部以2個位元組儲存
byte[] bytestr = System.Text.Encoding.Unicode.GetBytes(str);
int j = 0;
for (int i = 0; i < bytestr.GetLength(0); i++)
{
//取餘2是因為位元組數組中所有的雙數下標的元素都是unicode字元的第一個位元組
if (i % 2 == 0)
{
j++;
}
else
{
//單數下標都是字元的第2個位元組,如果一個字元第2個位元組為0,則代表該Unicode字元是英文字元,否則為中文字元
if (bytestr[i] > 0)
{
j++;
}
}
}
return j;
}
protected unsafe void setValue(Object Value) {
switch((CLI.FieldType)((CLI.UnmanagedBuffer*)buffer.ToPointer())->type) {
case CLI.FieldType.cli_oid:
*(uint*)((CLI.UnmanagedBuffer*)buffer.ToPointer())->data.ToPointer() = Convert.ToUInt32(Value);
break;
case CLI.FieldType.cli_int4:
*(int*)((CLI.UnmanagedBuffer*)buffer.ToPointer())->data.ToPointer() = Convert.ToInt32(Value);
break;
case CLI.FieldType.cli_bool:
case CLI.FieldType.cli_int1:
*(sbyte*)((CLI.UnmanagedBuffer*)buffer.ToPointer())->data.ToPointer() = Convert.ToSByte(Value);
break;
case CLI.FieldType.cli_int2:
*(Int16*)((CLI.UnmanagedBuffer*)buffer.ToPointer())->data.ToPointer() = Convert.ToInt16(Value);
break;
case CLI.FieldType.cli_int8:
*(Int64*)((CLI.UnmanagedBuffer*)buffer.ToPointer())->data.ToPointer() = Convert.ToInt64(Value);
break;
case CLI.FieldType.cli_real4:
*(Single*)((CLI.UnmanagedBuffer*)buffer.ToPointer())->data.ToPointer() = Convert.ToSingle(Value);
break;
case CLI.FieldType.cli_datetime:
case CLI.FieldType.cli_real8:
*(double*)((CLI.UnmanagedBuffer*)buffer.ToPointer())->data.ToPointer() = Convert.ToDouble(Value);
break;
case CLI.FieldType.cli_asciiz:
case CLI.FieldType.cli_pasciiz:
string s = Value.ToString();
IntPtr str = Marshal.StringToHGlobalAnsi(s);
//糾正中文字元截取錯誤的問題
try {
CopyBufferData((CLI.FieldType)((CLI.UnmanagedBuffer*)buffer.ToPointer())->type, bytelengh(s), str);
}
finally {
Marshal.FreeCoTaskMem(str);
}
break;
case CLI.FieldType.cli_array_of_int1:
if (Value is byte[]) {
byte[] arr = (byte[])Value;
int len = arr.Length;
SetBufferTypeAndSize((CLI.UnmanagedBuffer*)buffer.ToPointer(), CLI.FieldType.cli_array_of_int1, len, false);
byte* dst = (byte*)((CLI.UnmanagedBuffer*)buffer.ToPointer())->data.ToPointer();
for (int i = 0; i < len; i++) {
*dst++ = arr[i];
}
break;
} else {
throw new CliError("getValue: Unsupported conversion type! "+Enum.GetName(typeof(CLI.FieldType), ((CLI.UnmanagedBuffer*)buffer.ToPointer())->type));
}
default:
throw new CliError("Unsupported type: "+Enum.GetName(typeof(CLI.FieldType), (CLI.FieldType)((CLI.UnmanagedBuffer*)buffer.ToPointer())->type));
}
}
也可以使用System.Text.Encoding.Default.GetBytes(s).Length,不過如果是奇葩系統長度可能會有變化,沒測試過,有興趣的同學可以試下
希望能對使用c#開發fastdb的朋友有所協助
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。