Char data stored in BCD (hexadecimal) code is represented in Int type.

Source: Internet
Author: User

The data in char data is stored in hexadecimal notation. The possible values of each char data are from 0x00 ~ 0xff (indicates that the binary range is from 00000000 ~ 11111111) hexadecimal number, that is, each char data can store 8-bit data. If the storage type is BCD, the data is stored according to the binary mechanism. The corresponding table is as follows:
[Cpp]
Int binary BCD code Hex
0 0000 0000 0
1 0001 0001 1
2 0010 0010 2
3 0011 0011 3
4 0100 0100 4
5 0101 0101 5
6 0110 0110 6
0111 0111 7
8 1000 1000 8
9 1001 1001 9
10 1010 1010
11 1011 1011 B
12 1100 1100 C
13 1101 1101 D
14 1110 1110 E
15 1111 1111 F

The code for implementing the conversion is as follows:
[Cpp]
# Include <iostream>
# Include <time. h>
Using namespace std;
 
String 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; // extracts four lower digits of the binary number
C2 = c [I]> 4; // extract the 4-digit high of the binary number
Int n = c2 * 10 + c1; // convert it to int (10 hexadecimal number)
// 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;
}
 
Int main (){
Char * wday [] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat "};
 
Time_t timep;
Struct tm * p;
Time (& timep );
P = localtime (& timep);/* obtain the local time */
Printf ("% d", (1900 + p-> tm_year), (1 + p-> tm_mon), p-> tm_mday );
 
Printf ("% s % d: % d \ n", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );
 
Int tt = (p-> tm_year-100 );
Unsigned char cc = (tt/10) & 0x0f) <4) + (tt % 10) & 0x0f );
Cout <"YY:" <Char2BCD (& cc, 1) <endl;
 
Cout <"INT:" <0x17 <endl; // 0x17 in hexadecimal notation is 23 in hexadecimal notation (that is, int type ).
Unsigned char c = 0x17;
Cout <"BCD:" <Char2BCD (& c, 1) <endl;
 
Unsigned char a [4] = {0x17,0x99,0x99 };
Cout <"ID" <Char2BCD (a, 3) <endl;
 
System ("pause ");
}

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.