1

I have a String say String a = "abc";. Now I want to convert it into a byte array say byte b[];, so that when I print b it should show "abc".

How can I do that? getBytes() method is giving different result.

My program looks like that so far:

String a="abc";
byte b[]=a.getBytes();

what I want is I have two methods made in a class one is public byte[] encrypt(String a) and another is public String decrypt(byte[] b) after doing encryption i saved the data into database but when i am getting it back then byte methods are not giving the correct output but i got the same data using String method but now I have to pass it into decrypt(byte[] b) How to do it this is the real scenario.

Durgesh Kumar
  • 935
  • 10
  • 17

6 Answers6

9

Well, your first problem is that a String in Java is not an array of bytes, but of chars, where each of them takes 16bit. This is to cover for unicode characters, instead of only ascii that you'd get with bytes. That means that if you use the getBytes method, you won't be able to print the string one array position at a time, since it takes two array positions (two bytes) to represent one character.

What you could do is use getChars and then cast each char to a byte, with the corresponding precision los. This is not a good idea since it won't work outside of normal English characters! You asked, though, so here you go ;)

EDIT: as @PeterLawerey mentions,Unicode characters make it even harder, with some unicode characters needing more than one char. There's a good discussion in StackOverflow and it links to an detailed article from Oracle.

Community
  • 1
  • 1
Miquel
  • 15,405
  • 8
  • 54
  • 87
1

You could use this constructor to build your string back again:

 String a="abc"; 
 byte b[]=a.getBytes("UTF-8");
 System.out.println(new String(b, "UTF-8"));

Other than that, you can't do System.out.println(b) and expect to see abc.

Boann
  • 48,794
  • 16
  • 117
  • 146
npinti
  • 51,780
  • 5
  • 72
  • 96
1
byte b[]=a.getBytes();
System.out.println(new String(b));
manurajhada
  • 5,284
  • 3
  • 24
  • 43
0

A byte is value between -128 and 127. When you print it, it will be a number by default.

If you want to print it as an ASCII char, you can cast it to a (char)

byte[] bytes = "abc".getBytes();
for(byte b: bytes)
    System.out.println((char) b);

prints

a
b
c
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
  1. This will convert "abc" to byte and then the code will print "abc" in respective ASCII code (ie. 97 98 99).

    byte a[]=new byte[160];
    String s="abc";
    a=s.getBytes();
    for(int i=0;i<s.length();i++)
    {
        System.out.print(a[i]+" ");
    }
    
  2. If you add these lines it will again change the ASCII code to String (ie. abc)

    String s1=new String(a);
    System.out.print("\n"+s1);
    

Hope it Helpes.

Modified Code:

To send byte array as an argument:

    public static void another_method_name(byte b1[])
    {
        String s1=new String(b1);
        System.out.print("\n"+s1);
    }
    public static void main(String[] args) 
    {
        byte a[]=new byte[160];
        String s="abc";
        a=s.getBytes();
        for(int i=0;i<s.length();i++)
        {
            System.out.print(a[i]+" ");
        }
        another_method_name(a);
    } 

Hope it helps again.

codeDEXTER
  • 1,181
  • 7
  • 14
  • well but i need byte array which contains the same value as in s. As i have to pass it as an argument to another method. I cannot pass 1 by 1 byte I have to pass it as a whole "abc" that too in the form of byte – Durgesh Kumar Jul 06 '12 at 08:03
0

It seems like you are implementing encryption and decryption code. String constructors are for text data. you should not use it to convert byte array which contains encrypted data to string value. You should use base64 instead, which encodes any binary data into ASCII.

this one is good public domain one

http://iharder.sourceforge.net/current/java/base64/

base64 apache commons

http://commons.apache.org/codec/download_codec.cgi

String msg ="abc";
byte[] data = Base64.decode(msg);
String convert = Base64.encodeBytes(data);
Chamila Adhikarinayake
  • 3,588
  • 5
  • 25
  • 32