2

I try to convert string to byte array. I know that the simplest way is to call string.getBytes(), however...

Here's my string: 111dpd7.pl/wp

When I call getBytes() it returns array like this:

[11, 49, 100, 112, 100, 55, 46, 112, 108, 47, 119, 112, 0]

I think this is some kind of different codeing, right ? How can I convert char like f.e. d , or p to byte correctly? Thanks in advance for help :)

Bartos
  • 1,007
  • 3
  • 15
  • 38
  • The start and end of your byte array look strange indeed. Can you provide more code, which shows how you create the string and how you print your resulting byte array? – Minsky Nov 23 '16 at 08:47
  • 2
    Looks like everything is OK with encoding (more or less). Try `"paste here your string".getBytes("UTF-8")` to ensure that the problem is not with encoding. For string you pasted in the question I got: `[49, 49, 49, 100, 112, 100, 55, 46, 112, 108, 47, 119, 112]` - which is quite similar to what you have. But start and end of array are different. You can also try to create string from your byte array: `new String(bytes, "UTF-8")` - look what you have as a result and whether it equals to what you expect your original string is – krossovochkin Nov 23 '16 at 08:49
  • start and end of array are different cause I do add array with index at the beggining - that's ok :) However, how can I convert that byte to String again ? f.e. 49 to "p" and so on ? – Bartos Nov 23 '16 at 09:41
  • @Bartos. Your comment asks a different question, than your inital question. The answer to the last one can be found for instance here: http://stackoverflow.com/questions/1536054/how-to-convert-byte-array-to-string-and-vice-versa – Minsky Nov 23 '16 at 10:10

0 Answers0