滴水逆向联盟

标题: 分享一个自己写的二进制和十六进制随机生成源码 [打印本页]

作者: Bravo    时间: 2015-9-4 17:40
标题: 分享一个自己写的二进制和十六进制随机生成源码
本帖最后由 Bravo 于 2015-9-4 17:41 编辑

这个是自己写的,主要是为了锻炼自己二进制和十六进制互相对应转化的问题。我对自己的要求是,一定要在下一个数字出来之前,把上一个数字的二进制或者十六进制写出来。共勉。

  1. // RandomBinary.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include <time.h>


  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9.         int nBinary[4] = { 0 };
  10.         int nCount = 0;
  11.         int nBinaryIndex = 0;
  12.         int nTest = 0xF;
  13.         int nIsOutputBinary = '0';
  14.         int nTime = 0;
  15.         

  16.         scanf_s("%d",&nIsOutputBinary, 1);  //输入1,生成二进制和十六进制,输入0,只生成16进制

  17.         switch (nIsOutputBinary)
  18.         {
  19.         case 1:
  20.         {
  21.                 nCount = 0;
  22.                 while (nCount < 10)
  23.                 {
  24.                         srand((unsigned)time(NULL));
  25.                         
  26.                         for (nBinaryIndex = 0; nBinaryIndex < 4; nBinaryIndex++)
  27.                         {
  28.                                 Sleep(1000);
  29.                                 
  30.                                 nBinary[nBinaryIndex] = rand() % 2;
  31.                         }

  32.                         printf("%d%d%d%d\r\n", nBinary[0], nBinary[1], nBinary[2], nBinary[3]);
  33.                         nCount++;

  34.                 }
  35.         }
  36.         case 0:
  37.         {
  38.                 nCount = 0;
  39.                 while (nCount < 10)
  40.                 {
  41.                         srand((unsigned)time(NULL));
  42.                         printf("%x\r\n", rand()%16);
  43.                         nCount++;
  44.                         Sleep(1000);

  45.                 }
  46.         }

  47.         default:
  48.         {

  49.         }

  50.         }
  51.         
  52.         system("pause");
  53.         
  54.         return 0;
  55. }

复制代码

作者: Bravo    时间: 2015-9-5 14:57
本帖最后由 Bravo 于 2015-9-13 22:22 编辑

之前那个程序写的不好,更新一下。
  1. // RandomBinary.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include <time.h>
  7. void print_2(int val2);


  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10.         int nCount = 0;
  11.         srand((unsigned)time(NULL));
  12.         int nHex[11] = { 0 };
  13.        
  14.         int nIsOutPutBinary = 0;
  15.         printf("Please make your own choice to decide which exercises we should start.\r\n");
  16.         printf("Press 1 to make binary convert hexadecimal exercise.\r\n");
  17.         printf("Press 2 to make hexadecimal convert binary exercise.\r\n");
  18.         printf("Now, please make your choice:");

  19.         scanf_s("%d", &nIsOutPutBinary);

  20.         switch (nIsOutPutBinary)
  21.         {
  22.         case 1:
  23.         {
  24.                 while (nCount < 10)
  25.                 {
  26.                         nHex[nCount] = rand() % 0xFF;
  27.                         printf("%x\r\n", nHex[nCount]);
  28.                         nCount++;
  29.                         Sleep(1000);
  30.                 }

  31.                 printf("Please correct your answer.\r\n");
  32.                 system("pause");
  33.                 nCount = 0;
  34.                 while (nCount < 10)
  35.                 {
  36.                         print_2(nHex[nCount]);
  37.                         nCount++;

  38.                 }
  39.                 system("pause");
  40.                 break;

  41.         }
  42.         case 2:
  43.         {
  44.                 while (nCount < 10)
  45.                 {
  46.                         nHex[nCount] = rand() % 0xFF;
  47.                         print_2(nHex[nCount]);
  48.                         nCount++;
  49.                         Sleep(1000);
  50.                 }

  51.                 printf("Please correct your answer.\r\n");
  52.                 system("pause");
  53.                 nCount = 0;
  54.                 while (nCount < 10)
  55.                 {
  56.                         printf("%x\r\n", nHex[nCount]);
  57.                         nCount++;

  58.                 }
  59.                 system("pause");
  60.                 break;

  61.         }
  62.                
  63.         default:
  64.                 break;
  65.         }

  66. }


  67.        
  68. void print_2(int val2)
  69. {
  70.         unsigned char *p = (unsigned char*)&val2 + 3; //从低位到高位,低端字节计算机
  71.         for (int k = 0; k <= 3; k++)
  72.         {
  73.                 int val2 = *(p - k);
  74.                 for (int i = 7; i >= 0; i--)
  75.                 {
  76.                         if (val2 & (1 << i))
  77.                                 printf("1");
  78.                         else
  79.                                 printf("0");
  80.                 }
  81.                 printf(" ");
  82.         }
  83.         printf("\r\n");
  84. }
复制代码








欢迎光临 滴水逆向联盟 (http://www.dtdebug.com/) Powered by Discuz! X3.2