Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert utf8 string to utf8 byte array?

How can I convert string to utf8 byte array, I have this sample code:

This works ok:

StreamWriter file = new StreamWriter(file1, false, Encoding.UTF8); file.WriteLine(utf8string); file.Close(); 

This works wrong, file is in ASCII:

byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(utf8string); FileStream fs = new FileStream(file2, FileMode.CreateNew); fs.Write(bytes, 0, bytes.Length); fs.Close(); 

I would like to get byte array what returned by this function:

System.IO.File.ReadAllBytes(path_to_file) 

because this works ok:

byte[] datab = File.ReadAllBytes(file1); FileStream fs2 = new FileStream(file3, FileMode.CreateNew); fs2.Write(datab, 0, datab.Length); fs2.Close(); 
like image 662
valch Avatar asked Jul 18 '12 10:07

valch


People also ask

How do you get a byte from a string?

Convert byte[] to String (text data) toString() to get the string from the bytes; The bytes. toString() only returns the address of the object in memory, NOT converting byte[] to a string ! The correct way to convert byte[] to string is new String(bytes, StandardCharsets. UTF_8) .

How do I encode an array of strings?

To encode string array values, use the numpy. char. encode() method in Python Numpy. The arr is the input array to be encoded.

How do I change the encoding of a string in Java?

Strings are immutable in Java, which means we cannot change a String character encoding. To achieve what we want, we need to copy the bytes of the String and then create a new one with the desired encoding.

What is a UTF-8 string?

UTF-8 is an encoding system for Unicode. It can translate any Unicode character to a matching unique binary string, and can also translate the binary string back to a Unicode character. This is the meaning of “UTF”, or “Unicode Transformation Format.”


1 Answers

Can use other option again:

string value = "\u00C4 \uD802\u0033 \u00AE";     byte[] bytes= System.Text.Encoding.UTF8.GetBytes(value); 

For more information can look on Encoding.UTF8 Property

like image 122
Tigran Avatar answered Sep 21 '22 20:09

Tigran