JAVA byte assignment Problem __java

Source: Internet
Author: User

This two days using Java for message encoding and decoding, always encountered a problem, need to give byte assignment.
Such as

It is quite troublesome to assign the value of a first address to a variable of AA. I wrote one today, record it.

    public static  byte generatebyte (String s) {

        byte tmp = 0;
        char[] arr = S.tochararray ();
        if (Arr.length > 2) {return
            tmp;
        }
        int t0 = Integer.parseint (character.tostring (arr[0));
        int T1 = Integer.parseint (character.tostring (arr[1));
        byte tmp0 = (byte) t0;
        byte Tmp1 = (byte) t1;
        TMP = (byte) (Tmp0 << 4);
        TMP = (byte) (tmp | tmp1);
        return tmp;
    }

byte array Print code

/** * * @Title: Gethexbyte * @Description: Converts a binary stream into a printable string type * @para
    M b: Need to print the stream * @param start: Print start point * @param length: Print length * @return * @return: StringBuilder * *
        public static StringBuilder Gethexbyte (byte[] b,int start, int length) {int t = 0;
        StringBuilder sb = new StringBuilder (); for (int i = start; I < start + length; i++) {if (i > 2 && b[i-1] = = ' U ' && b[i-2] = = '
            U ') {break;
            String hex = integer.tohexstring (B[i] & 0xFF);
            if (hex.length () = = 1) {hex = ' 0 ' + hex;

            } sb.append (""). Append (Hex.touppercase ());
            t++;
            if (t% = = 0) {sb.append ("\ n");
        } sb.append ("\ n");  
    return SB; }

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.