Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #pragma warning(disable:4996)
- #define MAX_LENGTH 100
- typedef unsigned int integer_t;
- char toHex(integer_t num) {
- char str[2][4];
- int u = 0;
- do {
- int mod = num % 16;
- num = num / 16;
- if (mod < 10) {
- str[1][u] = mod;
- }
- else {
- str[1][u] = 'a' + mod - 10;
- }
- mod = num % 16;
- num = num / 16;
- if (mod < 10) {
- str[0][u] = mod;
- }
- else {
- str[0][u] = 'a' + mod - 10;
- }
- u++;
- } while (u<=3);
- return str;
- }
- char toBinary(char num[], int q) {
- char binstr[65] = {""};
- int n;
- for (int i = 0;i < Length2(num);i++) {
- if (num[i] < 10) {
- n = num[i];
- }
- else {
- n = num[i] - 'a' + 10;
- }
- for (int j = 1; j < 4; j++) {
- int mod = num[i] % 2;
- num[i] = num[i] / 2;
- char d = '0' + mod;
- char *strcat(char *binstr, char *d);
- }
- }
- return binstr;
- }
- int Length(integer_t num)
- {
- int i = 0;
- while (num>0){
- i++;
- num = num / 256;
- }
- return i;
- }
- int Length2(char c[])
- {
- int i = 0;
- while (c[i] != 0)
- i++;
- return i;
- }
- int main(void) {
- integer_t num = 0;
- do {
- printf("Enter your chislo:\n");
- scanf("%ui", &num);
- // char str[3][6] = { {'1','2','3','4','5','6'}, {'1','2','3','4','5','6'}, {'1','2','3','4','5','6'} };
- // char str2[3][6] = { {'1','2','3','4','5','6'}, {'1','2','3','4','5','6'}, {'1','2','3','4','5','3'} };
- char str[3][6] = { "" };
- char str2[3][6] = { "" };
- //to 16
- str[3][6] = toHex(num);
- int q = Length(num);
- //rev
- for (int i = q; i != 0; i--) {
- str2[0][i - 1] = str2[0][q - i];
- str2[1][i - 1] = str2[1][q - i];
- }
- char strhex1[] = { "0x" };
- char strhex2[] = { "0x" };
- //tostring
- for (int i = q; i != 0; i--) {
- char d = str[1][i - 1];
- char *strcat(char *strhex2, char *d);
- char r = str[0][i - 1];
- char *strcat(char *strhex2, char *r);
- }
- for (int i = q; i != 0; i--) {
- char t = str2[1][i - 1];
- char *strcat(char *strhex1, char *t);
- char y = str2[0][i - 1];
- char *strcat(char *strhex1, char *y);
- }
- //to 2
- char s[33], s2[33];
- s[33] = toBinary(strhex1,q);
- s2[33] = toBinary(strhex2,q);
- int n = Length2(s);
- integer_t sum = 0;
- for (int i = n - 1;i > -1;i--)
- if (s[i] == '1')
- sum += pow(2, i);
- //output
- printf("%i = %s = %s \n", num, strhex1, s2);
- printf("%i = %s = %s \n", sum, strhex2, s);
- // printf("The words in your string are reversed, but the separators are located in their place:\n");
- // printf("%s\n", str);
- } while (num != '0');
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement