1 /*******************************************************************************
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
12 * Created on Jan 21, 2005
14 * Copyright Toni Kalajainen
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
20 * http://www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
28 package org.simantics.utils.bytes;
30 import org.simantics.utils.strings.EString;
33 * Byte array <-> string conversions
35 * All conversions have length in the beginning (32-bit integer) and
36 * followed by actual chars
38 * the length is in Little Endian
40 * @author Toni Kalajainen
42 public class LEString {
44 public static byte[] toBytes(String s) {
45 int size = s.length();
46 byte array[] = new byte[size + 4];
48 LEInt.write(size, array);
50 for (int i=0; i<size; i++)
51 array[i+4] = (byte) (s.charAt(i) );
56 * Write string to byte array
57 * @param str the string
58 * @param array the byte array
59 * @param offset the offset
61 public static void write(String str, byte array[], int offset)
63 int size = str.length();
64 if (offset+4+size>array.length)
65 throw new IndexOutOfBoundsException();
66 LEInt.write(size, array, offset);
67 for (int i=0; i<size; i++)
68 array[i+4+offset] = (byte) (str.charAt(i) );
72 * Write string to byte array
73 * @param str the string
74 * @param array the byte array
75 * @param offset the offset
77 public static void write(String str, byte array[])
79 int size = str.length();
80 if (4+size>array.length)
81 throw new IndexOutOfBoundsException();
82 LEInt.write(size, array);
83 for (int i=0; i<size; i++)
84 array[i+4] = (byte) (str.charAt(i) );
87 public static String toString(byte array[], int offset) {
88 if (offset+4>array.length)
89 throw new IndexOutOfBoundsException();
91 int size = LEInt.toInt(array, offset);
92 if (offset+4+size>array.length)
93 throw new IndexOutOfBoundsException();
95 //return new String(array, 4+offset, size);
96 char chars[] = new char[size];
97 for (int i=0; i<size; i++)
98 chars[i] = (char) (array[i+4+offset] & 0xff);
99 return new String(chars);
102 public static String toString(byte array[]) {
104 throw new IndexOutOfBoundsException();
106 int size = LEInt.toInt(array);
107 if (4+size>array.length)
108 throw new IndexOutOfBoundsException();
110 //return new String(array, 4, size);
111 char chars[] = new char[size];
112 for (int i=0; i<size; i++)
113 chars[i] = (char) (array[i+4] & 0xff);
114 return new String(chars);
117 public static void main(String[] args) {
118 String value = "STRING 01234"+(char)(128)+(char)(129)+(char)(255);
121 System.out.println(X+" = "+intToHex(X, 4));
123 byte array[] = toBytes(value);
124 System.out.print(value);
125 System.out.print(" = ");
126 printByteArray(array);
127 System.out.println();
130 System.out.print(value);
131 System.out.print(" = ");
132 printByteArray(array);
133 System.out.println();
135 write(value, array, 0);
136 System.out.print(value);
137 System.out.print(" = ");
138 printByteArray(array);
139 System.out.println();
141 value = toString(array, 0);
142 printByteArray(array);
143 System.out.print(" = ");
144 System.out.print(value);
145 System.out.println();
147 value = toString(array);
148 printByteArray(array);
149 System.out.print(" = ");
150 System.out.print(value);
151 System.out.println();
153 array = toBytes(value);
154 System.out.print(value);
155 System.out.print(" = ");
156 printByteArray(array);
157 System.out.println();
166 public static String intToHex(int value, int decimals) {
168 for (int i=0; i<decimals; i++) {
170 EString.HEX_VALUES[value & 0xF] + result;
172 EString.HEX_VALUES[(value>>4) & 0xF] + result;
178 public static void printByteArray(byte array[]) {
179 for (int i=0; i<array.length; i++) {
180 System.out.print(array[i] & 0xff);
181 if (i<array.length-1)
182 System.out.print(",");