java :'(

Any other programming language that is not covered in the Web Scripting category.

java :'(

Postby cjtenny on Fri Jan 20, 2006 12:32 am

yeah... i went on the IRC channel and only two people out of like 30 responded. so now i'm here.

yes this is my first post, yes i am trying to get a shell account.... but i figured, i was going to post this on some forum anyways, may as well be here.

anyways........

i'm trying to get the following C code to work in java, but i don't know how since java actually has strings.............. i know it works in C, i use it. this isnt a full program, just two functions used by my encryption algorithm.......... so yeah. any ideas on how i would do this in java?

Code: Select all
char toch(int w) {
   if (w > 9) return (w + 55);
   else return (w + 48);
}

int toi(char w) {
   if (w > 64) return (w - 55);
   else return (w - 48);
}

int leng(char* str) {
  int x;
  for (x = 0; str[x] != 0; x++) {}
  return x;
}

void revstr(char* str) {
  char tmp;
  int l = leng(str), l2 = l - 1, ong;
  for (ong = 0; ong < (l / 2); ong++) {
    tmp = str[ong];
    str[ong] = str[l2 - ong];
    str[l2 - ong] = tmp;
  }
}


toi and toch (to integer / character, my atoi replacements) just use ascii codes.
leng is my strlen replacement............

and sorry for the lack of comments.
cjtenny
Newbie
Newbie
 
Posts: 2
Joined: Thu Jan 19, 2006 9:51 pm

Postby al_tyranny on Fri Jan 20, 2006 3:21 am

Like I said before, I don't know basic Java fully, but I do know Google:

What the Mighty and Powerful Google doth sayeth is:
1) Make a StringBuffer object to screw with first.
RTFM! wrote:A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.


2) Once thou understandeth thy concept, thou shalt Google again, for a coherent answer (I tried "reverse java string"):

Code: Select all

// http://java.sun.com/docs/books/tutorial/java/data/stringsummary.html
protected static String reverse(String string) {
     StringBuilder sb = new StringBuilder(string);

        return sb.reverse().toString();
}


Oops! "StringBuilder" vs "StringBuffer"! Find the docs for StringBuilder and you'll find that it's pretty much the same thing as StringBuffer, except "but with no guarantee of synchronization." However, "Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations."

Since you're not using any threads, it's probably safe to use StringBuilder. It's up to you though.
mm_freak wrote:Slackware users are dreamers. Sorry to say.
al_tyranny
Phoenix Veteran
Phoenix Veteran
 
Posts: 294
Joined: Tue May 17, 2005 3:18 am
Location: Greater Boston Area

Postby cjtenny on Fri Jan 20, 2006 4:45 pm

Hmm. I kinda didnt get that, but google i shall.
cjtenny
Newbie
Newbie
 
Posts: 2
Joined: Thu Jan 19, 2006 9:51 pm

Postby blackd on Thu Jan 26, 2006 3:22 pm

In java the code will look something like this

Code: Select all
char toch(int w) {
   return w > 9 ? w + 55 : w + 48
}

int toi(char w) {
   return w > 64 ? w - 66 : w - 48
}

/* in java you don't need that
int leng(char* str) {
  int x;
  for (x = 0; str[x] != 0; x++) {}
  return x;
}
*/

String revstr(String str) {
   char[] res = str.toCharArray();
   char tmp = 0;
   for (int ong = 0; ong < res.length / 2; ong++) {
      tmp = str[ong];
      res[ong] = res[l2 - ong];
      res[l2 - ong] = tmp;
      
   }
   return new String(res);
}
blackd
Newbie
Newbie
 
Posts: 1
Joined: Wed Jan 25, 2006 12:37 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron