Print the small ticket automatically wrapping algorithm, can handle various character mixed row

Source: Internet
Author: User
Tags mixed ticket

This is an algorithm, I do in the thermal printer to print a small ticket to solve a problem, think about it or share to everyone, or a lot of people have a bit of help.

When printing a small ticket, you may encounter some problems with too long string length, but you can not intercept directly, so you need to wrap the line to display all the names. After analysis, is actually such a problem: the need to put the string branch, so that each line must not exceed the maximum length, the last line length of the specified minimum length, length is not enough, padded with space.

First, give a simple example of the effect of the diagram:

The specific algorithm source code:

#Region "line up the string by the specified maximum length, so that the last line length is the specified minimum length" ' <summary> ' to handle the string wrapping problem.
    The shortest is intlenmin, the longest is Intlenmax, and the last line is padded with space to intlenmin length. ' </summary> ' <param name= ' Stroldtext ' > Original string </param> ' <param name= ' intlenmin ' > Shortest byte length
    Degrees </param> ' <param name= ' Intlenmax ' > Maximum byte length </param> ' <returns>string</returns> ' <remarks></remarks> public Function automaticline (ByVal stroldtext as String, ByVal intlenmin as in

        Teger, ByVal Intlenmax As Integer) As String Dim intlength As Integer Dim strresult As String = "" ' Gets the byte length of the original string intlength = System.Text.Encoding.GetEncoding ("gb2312"). GetByteCount (stroldtext) If intlength > Intlenmax Then ' total bytes > maximum maximum number of bytes intercepted, ' then intercept the maximum number of bytes , and then processing the remaining strings ' get the UCS2 code Dim bytes as Byte () = System.Text.Encoding.Unicode.GetBytes of the string (Stroldtext ) ' Gets the actual intercept position of the character Dim INTCUTPos = Realcutpos (bytes, Intlenmax) ' uses recursive call strresult = System.Text.Encoding.Unicode.GetString (bytes , 0, Intcutpos * 2) + vbCrLf + automaticline (Mid (Stroldtext, Intcutpos + 1), intlenmin, Intlenmax) ElseIf Intleng th > Intlenmin Then ' If the maximum number of bytes > Total bytes > The shortest number of bytes, then the line, and padded space to the shortest number of bytes position strresult = Stroldtext + V Bcrlf + Space (intlenmin) Else ' If the total number of bytes < The shortest number of bytes, the direct completion of spaces to the shortest number of bytes strresult = Stroldtext + spaces (intlenmin-intlength) End If returns strresult end Function ' <summary> ' Back to the actual intercept position of the character ' ' </summary> ' <param name= ' bytes ' >ucs2 code </param> ' <param name= ' intlengt H "> Byte length to intercept </param> ' <returns></returns> '" ' <remarks></remarks> public Fu  Nction realcutpos (ByVal bytes as Byte (), ByVal intlength As Integer) As Integer gets the UCS2 encoded Dim INTCOUNTB as Integer = 0 ' statistic currentThe number of bytes Dim intcutpos as Integer = 0 ' records where the byte is to be intercepted while (Intcutpos < bytes. GetLength (0) AndAlso Intcountb < intlength) ' even position, such as 0, 2, 4, for the first byte of two bytes in the UCS2 code if Intcutpos Mod 2 = 0 Then ' When the first byte is UCS2, the number of bytes plus 1 intcountb + = 1 Else ' When UCS2 encoded
                Two bytes greater than 0 o'clock, the UCS2 character is kanji, one Chinese character is two bytes If bytes (intcutpos) > 0 Then intcountb = 1 End If If Intcutpos + + 1 ' while ' if Intcutpos is odd, it is processed into even I
                F Intcutpos Mod 2 = 1 Then ' The UCS2 character is kanji, remove this truncated kanji if bytes (intcutpos) > 0 Then Intcutpos = IntCutPos-1 Else ' The UCS2 character is a letter or a number, the character is reserved intcutpos = Intcutpos + 1 "End If" Intcutpos/2 end Function #End Region

Because the algorithm is processed in terms of bytes, so you can handle the Chinese and English, numbers and a variety of characters of the mixed row, when the interception of the last one is just Chinese characters, it will automatically handle the previous character, will not appear to intercept errors and lead to garbled.

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.