1

I have the following String-ified byte[]: "[0, 1, 2 , 3]".

And I would like to cast this into a byte[]. How can I do this in Java?

Ray
  • 627
  • 1
  • 7
  • 19
  • 3
    should the result be a byte array containing `{0, 1, 2, 3}`? In this case this is not "casting". – Federico klez Culloca Mar 23 '18 at 07:49
  • yes. I am trying to get that byte array. Sorry, I am not sure what word to use. – Ray Mar 23 '18 at 07:50
  • [Somewhat relatd](https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java). – user202729 Mar 23 '18 at 07:51
  • Possible way: remove `'['` and `']'` from the string, split on `','` and then parse the integers obtained in this way. This will work as long as the elements in the exploded string fit a byte. – Federico klez Culloca Mar 23 '18 at 07:52
  • Probably the `split` method of the `String` class and the `parseByte` method of the `Byte` class would both be useful for this. – Dawood ibn Kareem Mar 23 '18 at 07:52
  • @FedericoklezCulloca - I accidentally removed your comment. I am very sorry. I was about to vote up but I don't know what I clicked and it was removed. – Ray Mar 23 '18 at 07:56
  • 2
    @Ray it was an automatic comment from flagging as duplicate, and it was removed when I closed the question. No worries. – Kayaman Mar 23 '18 at 08:06

0 Answers0