Several implementations of converting to 16 in Java

Source: Internet
Author: User

Tag:throw   print    Implementation    final   ring    nal   %x   throws   git   

public class Hexutil {private static final string[] Digits_upper = {"0", "1", "2", "3", "4", "5", "6", "7",    "8", "9", "A", "B", "C", "D", "E", "F"};        public static void Main (string[] args) throws Decoderexception {System.out.println (ToHex1 ((byte)-128));        System.out.println (ToHex2 ((byte)-128));        System.out.println (ToHex3 ((byte)-128));    System.out.println (ToHex4 ((byte)-128));        } public static String ToHex1 (byte value) {int high = (value & 0xF0) >>> 4;        int low = value & 0x0F;    return Digits_upper[high] + Digits_upper[low];        } public static String ToHex2 (byte value) {int high = (value >>> 4) & 0x0F;        int low = value & 0x0F;    return Digits_upper[high] + Digits_upper[low];        } public static String toHex3 (byte value) {int tmp = value;        if (value < 0) {TMP = value + 256;        } int high = TMP/16; int low = tmp% 16;    return Digits_upper[high] + Digits_upper[low];    } public static String toHex4 (byte value) {return String.Format ("%x", value); }}
Reference

Complement

Several implementations of converting to 16 in Java

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.