Fastdb's Chinese Character truncation error

Source: Internet
Author: User

Fastdb's Chinese Character truncation error

In Fastdb C #, if the field type is defined as CLI. FieldType. cli_asciiz, garbled characters may occur when the Chinese character set is inserted,

Tracing code found that during the CopyBufferData process of the string buffer, fastdb directly uses s. length gets the number of characters, rather than the number of bytes. Because the Chinese character occupies two bytes, data copy is incomplete, leading to garbled characters.

The corrected code is as follows:

Protected int bytelengh (string str)
{
// Convert a string to a byte array using Unicode encoding. It stores all strings (including English and Chinese) in 2 bytes.
Byte [] bytestr = System. Text. Encoding. Unicode. GetBytes (str );
Int j = 0;
For (int I = 0; I <bytestr. GetLength (0); I ++)
{
// The remainder 2 is obtained because all the elements in the byte array with double numbers are the first byte of the unicode character.
If (I % 2 = 0)
{
J ++;
}
Else
{
// The singular subscript is the 2nd bytes of the character. If the value of a single character's 2nd bytes is 0, the Unicode character is an English character. Otherwise, the Unicode character is a Chinese character.
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 );
// Correct the Chinese character truncation error
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 ));
}
}


You can also use System. Text. Encoding. Default. GetBytes (s). Length. However, if the Length of the wonderful System may change, it has not been tested. If you are interested, try it.

I hope it will be helpful for my friends who use c # To develop fastdb.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.