0

java code --

String plainText = "thisismyplaintext";
byte[] bytes = plainText.getBytes("UTF-8");

System.out.println(bytes);
// Output - [B@3cd1a2f1

I am trying to do the same thing in javascript but I am always getting absolutely different output like example - [11, 12, 14, 16].

I have tried everything from different stackoverfow answers but nothing is working similar to this java code.

Please help me to achieve the same thing in javascript and to get similar output.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Pankaj Saw
  • 21
  • 5
  • 1
    Please keep in mind that Java and JavaScript are not the same programming language and correct the tags. – JayJamesJay Jul 20 '19 at 06:48
  • @JayJamesJay I think OP is aware of this. OP wants to translate the Java code to Javascript code – Nick Parsons Jul 20 '19 at 06:49
  • 1
    @Nick Parsons You're right, my mistake. – JayJamesJay Jul 20 '19 at 06:50
  • The output of java Byte arrays is the object reference. Read about object.toString(). You will not be able to get this in JavaScript. – Jens Jul 20 '19 at 06:58
  • Is there a way to achieve same thing in Node.js. Sorry I have not mentioned that I am actually using Node.js I know Node.js is also javascript but there's lot's of library available. – Pankaj Saw Jul 20 '19 at 07:30
  • Even so, this is clearly a javascript problem. The `java` tag is inappropriate. Hint: think about what the purpose of tags is: to make it easier for *other* people to find Q&A's that are relevant to them. People trying to solve a problem like this will be looking for javascript Q&A's not java Q&A's. – Stephen C Jul 20 '19 at 07:31
  • @Stephen. Ok I just mentioned Java because Java code is included. – Pankaj Saw Jul 20 '19 at 07:45
  • Yes, but it is not **about** Java. – Stephen C Jul 20 '19 at 07:49
  • The other problem is that you haven't explained what output you actually want to be produced. (Especially since that Java output is probably bogus ... for most purposes.) What are you *actually* trying to achieve? – Stephen C Jul 20 '19 at 07:52
  • @Stephen Please check this - https://stackoverflow.com/questions/57123473/aes-128-cbc-encrytion-decryption-using-crypto-in-node-js-not-giving-proper-resul – Pankaj Saw Jul 20 '19 at 09:48

1 Answers1

1

[B@3cd1a2f1 is not the result you are after. [B@3cd1a2f1 represents the class type ([B) of your array of bytes and the hex representation of your array's hash. You need to instead print the array contents, which can be done using Arrays.toString():

String plainText = "thisismyplaintext";
byte[] bytes = plainText.getBytes(StandardCharsets.UTF_8);
System.out.println(Arrays.toString(bytes));

This will give the array:

[116, 104, 105, 115, 105, 115, 109, 121, 112, 108, 97, 105, 110, 116, 101, 120, 116]

This you can get the same result in Javascript using the following:

const str = "thisismyplaintext";
const utf8 = unescape(encodeURIComponent(str));
const arr = [...utf8].map(c => c.charCodeAt(0));
console.log(arr);

- Credit to this answer for byte array algorithm

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64