Algorithm: converts a string to a unicode byte array.

Source: Internet
Author: User

As long as the character encoding programming is involved, the temper will be abnormal. Looking at the pile of garbled code, you may not kill it with a knife!


There is a need:


Print the specified content using a printer and then communicate with the device according to the specified protocol.

The printer recognizes Unicode characters.


Therefore, you need to convert the specified content (string) to unicode encoding and then to the corresponding Unicode byte array.


Encoding implementation:


/*** Converts a string to a Unicode character. ** @ Param SRC * the string to be converted. * @ return byte [] byte array of the string. * @ throws nullpointerexception * If the input string is null, this exception is thrown. * @ throws illegalargumentexception * escape characters such as \ t should be \ t as parameters; otherwise, an exception is reported. */public static byte [] string2unicode (string SRC) throws nullpointerexception, illegalargumentexception {If (src = NULL) {Throw new nullpointerexception ("the parameter of SRC is null ");} // define a constant: space final string Space = ""; // define a constant: hexadecimal final int radix_hex = 16; If ("". equals (SRC) {src = SRC + space;} // String Length final int Len = SRC. length (); // string to Char [] utfchar = SRC. tochararray (); system. out. println ("utfchar length =" + utfchar. length); string hexstr = ""; // return value (UNICODE character encoding is two bytes) byte [] result = new byte [Len * 2]; for (INT I = 0; I <Len; I ++) {// convert to hexadecimal hexstr = integer. tohexstring (utfchar [I]); // system. out. println ("hexstr =" + hexstr); // Add the number 00if (hexstr. length ()> 1 & hexstr. length () <= 2) {hexstr = "00" + hexstr;} else {// If the escape character is written incorrectly (for example, \ t should be \ t ), this exception is reported // continue; throw new illegalargumentexception ("the parameter of SRC is error, and you 'd better check escaped character. ");} // capture string head = hexstr. substring (0, 2); string tail = hexstr. substring (2, 4); try {// hexadecimal number to byteresult [I * 2] = (byte) integer. parseint (Head, radix_hex); Result [I * 2 + 1] = (byte) integer. parseint (tail, radix_hex);} catch (numberformatexception NFE) {continue ;}} return result ;}


Test cases:


public static void main(String[] args) {String src = "UNICODE\\t\\n\\rG OD";System.out.println(src);byte[] xx;try {xx = DataUtil.string2Unicode(src);int size = xx.length;for(int i=0;i<size; i++) {System.out.println("xx[" + i + "]= " + xx[i]);}} catch (Exception e) {e.printStackTrace();}}


Test result of online Conversion Tool (hexadecimal ):


\ U55 ----------------------- 'U'

\ U4e ----------------------- 'n'

\ U49 ----------------------- 'I'

\ U43 ----------------------- 'C'

\ U4f ----------------------- 'O'

\ U44 ----------------------- 'D'

\ U45 ----------------------- 'E'

\ U5c \ U74 ----------------------- '\ t'

\ U5c \ u6e ----------------------- '\ N'

\ U5c \ u72 ----------------------- '\ R'

\ U47 ----------------------- 'G'

\ U20 ----------------------- ''(Space)

\ U4f ----------------------- 'O'

\ U44 ----------------------- 'D'


Test results:


XX [0] = 0 XX [1] = 85 ----------------------- 'U'
XX [2] = 0 XX [3] = 78 ----------------------- 'n'
XX [4] = 0 XX [5] = 73 ----------------------- 'I'
XX [6] = 0 XX [7] = 67 ----------------------- 'C'
XX [8] = 0 XX [9] = 79 ----------------------- 'O'
XX [10] = 0 XX [11] = 68 ----------------------- 'D'
XX [12] = 0 XX [13] = 69 --------------------- 'E'


XX [14] = 0 XX [15] = 92 ----------------------- '\ t'
XX [16] = 0 XX [17] = 116


XX [18] = 0 XX [19] = 92 ----------------------- '\ N'
XX [20] = 0 XX [21] = 110


XX [22] = 0 XX [23] = 92 --------------------- '\ R'
XX [24] = 0 XX [25] = 114


XX [26] = 0 XX [27] = 71 ----------------------- 'G'
XX [28] = 0 XX [29] = 32 -----------------------''
XX [30] = 0 XX [31] = 79 ----------------------- 'O'
XX [32] = 0 XX [33] = 68 ----------------------- 'D'


Implementation code defects (currently discovered ):


The escape character must be in this form: \ t


The test results are consistent!









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.