Simple Dongdong, nothing to say, on the code.
View Code
Using System;
Using System. Collections. Generic;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data;
Using System. Text;
Public partial class GetPrintContent: System. Web. UI. Page
{
Protected string ClientCode = string. Empty;
Protected string LogoTxt = string. Empty;
Protected void Page_Load (object sender, EventArgs e)
{
If (! String. IsNullOrEmpty (Request ["Str"])
{
StringBuilder xmlString = new StringBuilder ();
This. ClientCode = "wins, lose, win, lose ";
XmlString. AppendLine ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ");
XmlString. AppendLine ("<PRINT> ");
XmlString. AppendLine ("<logo uri = \" Chinese and English string truncation fixed length \ "/> ");
String logoTxt = this. ClientCode. Replace ("\ r \ n ","");
If (logoTxt. Length/16 + 1)> 1)
{
Int startIndex = 0; // number of bytes www.2cto.com
Int subLen = 16;
Int subCount = logoTxt. Length/16 + 1;
For (int I = 0; I <subCount; I ++)
{
String oneOfVal = SubStrLenth (logoTxt, startIndex, subLen * 2 );
If (oneOfVal! = "")
{
XmlString. AppendLine ("<text val = \" "+ oneOfVal +" \ "/> ");
StartIndex + = GetStrByteLength (oneOfVal );
}
Else
{
Break;
}
}
}
Else
{
XmlString. AppendLine ("<text val = \" "+ logoTxt +" \ "/> ");
}
XmlString. AppendLine ("</PRINT> ");
Response. ContentEncoding = Encoding. UTF8;
Response. ContentType = "text/xml ";
Response. Charset = "UTF-8 ";
Response. Write (xmlString );
Response. End ();
//}
}
}
Private bool ISChinese (char C)
{
Return (int) C >=0x4e00 & (int) C <= 0x9FA5;
}
Private int GetStrByteLength (string str)
{
Return System. Text. Encoding. Default. GetByteCount (str );
}
Private string SubStrLenth (string str, int startIndex, int length)
{
Int strlen = GetStrByteLength (str );
If (startIndex + 1> strlen)
{
Return "";
}
Int j = 0; // number of bytes that record Traversal
Int L = 0; // records the number of bytes starting from each capture and traversing to the starting byte
Int strW = 0; // character width
Bool B = false; // The value of bool B is true only when the position where the screenshot starts is traversed.
String restr = string. Empty;
For (int I = 0; I <str. Length; I ++)
{
Char C = str [I];
If (ISChinese (C ))
{
StrW = 2;
}
Else
{
StrW = 1;
}
If (L = length-1) & (L + strW> length ))
{
B = false;
Break;
}
If (j> = startIndex)
{
Restr + = C;
B = true;
}
J + = strW;
If (B)
{
L + = strW;
If (L + 1)> length ))
{
B = false;
Break;
}
}
}
Return restr;
}
}
We hope to share the same path with you for more concise and convenient solutions.
From Proof of future