Study Notes: Convert hexadecimal data stored in Char to other data types

Source: Internet
Author: User

The most recent work involves reading The hexadecimal numbers stored in the char array. The code type (ASCII, BCD, float, Hex) of each char data is different. Convert the data type. The implementation is as follows:

String datatrans: char2bcd (unsigned char * C, int Len) {char s [100]; char C1, C2; string STR = ""; for (INT I = 0; I <Len; I ++) {C1 = C [I] & 0x0f; C2 = C [I]> 4; int n = (INT) (C2 & 0x08 + C2 & 0x04 + C2 & 0x02 + C2 & 0x01) <4) + (C1 & 0x08 + C1 & 0x04 + C1 & 0x02 + C1 & 0x01 )); if (n <= 9 & n> = 0) {STR + = "0" ;}str + = ITOA (N, S, 10) ;}return STR ;} void datatrans: char2byte (unsigned char C, unsigned char * s) {for (INT I = 7; I> = 0; I --) {s [I] = C & 0x01; C = C> 1 ;}} float datatrans: char2float (unsigned char * CP) {return * (float *) CP;} string datatrans: char2hex (char A1) {string STR = ""; // int n = (A1 <8) + A2; int n = A1; for (INT I = 2 * sizeof (char)-1; I> = 0; I --) {STR + = "0123456789 abcdef" [(n> I * 4) & 0xf)];} return STR;} int datatrans: hex2oct (unsigned char C1, unsigned char C2) {return (INT) (C1 <8) + (INT) c2 ;}

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.