Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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 2116 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 »

Hi,

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 :
1  
2  
3  
4  
        char[] src="42:life universe everything".toCharArray();
        char[] dst=new char[8];
        System.arraycopy(src,0,dst,0,dst.length);
        myId=new String(dst);


or..

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


Good luck !


Erik
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?
userId.length>8?8:userId.length



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>

so...

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

would be similar to:

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

or:

int y;
if(x<0)
 y=-x;
else
 y=x;

弾幕 ☆ @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 »

1  
userId.length>8?8:userId.length;


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

1  
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...

DP

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 830
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:
1  
2  
3  
4  
if(var)
   doThis();
else
   doThat();


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


Only when both methods return the same type (and not void):
1  
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.

DP

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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xFryIx (57 views)
2014-11-13 12:34:49

digdugdiggy (35 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (50 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (148 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!