X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2Fbytes%2FLEInt.java;h=1fb3417f94dcaf6d823cc9193910f0ff757be587;hb=e0f95bc4e0fa1797b9b97aec3b3329364ca634cf;hp=a5e0349eeb801641207d1e79c776b675ea583f83;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEInt.java b/bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEInt.java index a5e0349ee..1fb3417f9 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEInt.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEInt.java @@ -1,159 +1,159 @@ -/******************************************************************************* - * Copyright (c) 2007- VTT Technical Research Centre of Finland. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -/* - * Created on Jan 21, 2005 - * - * Copyright Toni Kalajainen - * - * 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. - */ -package org.simantics.utils.bytes; - -/** - * Little Endian int <-> byte array conversions - * Intel Order - * - * @author Toni Kalajainen - */ -public class LEInt { - - /** - * Convert int to byte array - * @param l int value - * @return byte array - */ - public static byte[] toBytes(int value) - { - byte array[] = new byte[4]; - array[3] = (byte) (value & 0xff); - array[2] = (byte) ((value >> 8) & 0xff); - array[1] = (byte) ((value >> 16) & 0xff); - array[0] = (byte) ((value >> 24) & 0xff); - return array; - } - - /** - * Write int value to byte array - * @param value the int value - * @param array the byte array - * @param offset the offset - */ - public static void write(int value, byte array[], int offset) - { - if (offset+4>array.length) - throw new IndexOutOfBoundsException(); - array[3 + offset] = (byte) (value & 0xff); - array[2 + offset] = (byte) (value >> 8); - array[1 + offset] = (byte) (value >> 16); - array[0 + offset] = (byte) (value >> 24); - } - - /** - * Write int value to byte array - * @param value the int value - * @param array the byte array - * @param offset the offset - */ - public static void write(int value, byte array[]) - { - if (array.length<4) - throw new IndexOutOfBoundsException(); - array[3] = (byte) (value & 0xff); - array[2] = (byte) (value >> 8); - array[1] = (byte) (value >> 16); - array[0] = (byte) (value >> 24); - } - - /** - * read int value from byte array - * @param array the array - * @param offset offset - * @return the value - */ - public static int toInt(byte array[], int offset) - { - if (offset+4>array.length) - throw new IndexOutOfBoundsException(); - int value = - ( ((int) array[3 + offset] & 0xFF) ) | - ( ((int) array[2 + offset] & 0xFF) << 8) | - ( ((int) array[1 + offset] & 0xFF) << 16) | - ( ((int) array[0 + offset] & 0xFF) << 24); - return value; - } - - /** - * read int value from byte array - * @param array the array - * @return the value - */ - public static int toInt(byte array[]) - { - if (4>array.length) - throw new IndexOutOfBoundsException(); - int value = - ( ((int) array[3] & 0xFF) ) | - ( ((int) array[2] & 0xFF) << 8) | - ( ((int) array[1] & 0xFF) << 16) | - ( ((int) array[0] & 0xFF) << 24); - return value; - } - - /** - * Test cases - * @param args - */ - public static void main(String[] args) { - System.out.println("min="+Integer.MIN_VALUE+" max="+Integer.MAX_VALUE); - int value = -1290000000; - byte array[] = toBytes(value); - System.out.print(value); - System.out.print(" = "); - printByteArray(array); - System.out.println(); - - write(value, array, 0); - System.out.print(value); - System.out.print(" = "); - printByteArray(array); - System.out.println(); - - value = toInt(array, 0); - printByteArray(array); - System.out.print(" = "); - System.out.print(value); - System.out.println(); - - value = toInt(array); - printByteArray(array); - System.out.print(" = "); - System.out.print(value); - System.out.println(); - } - - public static void printByteArray(byte array[]) { - for (int i=0; i byte array conversions + * Intel Order + * + * @author Toni Kalajainen + */ +public class LEInt { + + /** + * Convert int to byte array + * @param l int value + * @return byte array + */ + public static byte[] toBytes(int value) + { + byte array[] = new byte[4]; + array[3] = (byte) (value & 0xff); + array[2] = (byte) ((value >> 8) & 0xff); + array[1] = (byte) ((value >> 16) & 0xff); + array[0] = (byte) ((value >> 24) & 0xff); + return array; + } + + /** + * Write int value to byte array + * @param value the int value + * @param array the byte array + * @param offset the offset + */ + public static void write(int value, byte array[], int offset) + { + if (offset+4>array.length) + throw new IndexOutOfBoundsException(); + array[3 + offset] = (byte) (value & 0xff); + array[2 + offset] = (byte) (value >> 8); + array[1 + offset] = (byte) (value >> 16); + array[0 + offset] = (byte) (value >> 24); + } + + /** + * Write int value to byte array + * @param value the int value + * @param array the byte array + * @param offset the offset + */ + public static void write(int value, byte array[]) + { + if (array.length<4) + throw new IndexOutOfBoundsException(); + array[3] = (byte) (value & 0xff); + array[2] = (byte) (value >> 8); + array[1] = (byte) (value >> 16); + array[0] = (byte) (value >> 24); + } + + /** + * read int value from byte array + * @param array the array + * @param offset offset + * @return the value + */ + public static int toInt(byte array[], int offset) + { + if (offset+4>array.length) + throw new IndexOutOfBoundsException(); + int value = + ( ((int) array[3 + offset] & 0xFF) ) | + ( ((int) array[2 + offset] & 0xFF) << 8) | + ( ((int) array[1 + offset] & 0xFF) << 16) | + ( ((int) array[0 + offset] & 0xFF) << 24); + return value; + } + + /** + * read int value from byte array + * @param array the array + * @return the value + */ + public static int toInt(byte array[]) + { + if (4>array.length) + throw new IndexOutOfBoundsException(); + int value = + ( ((int) array[3] & 0xFF) ) | + ( ((int) array[2] & 0xFF) << 8) | + ( ((int) array[1] & 0xFF) << 16) | + ( ((int) array[0] & 0xFF) << 24); + return value; + } + + /** + * Test cases + * @param args + */ + public static void main(String[] args) { + System.out.println("min="+Integer.MIN_VALUE+" max="+Integer.MAX_VALUE); + int value = -1290000000; + byte array[] = toBytes(value); + System.out.print(value); + System.out.print(" = "); + printByteArray(array); + System.out.println(); + + write(value, array, 0); + System.out.print(value); + System.out.print(" = "); + printByteArray(array); + System.out.println(); + + value = toInt(array, 0); + printByteArray(array); + System.out.print(" = "); + System.out.print(value); + System.out.println(); + + value = toInt(array); + printByteArray(array); + System.out.print(" = "); + System.out.print(value); + System.out.println(); + } + + public static void printByteArray(byte array[]) { + for (int i=0; i