This is an algorithm. It is a problem that I solve when I print a small ticket for a thermal printer. I want to share it with you, or many of you may be helpful.
When printing a small ticket, you may encounter a problem where the string length is too long, but you cannot directly intercept it, so you need to wrap it automatically to display all the names. After analysis, it is actually such a problem:You need to split the string so that each line cannot exceed the maximum length. The last line is the specified minimum length. If the length is not enough, fill it with spaces.
First, we will provide a simple example of the effect:
Specific algorithm source code:
# Region "splits the string by the specified maximum length, so that the last line length is the specified minimum length" ''' <summary> ''' to handle the automatic line feed of the string. The shortest is intlenmin, the longest is intlenmax, And the last line is filled with spaces to the length of intlenmin. ''' </Summary> ''' <Param name = "stroldtext"> original string </param> ''' <Param name = "intlenmin"> shortest byte length </ param> ''' <Param name = "intlenmax"> maximum byte length </param> ''' <returns> string </returns> ''' <remarks> </Remarks> Public Function automaticline (byval stroldtext as string, byval intlenmin as integer, byval intlenmax as integer) as string dim intlength as integer dim strresult as string = "" 'Get the byte length of the original string intlength = system. text. encoding. getencoding ("gb2312 "). getbytecount (stroldtext) If intlength> intlenmax then' total byte count> maximum number of truncated bytes, 'the maximum number of truncated bytes, then process the remaining string and then 'obtain the ucs2 code of the string dim bytes as byte () = system. text. encoding. unicode. getbytes (stroldtext) 'obtains the actual captured position of the character dim intcutpos = realcutpos (bytes, intlenmax)' and uses recursive calls to strresult = system. text. encoding. unicode. getstring (bytes, 0, intcutpos * 2) + vbcrlf + automaticline (mid (stroldtext, intcutpos + 1), intlenmin, intlenmax) elseif intlength> intlenmin then' if the maximum number of bytes> total number of bytes> the minimum number of bytes, wrap the line and fill in spaces to the minimum number of bytes. strresult = stroldtext + vbcrlf + space (intlenmin) else 'if the total number of bytes is less than the minimum number of bytes, fill in spaces directly to the location of the minimum number of bytes strresult = stroldtext + space (intlenmin-intlength) end if return strresult end function ''' <summary> ''' returns the actual truncation position ''' </Summary> ''' <Param name = "bytes"> ucs2 </param> ''' <Param name = "intlength"> length of the byte to be intercepted </param> ''' <returns> </returns> ''' <remarks> </remarks> Public Function realcutpos (byval bytes as byte (), byval intlength as integer) as integer 'get ucs2 encoding dim intcountb as integer = 0' count the current number of bytes dim intcutpos as integer = 0' record the position of the captured bytes while (intcutpos <bytes. getlength (0) andalso intcountb <intlength) 'is an even number, such as 0, 2, and 4, if intcutpos mod 2 = 0 then' is the first byte in ucs2 encoding, the number of bytes plus 1 intcountb + = 1 else 'when the second byte of ucs2 encoding is greater than 0, the ucs2 character is chinese character. One Chinese character is counted as two bytes if bytes (intcutpos)> 0 then intcountb + = 1 end if intcutpos + = 1 end while 'If intcutpos is an odd number, if intcutpos mod 2 = 1 then' and the ucs2 character is a Chinese character, remove this half of the Chinese character if bytes (intcutpos)> 0 then intcutpos = intcutpos-1 else 'the ucs2' character is a letter or number, then retain this character intcutpos = intcutpos + 1 end if return intcutpos/2 end function # End Region
Because the algorithm processes data in bytes, it can process the mixing of Chinese and English characters, numbers, and various characters. When the last character captured is exactly Chinese characters, it will automatically process the previous character, garbled characters are not caused by truncation errors.