Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  byte[] => String => byte[]  (Read 1519 times)
0 Members and 1 Guest are viewing this topic.
Offline nonnus29

Senior Duke




Giving Java a second chance after ludumdare fiasco


« Posted 2011-03-02 04:55:11 »

Does anyone know how to take a byte[] to a String and back to a byte[] and get the same bytes you started with?  I figured this would just work signed bytes not with standing.  But it turns out the String(byte[]) converts negative bytes to unexpected characters, unexpected because I don't know what its doing.  Here an example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class Test {
    public static void main(String[] args) {
        try {
            byte[] b = new byte[] {0, -127, 0, 12};
            String s = new String(b, "ascii");
            printBytes(b);
            System.out.println("\ntest:" + s + ":");
            printBytes(s.getBytes("ascii"));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private static void printBytes(byte[] bytes) {
        System.out.print("bytes:");

        for(byte b : bytes) {
            System.out.format("%d,", b);
        }
    }
}


The output is:

1  
2  
3  
bytes:bytes:0,-127,0,12,
test: {unprintable characters}
bytes:bytes:0,63,0,12,


So -127 goes to 63, that makes not sense.  Does anyone have any ideas on how to go about doing this?  Or what a good google search phrase would be?
Offline tberthel
« Reply #1 - Posted 2011-03-02 05:56:30 »

-127 is not ASCII code.  0-255 is traditionally Extended ASCII.

Offline nonnus29

Senior Duke




Giving Java a second chance after ludumdare fiasco


« Reply #2 - Posted 2011-03-02 06:06:54 »

-127 is not ASCII code.  0-255 is traditionally ASCII.

Right, -127 is 0x81 in hex which is 129 in unsigned byte, in signed byte it's -127.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm

Senior Duke


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #3 - Posted 2011-03-02 07:02:09 »

Check out base 64 encoding
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2011-03-02 07:06:29 »

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  
public static void main (String[] args) throws Exception {
   byte[] bytes = new byte[256];
   for (int i = 0; i < 256; i++) {
      bytes[i] = (byte)i;
      System.out.print(bytes[i]);
      System.out.print(", ");
   }
   System.out.println();

   StringBuilder buffer = new StringBuilder(bytes.length);
   for (byte b : bytes)
      buffer.append((char)b);

   byte[] bytes2 = new byte[buffer.length()];
   int i = 0;
   for (char c : buffer.toString().toCharArray()) {
      bytes2[i] = (byte)c;
      System.out.print(bytes2[i]);
      System.out.print(", ");
      if (bytes2[i] != bytes[i]) System.out.println("\no noes!");
      i++;
   }

   System.out.println(buffer);
}

Offline pjt33
« Reply #5 - Posted 2011-03-02 08:02:43 »

Use "ISO-8859-1" or "UTF-8" instead of "ASCII".
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #6 - Posted 2011-03-02 09:03:40 »

ASCII's 0-127, not 0-255.

Cas Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2011-03-02 14:55:49 »

Use "ISO-8859-1" or "UTF-8" instead of "ASCII".

I cannot stress this enough: you can not use UTF8 for encoding/decoding binary.
You will end up with corrupted data in the decoded byte[].

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2011-03-02 14:57:19 »

http://www.java-gaming.org/topics/so-never-use-utf-8-encoding-for-binary-stuff/20316/view.html

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline pjt33
« Reply #9 - Posted 2011-03-02 15:46:56 »

Use "ISO-8859-1" or "UTF-8" instead of "ASCII".

I cannot stress this enough: you can not use UTF8 for encoding/decoding binary.
You will end up with corrupted data in the decoded byte[].
That'll teach me to read the question again before answering. By the time I posted I was thinking in terms of String -> byte[] -> String.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nonnus29

Senior Duke




Giving Java a second chance after ludumdare fiasco


« Reply #10 - Posted 2011-03-02 17:18:54 »

Thanks guys, yes utf-8 doesn't work, but ISO-8859-1 works.

Doesn't solve my problem since what I really need to do is go from ascii to utf-16be and back.

Edit:  apparently

1  
String s = new String(bytes, "UTF-16BE");


and

1  
s.getBytes("ISO-8859-1");


does the trick after using CharsetEncoder and CharsetDecoder.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!