Simple String Question

B

Bob Campbell

I am new at Java programming and have gotten stuck on a problem
for which I am sure there is a simple solution.

I have a string (resp).
I need to create another string (resp2) which is the same thing as
resp, but with the 2-byte length of resp (in hex) at the beginning.

I've been able to do this in ASCII but have not found the right
combination of Java classes and methods and stuff to be able to
generate a String with the value in hex?

In C, this would just be strlen, sprintf, and strcat.

Is it possible to do this in Java?

Thanks!
 
N

Neil Campbell

Bob said:
I am new at Java programming and have gotten stuck on a problem
for which I am sure there is a simple solution.

I have a string (resp).
I need to create another string (resp2) which is the same thing as
resp, but with the 2-byte length of resp (in hex) at the beginning.

I've been able to do this in ASCII but have not found the right
combination of Java classes and methods and stuff to be able to
generate a String with the value in hex?

In C, this would just be strlen, sprintf, and strcat.

Is it possible to do this in Java?

Hi Bob,

You can get the length of resp by calling resp.length(). To convert this to
a hex value, you can use Integer.toString(int i, int radix). To
concatenate the strings you can just use the string addition operator:

String resp2 = lengthString + resp;

Hope this helps.
 
G

Greg Stark

Are you guys related? :)

Is there an upper bound on the length of a string? I would think that
strings good certainly have more the 2^16 characters in them, so two
bytes is not enough.
 
N

Neil Campbell

Greg said:
Are you guys related? :)

Hehe - not to my knowlege :)
Is there an upper bound on the length of a string? I would think that
strings good certainly have more the 2^16 characters in them, so two
bytes is not enough.

I don't think there is a limit (I'm not certain though), except of course
the amount of available memory. You can fairly easily either crop the
string to the required size though.

Similarly, I guess that depending on how the results are used, you may wish
to zero-pad the size field output for short strings.
 
B

Bob Campbell

Greg Stark said:
Are you guys related? :)

Is there an upper bound on the length of a string? I would think that
strings good certainly have more the 2^16 characters in them, so two
bytes is not enough.

Not related, no. (and thanks for the answer which I will try tomorrow
at work).

I forgot to mention that the string is guaranteed to be less than 64K
so there is no danger of the length being greater that 0xffff. This
is ISO 8583 financial stuff going back to a simulator which is
expecting those two bytes.

Thanks again.
 
J

John McGrath

I don't think there is a limit (I'm not certain though), except of course
the amount of available memory.

The length field is an int, so the maximumlength is Integer.MAX_VALUE,
which is 2,147,483,647 (0x7fffffff).
 
N

Neil Campbell

John said:
The length field is an int, so the maximumlength is Integer.MAX_VALUE,
which is 2,147,483,647 (0x7fffffff).

Ah yes, I'd overlooked that. Thanks for pointing that out.
 
B

bc21

Hi Bob,

You can get the length of resp by calling resp.length(). To convert this to
a hex value, you can use Integer.toString(int i, int radix). To
concatenate the strings you can just use the string addition operator:

String resp2 = lengthString + resp;

Hope this helps.

Thanks for the suggestion, but still no luck. Again, I am new at this
so I am not experienced enough with Java to be able to fill in the
blanks in your answer. I surely misinterpreted something.

I am
sure this is not hard to do, but I have not yet stumbled onto the
happy combination that works in this language. :)

Here are five combinations of things that won't compile:

(1) Integer reslen = new Integer(response.length());
String lenstr = toString(reslen, 16);

Error is "toString() in java.lang.Object cannot be applied
to (java.lang.Integer, int)"

(2) int reslen = response.length();
String lenstr = toString(reslen, 16);

Error is "toString cannot be applied to (int, int)"

(3) Integer reslen = new Integer(response.length());
String lenstr = Integer.toString(reslen, 16);

Error is "cannot resolve symbol
symbol: method toString(java.lang.Integer,int)
location: class.java.lang.integer(reslen,16)"

(4) Integer reslen = new Integer(response.length());
String lenstr = reslen.toSting(reslen, 16);

Error is same as (3).

