Description: Given a string of numbers, BCD encoding, after the completion of the encoding in 8Bit to output the corresponding decimal value, if the given string is an odd digit, the encoding needs to be at the highest bit 0.
Simple BCD code: decimal Value 0~9, respectively, corresponding to 4bit binary encoding 0000~1001
Given the number string 1234, the corresponding BCD code is 0001,0010,0011,0100, the output decimal value in 8bit is: 18 52
Given the number string 123, the corresponding BCD code is 0000 0001 0010 0011 (the highest bit of 0), starting from the high level in 8bit output decimal value is: 135
Input: Only consider the case where the input string is a positive integer and the number of bits does not exceed 10 bits, the exception is not considered.
Output: No space interval is required for output.
Example input: 123
Sample output: 135
1#include <stdio.h>2#include <stdlib.h>3#include <iostream>4#include <math.h>5 Char* Bcdchange (Chara)6 {7 Switch(a)8 {9 Case '0':Ten return "0000"; One Break; A Case '1': - return "0001"; - Break; the Case '2': - return "0010"; - Break; - Case '3': + return "0011"; - Break; + Case '4': A return "0100"; at Break; - Case '5': - return "0101"; - Break; - Case '6': - return "0110"; in Break; - Case '7': to return "0111"; + Break; - Case '8': the return " +"; * Break; $ Case '9':Panax Notoginseng return "1001"; - Break; the } + } A intMain () the { + Charinput[Ten]=" /"; - Charoutput[Ten]=" /"; $ Chartmp[3]=" /"; $scanf"%s", input); - intLen =4*strlen (input); - Charbcd[ -] =" /"; the for(size_t i =0; I <strlen (input); i++) - {Wuyi Char*pbcd=Bcdchange (Input[i]); the strcat (BCD,PBCD); - } Wu if((Strlen (BCD))%8) - { About Charaddstr[ the] ="0000"; $ strcat (ADDSTR,BCD); -Memset (BCD,0, strlen (BCD)); - strcpy (BCD,ADDSTR); - } A intLEN_BCD =strlen (BCD); +printf"%s\n", BCD); the intA =0; - for(size_t i =0; I<strlen (BCD); i = i+8) $ { theA =0; thememset (TMP,0, strlen (TMP)); thesize_t j =0; the for(j =0; J <8; j + +) - { in if(bcd[i+j]=='1') the { theA = A + (int) Pow (2,7-j); About the } the } the_itoa (A, TMP,Ten); + strcat (output, TMP); - } theprintf"%s\n", output);Bayi GetChar (); the GetChar (); the return 0; -}
String Detection two--BCD transcoding