Here you can find the source of convertToTwoBytes(int numberToConvert)
Parameter | Description |
---|---|
numberToConvert | number to convert |
public static byte[] convertToTwoBytes(int numberToConvert)
//package com.java2s; /*//from w w w . j a va 2 s . c om * Copyright 2016-present Open Networking Laboratory * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import javax.xml.bind.DatatypeConverter; public class Main { /** * Converts an integer to two bytes. * * @param numberToConvert number to convert * @return given number as bytes */ public static byte[] convertToTwoBytes(int numberToConvert) { byte[] numInBytes = new byte[2]; String s1 = Integer.toHexString(numberToConvert); if (s1.length() % 2 != 0) { s1 = "0" + s1; } byte[] hexas = DatatypeConverter.parseHexBinary(s1); if (hexas.length == 1) { numInBytes[0] = 0; numInBytes[1] = hexas[0]; } else { numInBytes[0] = hexas[0]; numInBytes[1] = hexas[1]; } return numInBytes; } }