Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

conversions  RSS feed

 
Chris Pat
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
If I have my string in a byte[]
byte[] myString = "testString";
how do I print it out? I have tried myString.toString(), however that just gives me the pointer to the byte[] on the heap. How does this then work with the locale? tia.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your encoding? Try using one of String's constructors:

String(byte[] bytes) //platform's default charset
String(byte[] bytes, String charsetName)
[ December 29, 2005: Message edited by: Jeff Albrechtsen ]
 
Chris Pat
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff
Thanks. However it still in not working. I have created a list with below and I still get the same results

...
byte[] string0 = "ListString1".getBytes();
byte[] string1 = "ListString2".getBytes();
byte[] string2 = "ListString3".getBytes();
List list = new ArrayList();
list.add(string0);
list.add(string1);
list.add(string2);
...



...
boolean firstTime = true;
for (int i = 0; i < len; i++) {
if (firstTime) {
firstTime = false;
} else {
buffer1.append(',');
}
byte[] element = (byte[]) value_.get(i);
buffer.append("A ");
System.out.println("tempChar= "+new String(element.toString()));
buffer.append(new String(element.toString()));
}
buffer1.append('}');
return buffer1.toString();
...
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's time to step back. That code you posted was really torturing strings! What are you trying to do, what is your goal? What do you need byte[]'s? What aren't you working solely with Strings?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!