滴水逆向联盟
标题:
分享一个自己写的二进制和十六进制随机生成源码
[打印本页]
作者:
Bravo
时间:
2015-9-4 17:40
标题:
分享一个自己写的二进制和十六进制随机生成源码
本帖最后由 Bravo 于 2015-9-4 17:41 编辑
这个是自己写的,主要是为了锻炼自己二进制和十六进制互相对应转化的问题。我对自己的要求是,一定要在下一个数字出来之前,把上一个数字的二进制或者十六进制写出来。共勉。
// RandomBinary.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <time.h>
int _tmain(int argc, _TCHAR* argv[])
{
int nBinary[4] = { 0 };
int nCount = 0;
int nBinaryIndex = 0;
int nTest = 0xF;
int nIsOutputBinary = '0';
int nTime = 0;
scanf_s("%d",&nIsOutputBinary, 1); //输入1,生成二进制和十六进制,输入0,只生成16进制
switch (nIsOutputBinary)
{
case 1:
{
nCount = 0;
while (nCount < 10)
{
srand((unsigned)time(NULL));
for (nBinaryIndex = 0; nBinaryIndex < 4; nBinaryIndex++)
{
Sleep(1000);
nBinary[nBinaryIndex] = rand() % 2;
}
printf("%d%d%d%d\r\n", nBinary[0], nBinary[1], nBinary[2], nBinary[3]);
nCount++;
}
}
case 0:
{
nCount = 0;
while (nCount < 10)
{
srand((unsigned)time(NULL));
printf("%x\r\n", rand()%16);
nCount++;
Sleep(1000);
}
}
default:
{
}
}
system("pause");
return 0;
}
复制代码
作者:
Bravo
时间:
2015-9-5 14:57
本帖最后由 Bravo 于 2015-9-13 22:22 编辑
之前那个程序写的不好,更新一下。
// RandomBinary.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <time.h>
void print_2(int val2);
int _tmain(int argc, _TCHAR* argv[])
{
int nCount = 0;
srand((unsigned)time(NULL));
int nHex[11] = { 0 };
int nIsOutPutBinary = 0;
printf("Please make your own choice to decide which exercises we should start.\r\n");
printf("Press 1 to make binary convert hexadecimal exercise.\r\n");
printf("Press 2 to make hexadecimal convert binary exercise.\r\n");
printf("Now, please make your choice:");
scanf_s("%d", &nIsOutPutBinary);
switch (nIsOutPutBinary)
{
case 1:
{
while (nCount < 10)
{
nHex[nCount] = rand() % 0xFF;
printf("%x\r\n", nHex[nCount]);
nCount++;
Sleep(1000);
}
printf("Please correct your answer.\r\n");
system("pause");
nCount = 0;
while (nCount < 10)
{
print_2(nHex[nCount]);
nCount++;
}
system("pause");
break;
}
case 2:
{
while (nCount < 10)
{
nHex[nCount] = rand() % 0xFF;
print_2(nHex[nCount]);
nCount++;
Sleep(1000);
}
printf("Please correct your answer.\r\n");
system("pause");
nCount = 0;
while (nCount < 10)
{
printf("%x\r\n", nHex[nCount]);
nCount++;
}
system("pause");
break;
}
default:
break;
}
}
void print_2(int val2)
{
unsigned char *p = (unsigned char*)&val2 + 3; //从低位到高位,低端字节计算机
for (int k = 0; k <= 3; k++)
{
int val2 = *(p - k);
for (int i = 7; i >= 0; i--)
{
if (val2 & (1 << i))
printf("1");
else
printf("0");
}
printf(" ");
}
printf("\r\n");
}
复制代码
欢迎光临 滴水逆向联盟 (http://www.dtdebug.com/)
Powered by Discuz! X3.2