Byte trouble!
Hello I am well and truly stuck!
Well basically im making a program that converts whatever you type in to a byte value aka string to byte conversion, that bit works fine! But....
Theres another screen where you enter the byte value in a JTextField, and i need to be able to convert it back to a String, but the thing is that it inputs it into a string straight from the JTextField, it wont go straight into a byte variable from the JTextField, I cannot use parseInt or toString or getBytes because those convert the value they dont just put it in, for example
Entered text: "I Love Java"
Byte Value taken "[B@859a68"
//Turning back into a String
Entered text "[B@859a68"
getBytes "[B@1efb4be"
Converted to String should be "I Love Java"
But is "B@1efb4be"
So it leads me to my question - How can i put the byte value from a String straight into a byte variable with NO CONVERTING!
Like This:-
String = "[B@859a68"
exact same value put in byte with no conversion
Byte = "[B@859a68"
String's getBytes method works for me.
> Entered text: "I Love Java"
> Byte Value taken "[B@859a68"
>
> //Turning back into a String
>
> Entered text "[B@859a68"
> getBytes "[B@1efb4be"
[B@859a68 is not a byte. It is the name of this specific instance of a byte array. You won't be able to store that full String into a single byte, so you have an array of bytes. Print each byte individually instead of printing the array all at once.
I have a feeling the implementation of getBytes() might need corrections too - as opposed to just how you are printing it's return value - due to the misunderstanding of the data representation.
> String's getBytes method works for me.Clarification to my other post. I replied thinking maybe you implemented your own method, also called getBytes().
No one has understood me correctly, its so frustrating for me i wish i could talk to you in person
it is in an byte array! Ill show you my code
String text = CryptBox.getText();
// String text will be inputted by user as a byte value!!!!!!!!!!!!!!!!!!!!!!!
String password = PasswordBox.getText();
int password_int = Integer.parseInt(password);
byte [] decrypted_text = text.getBytes();
String decrypted_str = new String(decrypted_text);
CryptBox.setText(" decrypted text is " + decrypted_str );
Surely decrypted_str would be "I Love Java" but its not could someone show me how to turn String text into a byte value and then converted back into a string to reveal the message " i love java"
Message was edited by:
Beansontoast
We're frustrated too, and ironically, the way to end frustration is
to post a short (< 1 page) example program that we can copy and run.
Here's mine, and it works. I don't see your problem.
public class BytesExample {
public static void main(String[] args) {
String text = "I love Java";
byte [] decryptedText = text.getBytes();
String decryptedStr = new String(decryptedText);
System.out.println(decryptedStr);
System.out.println("equal strings: " + text.equals(decryptedStr));
}
}
OK sorry ive really messed up here, i was wrong!I was trying to make a little encryption program by converting strings to bytes and back, but ive realised if u convert a string to a byte it will be different each time
> If u convert a string to a byte it will be different each timeWTF ?"I Love Java" will convert to the same sequence of bytes, every time.
well my program doesnt do that....its wierd it comes up with different ones each time for "I Love Java" with exactly the code written above put into my program-for example :-[B@1578aab[B@9ff0a8[B@f892a4Im so confused
How to be less confused: one again, post a short (<1 page) exampleprogram that forum members can copy and run. Do you notice the pattern?You keep claiming mystery code doesn't work, and the rest of the forum says:?
> [B@1578aab
The above looks like what you'd get if you tried to "print" an array of bytes. It's just displaying the default toString() result of an array, not the contents of the array. It won't magically print each byte in the array, unless you explicitly write code to do that.
Give an example of "text"'s value.
You say it would be the byte value of "I Love Java". I don't really know what you expect that value to be.
I'm going to assume you mean that you cast each character from a (char) to an (int), and then keep the first 8 bits. In this case I would expect to see the following (from http://www.electronic-engineering.ch/microchip/datasheets/ASCII-Map.pdf):
'I' - 73 - 01001001
' ' - 32 - 00100000
'L' - 76 - 01001100
'o' - 111 - 01101111
'v' - 118 - 01110110
'e' - 101 - 01100101
' ' - 32 - 00100000
'J' - 74 - 01001010
'a' - 97 - 01100001
'v' - 118 - 01110110
'a' - 97 - 01100001
What is the exact text entered into the CryptBox? Is it going to be a String of the binary, hex, int, or character representation?
These are all questions you've left un-answered, which is the cause of the confusion.Supposing you are going to enter it in binary (if hex, it's the same thing), then your code of:String text = CryptBox.getText();
byte [] decrypted_text = text.getBytes();
String decrypted_str = new String(decrypted_text);
CryptBox.setText(" decrypted text is " + decrypted_str );
should be replaced with
String text = "0100100100100000010011000110111101110110011001010010000001001010011000010111011001100001
";
// text holds the value entered into a text box, which is the byte representation of "I Love Java"
byte [] decrypted_text = new byte[text.size()/8];
for (int i = 0; i < decrypted_text.length; i += 8) {
decrypted_text[i] = setByteFromText(text.substring(i,i+8);
}
String decrypted_str = new String(decrypted_text);
CryptBox.setText(" decrypted text is " + decrypted_str );
Warning: I didn't try compiling my code.
Message was edited by:
bheilers
