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 Big Endian
40 * @author Toni Kalajainen
42 public class BEString {
44 public static byte[] toBytes(String s) {
45 int size = s.length();
46 byte array[] = new byte[size + 4];
48 BEInt.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 BEInt.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 BEInt.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 = BEInt.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 = BEInt.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);
123 public static String intToHex(int value, int decimals) {
125 for (int i=0; i<decimals; i++) {
126 result += EString.HEX_VALUES[(value>>4) & 0xF];
127 result += EString.HEX_VALUES[value & 0xF];
133 public static void main(String[] args) {
134 String value = "STRING 01234"+(char)(128)+(char)(129)+(char)(255);
137 System.out.println(X+" = "+intToHex(X, 4));
139 byte array[] = toBytes(value);
140 System.out.print(value);
141 System.out.print(" = ");
142 printByteArray(array);
143 System.out.println();
146 System.out.print(value);
147 System.out.print(" = ");
148 printByteArray(array);
149 System.out.println();
151 write(value, array, 0);
152 System.out.print(value);
153 System.out.print(" = ");
154 printByteArray(array);
155 System.out.println();
157 value = toString(array, 0);
158 printByteArray(array);
159 System.out.print(" = ");
160 System.out.print(value);
161 System.out.println();
163 value = toString(array);
164 printByteArray(array);
165 System.out.print(" = ");
166 System.out.print(value);
167 System.out.println();
169 array = toBytes(value);
170 System.out.print(value);
171 System.out.print(" = ");
172 printByteArray(array);
173 System.out.println();
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(",");