Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

byte[] & String together

Posted on 2003-03-31
5
Medium Priority
?
168 Views
Last Modified: 2010-05-18
Hi!
I have a small problem.

This piece of code is ok.

String USER = new String("text for user");
String TEXT = new String("text number 1");
byte[] bajt = "text for user: text number 1".getBytes();


But I'd like to do sth. like below:

String USER = new String("text for user");
String TEXT = new String("text number 1");
byte[] bajt = "text for user:"+TEXT.getBytes();

...third line is wrong.
Can anyone help me???

Czarek

0
Comment
Question by:czaron
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8241842
The + operator does not work for arrays.


Try:

byte[] bajt = ("text for user:"+TEXT).getBytes();


0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8260184
>> byte[] bajt = ("text for user:"+TEXT).getBytes();

would eventually be pretty much the same as:

>> byte[] bajt = "text for user: text number 1".getBytes();

I guess. Isn't it, objects?

Mayank.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 8264131
yes they have the same result.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8267065
Yeah!

By the way, czaron,

>> byte[] bajt = "text for user:"+TEXT.getBytes();

is not possible that way. Since 'bajt' is a byte[] array, so you cannot directly assign a String object to it directly. Moreover, like objects said, the '+' operator does not work for arrays.

I also don't understand that why do you want your 'bajt' array to hold "text for user:"+TEXT.getBytes();? What is it supposed to mean?? If you want a byte[] array to hold the equivalent of a String, then you can use the getBytes () method, which I guess you already know. But what do you exactly mean by: "text for user:"+TEXT.getBytes();??

I would've done: byte[] bajt = (USER + TEXT).getBytes () ;

Please clarify..

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8297032
Was that an answer :-)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question