BlackBoard (http://www.black-board.net/index.php)
- Design, Programmierung & Entwicklung (http://www.black-board.net/board.php?boardid=55)
-- Programmieren (http://www.black-board.net/board.php?boardid=4)
--- Java Umwandlungsproblem (http://www.black-board.net/thread.php?threadid=22946)


Geschrieben von kilone am 08.07.2007 um 14:12:

verrückt Umwandlungsproblem

Hi ich hab ein Problem mit der Umwandlung einzelner Zeichen.
Wenn ich z.B:
code:
1:
2:
3:
4:
5:
6:
String uni = "€";
byte [] byt = uni.getBytes();

System.out.println("String: "+uni+" ToByte[]: "+byt[0]+" ToChar:"+(char)byt[0]);


mache bekomme ich als Ausgabe:
code:
1:
String: € ToByte[]: -128 ToChar:?


Ich denke mal das das ein Problem mit dem ASCII ist weil € ein Unicode Zeichen ist. Aber wie kann ich das Java beibringe das er mir da wieder das richtige Zeichen ausgibt? Ich hoffe ihr könnt mir helfen weiß nämlich nicht mehr weiter unglücklich


mfg

Kilone



Geschrieben von phoenix am 08.07.2007 um 15:19:

 

Schau dir das mal genauer an.

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:

* Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.

/*
 * Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import java.io.*;
import java.util.*;

public class StringConverter {

   public static void printBytes(byte[] array, String name) {
      for (int k = 0; k < array.length; k++) {
         System.out.println(name + "[" + k + "] = " + "0x" +
            UnicodeFormatter.byteToHex(array[k]));
      }
   }

   public static void main(String[] args) {

      System.out.println(System.getProperty("file.encoding"));
      String original = new String("€");
   
      System.out.println("original = " + original);
      System.out.println();
   
      try {
          byte[] utf8Bytes = original.getBytes("UTF8");
          byte[] defaultBytes = original.getBytes();
 
          String roundTrip = new String(utf8Bytes, "UTF8");
          System.out.println("roundTrip = " + roundTrip);
 
          System.out.println();
          printBytes(utf8Bytes, "utf8Bytes");
          System.out.println();
          printBytes(defaultBytes, "defaultBytes");
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      }
      
   } // main

}



mfg


Forensoftware: Burning Board 2.3.6, entwickelt von WoltLab GmbH