PDA

View Full Version : سوال: coder



anisaanisa
پنج شنبه 27 تیر 1387, 16:57 عصر
خانومها و آقایون دانشمند هم اکنون به یاری سبزتان نیازمندیم.اگه سر در آوردین به یه کم راهنمایی
نیاز است
ممنون

import javax.swing.JOptionPane;

class coder_decoder {
public static void main(String[] args) {
char[] arry;
char text4,text2,text5;
byte text3;

String text=JOptionPane.showInputDialog(null,"Enter a Text");
arry=text.toCharArray();

for(int i=0;i<arry.length;i++)
{
text2 = text.charAt(i); //har done az horof ro dar text2 mirize
text3 = (byte) text2; //harfe dakhele txt2 ro code ascii sho dar miare
text3 += 5; // code mikone horofo
text4 = (char) text3; //be character tabdil mikone
System.out.print(text4);

//-------------------now i want to decode-----------------------\\
//text3-=5;
}
//----System.out.print("\noreginal text is: "+text); because i cant find any solution

}
}

amirfarshad
پنج شنبه 27 تیر 1387, 17:11 عصر
مشکل چیه؟
بنظر من داره درست کار میکنه.
یک مقدار string میگیره و بعد به آرایه ای از کاراکترها تبدیل میکنه، سپس اون رو به byte تبدیل میشه و بعد 5 واحد بهش اضافه میشه، سپس مقدار جدید رو به کاراکتر تبدیل میکنه.
اونجا که به بایت تبدیل میشه، احتمالا میاد و کد اسکی کاراکتر رو میگیره.
این چیزی بود که من فهمیدم ازش. حالا مشکل چیه؟ میخواهی دوباره decode کنی؟ این راه بذهن من رسید:

class coder_decoder {
public static void main(String[] args) {
char[] arry;
char text4, text2, text5;
byte text3;
byte[] tmp;

String text = JOptionPane.showInputDialog(null, "Enter a Text");
arry = text.toCharArray();
System.out.println("original text:"+text);
tmp = new byte[arry.length];
System.out.print("code text:");
for (int i = 0; i < arry.length; i++) {
text2 = text.charAt(i); //har done az horof ro dar text2 mirize
text3 = (byte) text2; //harfe dakhele txt2 ro code ascii sho dar miare
text3 += 5; // code mikone horofo
tmp[i] = text3;
text4 = (char) text3; //be character tabdil mikone
System.out.print(text4);

//-------------------now i want to decode-----------------------\\
//text3-=5;

}
System.out.print("\ndecode text:");
for (int i = 0; i < tmp.length; i++) {
tmp[i] -= 5;
System.out.print((char) tmp[i]);
}
//----System.out.print("\noreginal text is: "+text); because i cant find any solution
}
}
اون خطهایی رو که bold شده به کد اضافه کردم.

amirfarshad
پنج شنبه 27 تیر 1387, 17:40 عصر
با اجازه کمی کد رو دستکاری کردم، فکر کنم این بهتر شد

import javax.swing.JOptionPane;

class coder_decoder {
public static void main(String[] args) {
char[] arry;
byte text3;
String code = "";
String decode = "";

String text = JOptionPane.showInputDialog(null, "Enter a Text");
arry = text.toCharArray();
System.out.println("original text:" + text);
for (int i = 0; i < arry.length; i++) {
text3 = (byte) text.charAt(i);
text3+=5;
code += (char) text3;
text3-=5;
decode+=(char)text3;
}
System.out.println("code text:" + code);
System.out.println("decode text:" + decode);
}
}