Hexadecimal View
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1837 Accepted Submission(s): 753
* addr: the 4-digit hexadecimal beginning address of this row.
* dump: the hexadecimal representation of this row, separating every two characters by a whitespace. If there are less than 16 characters in the last row, pad it with spaces.
* text: the ASCII translation of this row, with uppercase characters converted to lowercase and lowercase characters converted to uppercase.
Use lowercase for the letter digits. See sample for more details.
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
char input[4100];
int main()
{
int i,j,len,k;
while(gets(input) != NULL)
{
len = strlen(input);
for(i = 0;i < len;i+=16)//最外層循環,每次使標記前進16個字符----因爲每輸出16個字符爲一個循環
{
printf("%04x:",i);//i永遠是16的整數倍,可以確保以十六進制輸出時最後一位爲0————舉個例子就想清了——%04X加0是要0補齊前面的位置
for(j = i;j < i + 16;j++)//輸出字符
{
if(j % 2 == 0) printf(" ");//數字之間的空格
if(j < len)printf("%02x",input[j]);
else printf(" "); //j < len,說明輸出還未滿16個字符就沒有字符了,那就用空格代替
}
printf(" ");//最後追加一個空格
for(k = i;k < i + 16 && k < len;k++)//對字符的大小寫處理
{
if(isalpha(input[k]) == 2) printf("%c",input[k] - 'a' + 'A');
else if(isalpha(input[k]) == 1) printf("%c",input[k] - 'A' + 'a');
else printf("%c",input[k]);
}
printf("\n");
}
}
return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char input[4100],temp[4100];
int line;
int main()
{
int i,j,len,k,m;
while(gets(input) != NULL)
{
len = strlen(input);
for(i = 0;i < len;i++)
{
if(isalpha(input[i]) == 1) temp[i] = input[i] + 32;
else if(isalpha(input[i]) == 2) temp[i] = input[i] - 32;
else temp[i] = input[i];
}
printf("0000:");
line = 0;
k = 5;
for(i = 0;i < len;i++)
{
if(i % 16 == 0 && i)
{
printf(" ");
for(j = i - 16; j < i;j++)
printf("%c",temp[j]);
line++;
printf("\n%03x0:",line);
k = 5;
}
if(i % 2 == 0) {printf(" ");k++;}
printf("%02x",input[i]);k+=2;
}
if(len % 16 != 0)
{
printf(" ");k++;
for(m = 0;m < 45 - k + 1;m++)
printf(" ");
for(j = len - len % 16;j < len;j++)
printf("%c",temp[j]);
}
printf("\n");
memset(input,'\0',sizeof(input));
memset(temp,'\0',sizeof(temp));
}
return 0 ;
}