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的朋友有所帮助
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/natahew/article/details/48136633