?
Solved

byte[] & String together

Posted on 2003-03-31
5
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 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