Java XML Data Type Converter convertToTwoBytes(int numberToConvert)

Here you can find the source of convertToTwoBytes(int numberToConvert)

Description

Converts an integer to two bytes.

License

Apache License

Parameter

Parameter Description
numberToConvert number to convert

Return

given number as bytes

Declaration

public static byte[] convertToTwoBytes(int numberToConvert) 

Method Source Code

//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;
    }
}

Related

  1. compress(String string)
  2. compress(String string)
  3. convertBpmnVariableValueToXslParam(final Object bpmnVariableValue)
  4. convertToFourBytes(int numberToConvert)
  5. convertToPEM(PublicKey key)
  6. createBasicAuthenticationProperty(final String username, final String password)
  7. decode(final CharSequence _text)
  8. decode(String auth)
  9. decode(String source)