(5) Integer reslen = response.length();
String length = toString((reslen, 16);

[Removed the "new Integer" here just for kicks].
Error is "incompatible types
found: int
required: java.lang.Integer
Integer reslen = response.length()

^


Anybody want to see if they can do this?
All I need to do is prepend a 2-byte length (in hex) to a string.

Thanks!
 
N

Neil Campbell

Here are five combinations of things that won't compile:

(1) Integer reslen = new Integer(response.length());
String lenstr = toString(reslen, 16);

Error is "toString() in java.lang.Object cannot be applied
to (java.lang.Integer, int)"

(2) int reslen = response.length();
String lenstr = toString(reslen, 16);

Error is "toString cannot be applied to (int, int)"

The problem with these two is that you haven't qualified toString, so it is
picking up the toString method of the class which contains this code. This
toString takes no arguments, so it won't compile like this.
(3) Integer reslen = new Integer(response.length());
String lenstr = Integer.toString(reslen, 16);

Error is "cannot resolve symbol
symbol: method toString(java.lang.Integer,int)
location: class.java.lang.integer(reslen,16)"

(4) Integer reslen = new Integer(response.length());
String lenstr = reslen.toSting(reslen, 16);

Error is same as (3).

This is more or less it. The only problem is that Integer.toString() takes
two ints, not an Integer and an int. You can either dispense with the
creation of the Integer object, or just extract the value from it when you
need it:

(from 3)
Integer reslen = new Integer(response.length());
String lenstr = Integer.toString(reslen.intValue(), 16);

(from 4)
int reslen = response.length();
String lenstr = reslen.toString(reslen, 16);
(5) Integer reslen = response.length();
String length = toString((reslen, 16);

[Removed the "new Integer" here just for kicks].
Error is "incompatible types
found: int
required: java.lang.Integer
Integer reslen = response.length()

This one is simply a case of trying to assign an int (as returned by
length()) to an Integer (reslen) - you can't do this directly as one is a
basic type and one is a class.

It's a bit confusing if you're new to the language, but you need to be aware
of this distinction between ints and Integers.

Cheers,
Neil
 
B

Bob Campbell

(e-mail address removed) wrote:

[some of my junk deleted here]



(from 3)
Integer reslen = new Integer(response.length());
String lenstr = Integer.toString(reslen.intValue(), 16);

(from 4)
int reslen = response.length();
String lenstr = reslen.toString(reslen, 16);

Cheers,
Neil

Thanks, Neil.
I am making progress now. Still not quite there, but...

Got it to finally compile using this combination:
Integer reslen = new Integer(response.length());
String lenstr = Integer.toString(reslen.intValue(), 16);

(Then... String newstr = lenstr + response to make the new string).

[The one with int that you had under "(from 4)" gave an error
of "int cannot be dereferenced" but the other one "from (3)"
compiled.]

The next problem is one that maybe can't be fixed in Java
if it's apples and oranges...

I need the length (which happens to be 57 decimal, 39 hex) to be
represented in the new string in the first two bytes/chars as the
actual length. For example if, say, the first 3 characters of the
response are XYZ (which have ASCII decimal values of 88, 89, & 90),
then a DECIMAL display of the new string should show this...
00 57 88 89 90.

But I am getting this...51 57 88 89 90 which means the first two bytes
are actually the hex representation of the length display in ASCII,
if that makes any sense (51 being 0x33 and 57 being 0x39, representing
3 and 9 in ASCII, the hexidecimal equivalent of the actual length).

(I am using this to print the first few bytes "(int)newstr.charAt(0) +
(int)newstr.charAt(1) +...etc" because nobody could tell me how to
display in hex, although I would guess you can do that in Java. As
it is, I am sure the way I am doing it is inefficient enough. It
looks pretty goofy, but it does what I want).

Anyhow...if you know of a way to get a "00 57" on the front of this
thing, I would appreciate it, but maybe String don't like the mix.

I have spent several hours now trying to find the right combination
of things that I could have done in C in about 3 or 4 statements.
Hopefully, I will have the time to learn this language some day with
the 3 Java books I already have. :)

Thanks!
 
N

Neil Campbell

Bob said:
(from 4)
int reslen = response.length();
String lenstr = reslen.toString(reslen, 16);

[The one with int that you had under "(from 4)" gave an error
of "int cannot be dereferenced" but the other one "from (3)"
compiled.]

Sorry, my fault - that should have been Integer.toString(reslen, 16) on the
second line.
The next problem is one that maybe can't be fixed in Java
if it's apples and oranges...

I need the length (which happens to be 57 decimal, 39 hex) to be
represented in the new string in the first two bytes/chars as the
actual length. For example if, say, the first 3 characters of the
response are XYZ (which have ASCII decimal values of 88, 89, & 90),
then a DECIMAL display of the new string should show this...
00 57 88 89 90.

But I am getting this...51 57 88 89 90 which means the first two bytes
are actually the hex representation of the length display in ASCII,
if that makes any sense (51 being 0x33 and 57 being 0x39, representing
3 and 9 in ASCII, the hexidecimal equivalent of the actual length).

Apologies once again, I misunderstood the original requirement. The easiest
way I can think of for getting this result is to turn the length into a
couple of characters explicitly, and adding them to string something like
this:

char c1 = (char)(len >> 8);
char c2 = (char)(len & 0xFF);
newString = String.valueOf(c1) + String.valueOf(c2) + oldString;

It's not particularly pretty, but it seems to work :)
 
B

Bob Campbell

The easiest
way I can think of for getting this result is to turn the length into a
couple of characters explicitly, and adding them to string something like
this:

char c1 = (char)(len >> 8);
char c2 = (char)(len & 0xFF);
newString = String.valueOf(c1) + String.valueOf(c2) + oldString;

It's not particularly pretty, but it seems to work :)

That combination worked!
THANKS!!!
 
V

Virgil Green

Bob said:
(e-mail address removed) wrote:

[some of my junk deleted here]



(from 3)
Integer reslen = new Integer(response.length());
String lenstr = Integer.toString(reslen.intValue(), 16);

(from 4)
int reslen = response.length();
String lenstr = reslen.toString(reslen, 16);

Cheers,
Neil

Thanks, Neil.
I am making progress now. Still not quite there, but...

Got it to finally compile using this combination:
Integer reslen = new Integer(response.length());
String lenstr = Integer.toString(reslen.intValue(), 16);

(Then... String newstr = lenstr + response to make the new string).

[The one with int that you had under "(from 4)" gave an error
of "int cannot be dereferenced" but the other one "from (3)"
compiled.]

The next problem is one that maybe can't be fixed in Java
if it's apples and oranges...

I need the length (which happens to be 57 decimal, 39 hex) to be
represented in the new string in the first two bytes/chars as the
actual length. For example if, say, the first 3 characters of the
response are XYZ (which have ASCII decimal values of 88, 89, & 90),
then a DECIMAL display of the new string should show this...
00 57 88 89 90.

But I am getting this...51 57 88 89 90 which means the first two bytes
are actually the hex representation of the length display in ASCII,
if that makes any sense (51 being 0x33 and 57 being 0x39, representing
3 and 9 in ASCII, the hexidecimal equivalent of the actual length).

(I am using this to print the first few bytes "(int)newstr.charAt(0) +
(int)newstr.charAt(1) +...etc" because nobody could tell me how to
display in hex, although I would guess you can do that in Java. As
it is, I am sure the way I am doing it is inefficient enough. It
looks pretty goofy, but it does what I want).

Anyhow...if you know of a way to get a "00 57" on the front of this
thing, I would appreciate it, but maybe String don't like the mix.

I have spent several hours now trying to find the right combination
of things that I could have done in C in about 3 or 4 statements.
Hopefully, I will have the time to learn this language some day with
the 3 Java books I already have. :)

