Conversion between byte array and string (character array), byte array
1. Conversion by default:
(1) convert string (char []) to byte []
byte[] byteArr = System.Text.Encoding.Default.GetBytes(char[]);byte[] byteArr = System.Text.Encoding.Default.GetBytes(string);byte[] byteArr = System.Text.Encoding.Default.GetBytes(char[], startindex, count);int count = System.Text.Encoding.Default.GetBytes(char * , count, byte*, startindex);int count = System.Text.Encoding.Default.GetBytes(char[] , startindex, count, byte[], startindex);int count = System.Text.Encoding.Default.GetBytes(s , startindex, count, byte[], startindex);
(2) byte [] to string (char [])
string str = System.Text.Encoding.Default.GetString ( byteArray );string str = System.Text.Encoding.Default.GetString ( byteArray, startindex, count );
The GetString method is similar to the GetChar method. The latter converts the byte array to the corresponding character array.
2. Other encoding methods
Replace the preceding Default with the ASCII, Unicode, UTF8, UTF7, UTF32, and Other encoding methods, and then call the corresponding method to convert the Default value.
string str = "01";byte[] byteArr = System.Text.Encoding.ASCI.GetBytes(str);
The above code gets byteArr = {0x30, 0x31}, which is the ASCI encoding value of 0 and 1.
byte[] byteArr = new byte[]{0x4e, 0x01};string a = System.Text.Encoding.BigEndianUnicode.GetString(byteArr);
The code above assigns a value to variable a for decoding the second Chinese character "ding" of unicode encoding, and the unicode code of "ding" is \ u4e01.
3. Convert Byte [] to a hexadecimal string
public static string ToHexString ( byte[] bytes ) { string hexString = string.Empty; if ( bytes != null ) { StringBuilder strB = new StringBuilder (); for ( int i = 0; i < bytes.Length; i++ ) { strB.Append ( bytes[i].ToString ( "X2" ) ); } hexString = strB.ToString (); } return hexString; }
4. convert a hexadecimal string to a Byte array
public static byte[] GetBytes(string hexString, out int discarded) { discarded = 0; string newString = ""; char c; // remove all none A-F, 0-9, characters for (int i=0; iFor the obtained webpage or other files that use unicode encoding, you can use the preceding method to convert the encrypted unicode encoding string to a byte array, then use the preceding conversion method to convert to the corresponding character.
In java, how does one convert the byte array content to a string?
Public class ByteTest {
Public static void main (String [] args ){
String str = "Hello world! ";
// Convert string to byte
Byte [] bs = str. getBytes ();
System. out. println (Arrays. toString (bs ));
// Convert byte to string
String str2 = new String (bs );
System. out. println (str2 );
}
}
Is there a conversion from a string to a byte array in labview? Is there a conversion from a string to a character (16-bit) array ?, I want him to collect a 16-bit data.
Three digits. See the figure. The index box is hidden in the array constant. The string and numeric values are displayed in hexadecimal format for easy viewing. Use big-endian or little-endian to restore functions from strings.