Array, byte array, conversion, etc.

Source: Internet
Author: User

There are many classes related to Array Operations in the system namespace. The system. array class provides the following common methods:
Binarysearch: Binary SearchAlgorithmSearch for values in a one-dimensional sorting array.
Copy: Copy part of an array to another array, and perform type-based forced conversion and packing as needed.
Copyto: Copy all elements of the current one-dimensional array to the specified one-dimensional array.
Resize: changes the array size to the specified size.
Sort: sorts elements in one-dimensional array objects.
Unlike most classes, array provides the createinstance method to allow later access binding, rather than providing public constructors. The array. COPY method not only copies elements between arrays of the same type, but also between standard arrays of different types. It automatically performs forced type conversion. Some methods, such as createinstance, copy, copyto, getvalue, and setvalue, provide overload (taking 64-bit integers as parameters) to adapt to large-capacity arrays. Longlength and getlonglength return the 64-bit integer indicating the length of the array. Before you perform an operation (such as binarysearch) that requires sorting the array, you must sort the array.
Arraylist is different from array. The former is a collection object. The toarray method of arraylist can directly export all the elements in arraylist to an array without having to copy the elements one by one in a loop.
Toarray is used as follows:

Arraylist ay = new arraylist ();
Ay. Add ("Sheep ");
Ay. Add ("cat ");
Ay. Add ("dog ");
String [] Al = (string []) Ay. toarray (typeof (string ));
Console. writeline (Al [0]);

the key lies in the toarray parameter. Here, we should use typeof in reflection to obtain the original data type of the elements in arraylist.
in an array, there is a special byte array, that is, byte []. Data in memory and files are stored in byte arrays. If the Program needs to operate on the data, more or less bytes [] are used.
for the conversion between byte [] and other types, you can use the memorycopy function in C ++. Although there are functions similar to memorycopy in C: buffer. blockcopy, but because of its strong type, it cannot implement the function of converting byte arrays and other types in C.
to solve this problem, you need to manually write other types of data to obtain the byte array through bitwise AND logical operations. The following Code :

// Convert an integer to a byte array
Int I = 1234567; // The hexadecimal format is: 0012d687

Byte [] DB = new byte [4]; // Int Is a 4-byte (32-bit) Integer
DB [0] = (byte) (I & 0x000000ff); // obtain the 1st and 2nd digits
DB [1] = (byte) (I & 0x0000ff00)> 8); // take the 3 and 4 digits and move the 2 digits to the right
DB [2] = (byte) (I & 0x00ff0000)> 16); // take the 5 th and 6 digits and move the 4 digits to the right
DB [3] = (byte) (I & 0xff000000)> 24); // take the 7 th and 8 digits and move the 6 digits to the right
Console. writeline ("use and bit operation and shift operation to convert to array: {0}, {1}, {2}, {3}", DB [0], DB [1], DB [2], DB [3]);

// Convert the byte array to an integer type
Int J = 0;
J = (INT) DB [0]; // restores 1st bytes (low)
J + = (INT) DB [1] <8; // restore 2nd bytes
J + = (INT) DB [2] <16; // restore 3rd bytes
J + = (INT) DB [3] <24; // restores 4th bytes (high)
Console. writeline ("use shift operation to restore to integer: {0}", J );

Another method is to use marshal. The marshal class includes most of the memory operations, such as space allocation, pointers, memory replication, and memory read/write. The following code uses marshal to implement the same functions:

int I = 1234567; // the corresponding hexadecimal format is: 0012d687
// use marshal to convert to an array
byte [] newdb = new byte [4];
intptr P = marshal. allochglobal (4); // allocates memory space and returns the space pointer
marshal. writeint32 (P, I); // write the int value to the space
marshal. copy (p, newdb, 0, 4); // copy the content of the memory space to the byte array newdb. Meanings of parameters source, destination, startindex, length
console. writeline ("using marshal to convert integers into Arrays: {0}, {1}, {2}, {3}", newdb [0], newdb [1], newdb [2], newdb [3]);
// use marshal to convert to an integer
P = marshal. allochglobal (4); // allocates memory space and returns the space pointer
marshal. copy (newdb, 0, P, 4); // copy the database to the memory. Meaning of each parameter source, startindex, destination, length
int K = marshal. readint32 (p); // read the memory to the variable k
console. writeline ("using marshal to restore an integer {0}", k);

You can also use the system. bitconverter class to make it easier. Below are some common methods:
Getbytes () returns values of other data types in the form of byte arrays.
Toboolean returns the Boolean value converted from a byte at the specified position in the byte array.
Tochar returns the Unicode characters converted from two bytes at the specified position in the byte array.
Todouble returns the double-precision floating point number converted from eight bytes at the specified position in the byte array.
Toint16 returns a 16-bit signed integer converted from two bytes at the specified position in the byte array.
Toint32 returns a 32-bit signed integer converted from four bytes at the specified position in the byte array.
Toint64 returns a 64-bit signed integer converted from eight bytes at the specified position in the byte array.
Tosingle returns the single-precision floating point number converted from four bytes at the specified position in the byte array.
The tostring has been overloaded. Returns the string converted from the element of the byte array.

**************************************** **************************************** ***************

// Integer to byte array Conversion
Public static byte [] inttobyte (INT number ){
Int temp = number;
Byte [] B = new byte [4];
For (INT I = B. Length-1; I>-1; I --){
B = new INTEGER (temp & 0xff). bytevalue (); // Save the highest bit in the second bit
Temp = temp> 8; // shift 8 digits to the right
}
Return B;
}
// Convert byte arrays to Integers
Public static int bytetoint (byte [] B ){
Int S = 0;
For (INT I = 0; I <3; I ++ ){
If (B> = 0)
S = S + B;
Else
S = S + 256 + B;
S = S * 256;
}
If (B [3]> = 0) // The last one is not multiplied because it may overflow.
S = S + B [3];
Else
S = S + 256 + B [3];
Return S;
}
// Character to byte Conversion
Public static byte [] chartobyte (char ch ){
Int temp = (INT) ch;
Byte [] B = new byte [2];
For (INT I = B. Length-1; I>-1; I --){
B = new INTEGER (temp & 0xff). bytevalue (); // Save the highest bit in the second bit
Temp = temp> 8; // shift 8 digits to the right
}
Return B;
}
// Byte to character conversion
Public static char bytetochar (byte [] B ){
Int S = 0;
If (B [0]> 0)
S + = B [0];
Else
S + = 256 + B [0];
S * = 256;
If (B [1]> 0)
S + = B [1];
Else
S + = 256 + B [1];
Char CH = (char) S;
Return ch;
}
// Float-byte Conversion
Public static byte [] doubletobyte (double D ){
Byte [] B = new byte [8];
Long L = double. doubletolongbits (d );
For (INT I = 0; I <B. length; I ++ ){
B = new long (L). bytevalue ();
L = L> 8;
}
Return B;
}
// Byte to floating point conversion
Public static double bytetodouble (byte [] B ){
Long L;
L = B [0];
L & = 0xff;
L | = (long) B [1] <8 );
L & = 0 xFFFF;
L | = (long) B [2] <16 );
L & = 0 xffffff;
L | = (long) B [3] <24 );
L & = 0 xffffffffl;
L | = (long) B [4] <32 );
L & = 0 xffffffffl;
L | = (long) B [5] <40 );
L & = 0 xffffffffffl;
L | = (long) B [6] <48 );
L & = 0 xffffffffffffl;
L | = (long) B [7] <56 );
Return double. longbitstodouble (L );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.