Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (711)
Games in Android Showcase (213)
games submitted by our members
Games in WIP (785)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Question regarding StringUtils.bytesToHex()  (Read 3151 times)
0 Members and 1 Guest are viewing this topic.
Offline shinova

Senior Newbie

« Posted 2006-11-29 04:12:34 »

Hi guys,

I'm trying to reduce the length of IDs to 8 characters in J2ME. From what i see in J2SE, it works  perfectly fine doin this way
myID = StringUtils.bytesToHex(userID, userID.length - 4);

however in J2ME, it didn't work that way.   Undecided
Offline ctrl-alt-dev

Senior Newbie

Today, my bedroom. Tomorrow the World !

« Reply #1 - Posted 2006-11-29 14:17:40 »


Mmm. I cant seem to find the class StringUtils and the method bytesToHex in the JDK docs.

If I look at your code I think you're converting a byte/char array into a hexadecimal string minus
the last 4 bytes/chars. One byte/char would correspond to 2 digits in the resulting string.

So limiting the length to 8 would only work if the original userId would be exactly 8 bytes
-> abcdefgh minus 4 -> abcd -> 61626364
If it would be, lets say 6 bytes the result would be :
-> abcdef minus 4 -> ab -> 6162
Oops :-)

If you would want to limit the length of an array why not use System.arraycopy :
        char[] src="42:life universe everything".toCharArray();
        char[] dst=new char[8];
        myId=new String(dst);


       myId=new String(userId,0,userId.length>8?8:userId.length);

Good luck !

Offline shinova

Senior Newbie

« Reply #2 - Posted 2006-11-30 03:41:26 »

wow, it works using this,
myId=new String(userId,0,userId.length>8?8:userId.length);
thanks!!   Grin

but could you roughly explain to me the meaning of this line?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #3 - Posted 2006-11-30 06:55:53 »

<condition>?<do this if its true>:<do this if its false>


int y=x<0?-x:x;

would be similar to:

int y=(int)Math.abs(x);


int y;

弾幕 ☆ @mahonnaiseblog
Offline pepijnve

Junior Devvie

Java games rock!

« Reply #4 - Posted 2006-11-30 11:18:43 »

You should probably replace "do this" with "the value of this". Otherwise it sounds like ?: is a flow control statement.
Offline darkprophet

Senior Devvie

Go Go Gadget Arms

« Reply #5 - Posted 2006-11-30 11:35:15 »


Is the exact same (if you are using ints, which you are) to:

Math.max(user.length, 8);

The only difference is that the 2nd version is more verbose and it does some funky stuff with NaN and negative zeros in float and doubles...


Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline Riven

« JGO Overlord »

Medals: 1260
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #6 - Posted 2006-11-30 14:24:48 »

It's not equal...

you can do:

But you can't do:
(var ? doThis() : doThat());

Only when both methods return the same type (and not void):
result = (var ? doThis() : doThat());

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline darkprophet

Senior Devvie

Go Go Gadget Arms

« Reply #7 - Posted 2006-11-30 15:18:52 »

yes, but we are talking about array.length which is of type int...there is no problem with methods in this case.

But yes, you are right, both methods need to return the same type and not void obviously.


Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline shinova

Senior Newbie

« Reply #8 - Posted 2006-12-01 01:26:21 »

icic.., thanks for all the explanation. really appreciate it!  Wink
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Archive (9 views)
2017-02-27 19:41:49

Riven (23 views)
2017-02-27 17:26:59

numerical (400 views)
2017-02-21 07:32:16

numerical (401 views)
2017-02-21 07:31:46

theagentd (516 views)
2017-02-18 13:42:33

theagentd (512 views)
2017-02-18 13:35:16

h.pernpeintner (1677 views)
2017-01-24 22:39:11

h.pernpeintner (1660 views)
2017-01-24 22:38:32

Galdo (2226 views)
2017-01-12 13:44:09

Archive (2176 views)
2017-01-02 05:31:41
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!