Thanks!

I was "afraid" that was what you really wanted. How do you want the length
557 to be represented?

Would "05 57 88 89 90..." be what you wanted?
 
B

blmblm

I am new at Java programming and have gotten stuck on a problem
for which I am sure there is a simple solution.

I have a string (resp).
I need to create another string (resp2) which is the same thing as
resp, but with the 2-byte length of resp (in hex) at the beginning.

I've been able to do this in ASCII but have not found the right
combination of Java classes and methods and stuff to be able to
generate a String with the value in hex?

In C, this would just be strlen, sprintf, and strcat.

Is it possible to do this in Java?

I notice that downthread you say you've come up with a solution
that works, which is great but frankly surprises me, because of
something no one seems to have mentioned:

To the best of my knowledge, a Java "char" is not a byte (as in C),
but a 16-bit Unicode character. So if you want to create something
with a 2-byte hex value at the beginning, followed (presumably) by
some text, by manipulating the first two characters of a String,
hm, that sounds to me a lot like a "can't get there from here"
situation. (Not completely, obviously, if you've come up with
something that works, but close.)

Out of curiosity, what are you doing with this String after
creating it? turning it into a sequence of bytes and then ....
sending it over a network? passing it to something in C? ??
There might be a better solution than what has been proposed
so far ....

This might also explain why something that's easy in C is not
so easy in Java -- in C, "char" is more or less synonymous with
"unspecified 8-bit value", whereas in Java that's not the case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top