Re: String of numbers into to array of numbers
On Sep 23, 12:53 pm, Mark Space <marksp...@sbcglobal.net> wrote:
Mark Space wrote:
public class Base64 {
public static String encode( String s ) {
}
public static String decode( String s ) {
}
Just thinking later, this is probably wrong. The correct parameter type
would be byte[] as you had it. I just got confused with the example on
Wikipedia.
public static String encode( byte[] ){}
public static byte[] decode( String s ) {}
You can still use the example at Wikipedia to check your program.
String test = "Man is distinguished, not only by his reason";
String enc = encode( test.getBytes() );
This might make testing a little easier.
Hi All,
Here are 2 attempts to get it correct.
bH
First a String to and from Char
// no import java needed
public class StringToAndFromChar {
// a line of Text only please
private String oneLine = "There 365 days this year in 2008.";
private int[] aArrayofNum = new int [oneLine.length()];
private int shoInt = 0;
//main
public static void main(String[] args) {
StringToAndFromChar StringToAndFromChar1 = new
StringToAndFromChar();
}
public StringToAndFromChar(){
System.out.println("Show the word(s)");
System.out.println( oneLine);
int t = 0;
for (int i = 0; i < oneLine.length (); i++) {
System.out.println("The Letter is: " + oneLine.charAt(i));
shoInt = (int)oneLine.charAt(i);
System.out.println("it's char number is: "+ shoInt);
//compare individual shoInt to 0 to 256 and save it to
array
for (int ia = 0; ia <256; ia++) {
if (ia==(shoInt)){
System.out.println("Single Letter Reconstituted:
");
aArrayofNum[t]=ia;
t++;
System.out.println((char)
(Integer.parseInt(String.valueOf(ia))));
}
}
}
System.out.println("The Single Line Reconstituted: ");
for (int ic = 0; ic <oneLine.length (); ic++) {
System.out.print((char)
(Integer.parseInt(String.valueOf(aArrayofNum[ic]))));
} System.out.print("\n");
}
}
Second using Base64.
bH
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.UnsupportedEncodingException;
// The Base64 is part of the download from Roedy's website
// and is not included here
public class EnterStrGetCharNum extends JFrame
implements ActionListener {
private JPanel infoPanel = new JPanel();
private static JLabel lbel1= new JLabel(" Text Converted"
+" To and From Base64" );
private static JLabel lbel2 = new JLabel(" Enter Your"
+ " One Line: ");
private static JLabel lbel3 = new JLabel(" Please... No "
+"Special Symbols " );
private static JLabel lbel4 = new JLabel(" Base64 Hex: ");
private static JLabel lbel5 = new JLabel(" Base64 "
+"Encoded: ");
private static JLabel lbel6 = new JLabel(" Base64 "
+"Reconstituted Bytes: ");
private static JLabel lbel7 = new JLabel(" Reconstituted "
+"String ");
// When it is running enter your own text
private JTextField txtFld1 = new JTextField(" Put In Your"
+" One Line Of Text ",20);
private JTextField txtFld2 = new JTextField(20);
private JTextField txtFld3 = new JTextField(20);
private JTextField txtFld4 = new JTextField(20);
private JTextField txtFld5 = new JTextField(20);
private String collectBytesOriginal="";
private String collectStringOriginal= "";
private String reconstitutedString = "";
private String reconstitutedHex = "";
private JButton b1 = new JButton("String to and "
+" from Base64");
private JButton b2 = new JButton(" Close
");
Base64 base64 = new Base64();
public EnterStrGetCharNum() {
infoPanel.add(lbel1);
infoPanel.add(lbel2);
infoPanel.add(lbel3);
infoPanel.add(txtFld1);
infoPanel.add(b1);
b1.addActionListener(this);
infoPanel.add(lbel4);
infoPanel.add(txtFld2);
infoPanel.add(lbel5);
infoPanel.add(txtFld3);
infoPanel.add(lbel6);
infoPanel.add(txtFld4);
infoPanel.add(lbel7);
infoPanel.add(txtFld5);
infoPanel.add(b2);
b2.addActionListener(this);
Container contentPane= getContentPane();
contentPane.setLayout( new GridLayout(0,1) );
setSize(270,430);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.add(infoPanel);
this.setResizable(true);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1){
try{
String originalString = txtFld1.getText();
byte[] sendBytes = originalString.getBytes(
"8859_1"/* encoding */ );
base64.setLineLength( 72 );
String encoded = base64.encode( sendBytes );
byte[] reconstitutedBytes = base64.decode( encoded );
reconstitutedString =
new String( reconstitutedBytes, "8859_1"/* encoding */ );
/* display all intermediate results in encode decode process
*/
for ( int i = 0; i < sendBytes.length; i++ )
{
byte b = sendBytes[ i ];
collectBytesOriginal+= " " + Integer.toHexString( b ) ;
}
txtFld2.setText(collectBytesOriginal);
txtFld3.setText(encoded);
for ( int i = 0; i < reconstitutedBytes.length; i++ )
{
byte b = reconstitutedBytes[ i ];
reconstitutedHex += " "+ Integer.toHexString( b );
}
txtFld4.setText(reconstitutedHex);
txtFld5.setText(reconstitutedString);
}
catch (UnsupportedEncodingException ea )
{System.out.println( ea);}
}
if(e.getSource() == b2){
System.exit(0);
}
}
public static void main(String[] args)
throws java.io.UnsupportedEncodingException
{
EnterStrGetCharNum EnterStrGetCharNum1 =
new EnterStrGetCharNum();
}
}