In C #, uint-byte []-Char []-string is converted to each other.

Source: Internet
Author: User

Uint ----- byte [] ----- char [] ----- string

Some types of mutual conversion are often required for some interoperability. For example, when using C # To access WIN32API, you often need to input the DWORD parameter to the API: the data represented by the uint value may need to be displayed as characters in actual application, but the relationship between C # APIs cannot be compared with that of C ++, therefore, it is necessary to convert some data types in C,
Some simple conversion operations are pasted below to facilitate memory

Uint ---> byte []

Byte [] bpara = system. bitconverter. getbytes (uint upara );

Byte [] ---> uint

Uint upara = system. bitconverter. touint32 (bpara );

Byte ---> char

System. Convert. tochar (bpara );

Char ---> byte

System. Convert. tobyte (cpara );

Byte [] ---> char []

(1) Char [] cpara = system. Text. encoding. Default. getchars (bpara); (1)

(2) Char [] cpara = new char [bpara. Length];

For (INT I = 0; I <bpara. length; I ++) {char [I] = system. Convert. tochar (bpara [I]);}

(3) Char [] cpara = new asciiencoding (). getchars (bpara );
 

Char [] ---> byte []

(1) byte [] bpara = system. Text. encoding. Default. getbytes (cpara );

(2) byte [] bpara = new asciiencoding (). getbytes (cpara );

Char [] ---> string

String spara = new string (cpara );

String --- char []

Char [] cpara = spara. tochararray ();

Uint ----> char []

(1) uint --> byte [];

(2) byte [] --> char [];

Uint ---> string

(1) uint --> byte [];

(2) byte [] --> char [];

(3) Char [] --> string;

Byte [] ---> string

(1). byte [] --> char [];

(2). Char [] --> string;

(3) New asciiencoding (). getstring (bprar );

Char [] ---> uint

(1). Char [] --> byte [];

(2). byte [] --> uint;

String ---> byte []

Bpara = system. Text. encoding. Default. getbytes (spara );

String ---> uint

(1) string --> byte [];

(2) byte [] --> uint;

Note that when interacting with APIs using uint characters, you must pay attention to the character sequence, which involves the issue of API high/low data, that is, the data obtained from dowrd In the API is usually in reverse order in the C # representation, therefore, when obtaining or passing strings in C #, you must note that the string can be converted to a uint for API use only after reverse processing.

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.