The problem of truncation of fastdb characters

Source: Internet
Author: User
Tags unsupported

Fastdb the C # version, if the field type is defined as CLI. Fieldtype.cli_asciiz, the use of the process of inserting the Chinese character set will appear garbled situation,

Tracing code discovery is in the process of Copybufferdata the string buffer directly fastdb directly using the s.length to obtain the number of characters, rather than get the number of bytes, because Chinese occupies two bytes, so the data copy is not complete, resulting in garbled.

Not much to say, the fix code is as follows:

protected int Bytelengh (String str)
{
Converts a string to a byte array using Unicode encoding, which stores all strings (including English 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 is 2 because all the even-numbered subscript elements in the byte array are the first byte of the Unicode character
if (i% 2 = = 0)
{
j + +;
}
Else
{
The singular subscript is the 2nd byte of the character, and if the 2nd byte of a character is 0, the Unicode character is an English character, otherwise Chinese characters
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);
Correcting problems with Chinese characters intercepting errors
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, but if it is a wonderful system length may change, not tested, interested students can try

Hope to be helpful to friends who develop FASTDB with C #

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The problem of truncation of fastdb characters

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.