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