encoding and deconding utf8 string and its bytes in java -


I am working on a project for which I have to encode a string code in java and encode it. My string is a UTF-8 string that meets the Persian character. I just want to x-ray each byte with a constant character and then want to re-write it with the same stable character

I wrote the code below, but it works completely wrong! ! I see it with an English character and it works

How can I fix this problem, thanks in advance!

  string str = "c"; Four keys = 'n'; Byte BK = (byte) key; Byte [] B = str.getBytes (); For (int i = 0; i & lt; B. laugh; i ++) {b [i] = byte.valouf (byte) (b [ii] ^ bkA); } String str1 = new string (b); B = str1.getBytes (); For (int i = 0; i  

The problem is coming when you make str1 with mut1 bytes. When you enter string str1 = new string (b); If there are some bytes in UTF 8 encoding, then please make a good string for me. But because you've exchanged the bytes, the encoding is invalid UTF 8, and Java does not know much about it. If you see the byte which is the b = str1.getBytes (); With getting derived from str1, you will see that they are different from those ones that you have created a string!

Actually you should not make a string with "nonsense" bytes --- do you really need to store the exterior bytes in the string?

If you really want to do this, you can trick the system using a single byte encoding where all the possible byte values ​​are valid. So you can make sure that whatever you put in the bytes string will become the same thing you get. This is an example that is working for me:

  public class B {static public void main} (string [] args) exception {string str = "c"; Println (STR); Four keys = 'n'; Byte BK = (byte) key; Byte [] B = Strobebats ("UTF 8"); System.out.println ("From the original bytes str:"); (Int i = 0; i & lt; b.length; i ++) {System.out.println (b [i]); } System.out.println ("Bytes are used to make the string:"); For (int i = 0; i & lt; B. laugh; i ++) {b [i] = byte.valouf (byte) (b [ii] ^ bkA); Println (b [i]); } String str1 = new string (b, "cp 1256"); B = str1.getBytes ("Cp1256"); System.out.println ("Bytes obtained from the straight:"); (Int i = 0; i & lt; b.length; i ++) {System.out.println (b [i]); B [ii] = (byte) (b [ii] ^ bk); } System.out.println ("Bytes to create straddles": "); for (int i = 0; i  

The output I get is:

 < Code> s by the original byte: -61 -65 -30 -119-9 1 bytes for making strat: -115 -15 -84 -57 -21 recover from the bytes straight: -115 -15 -84 -57 -21 Used to make the bytes strobe: -61 -65 -30 -119-9 1C  

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -