Hexadecimal is very important and useful for computer programmers. You are requested to provide a hexadecimal view for given data. The hexadecimal view is made up of one or more rows. Every row except the last one represents 16 characters. Each row consists of three columns separated by a space:
* 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.
Input
There are multiple test cases. Each line is a test case. The line is made up of no less than 1 and no more than 4096 printable characters including spaces.
Output
For each test case, output its hexadecimal view. Do not output any extra spaces after the last character of text.
Sample Input
Hex Dump #include <cstdio> printf("Hello, World!\n"); main = do getLine >>= print . sum . map read . words
Sample Output
0000: 4865 7820 4475 6d70 hEX dUMP 0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE <CSTDIO 0010: 3e > 0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w 0010: 6f72 6c64 215c 6e22 293b ORLD!\N"); 0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN 0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU 0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W 0030: 6f72 6473 ORDS
题目大意:输入一组字符串,每行最多输出字符串中16个字符,每两个字符用一个空格隔开,因为所有的字符用两位16进制数就可以表示完了 ,根据样例输出可知一个字符用两位16进制数表示,
输出的每一行有三部分组成,
第一部分:16进制表示的每行中首个字符在字符串的位置,
第二部分:每一个字符用两位16进制数表示,每两个字符用空格隔开,每行不足16个字符的用空格补充,因为两位16进制表示一个字符,所以每缺少一个字符就用两个空格表示,
第三部分:如果字符是大写字母就输出小写,如果是小写字母就输出大写字母,其他原样输出
每一部分用一个空格隔开
解题思路:理解题意,就好做了,没有什么技巧,按要求输出即可
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
const int maxn=1e5;
using namespace std;
char s[maxn];
int main()
{
while(gets(s))
{
int len=strlen(s);
for(int i=0;i<len;i+=16)
{
printf("%04x: ",i);//输出16进制表示的地址
for(int j=i;j<i+16;j+=2)//输出16进制表示的字符
{
if(i!=j)
printf(" ");
if(j<len)
printf("%02x",s[j]);
else
printf(" ");
if(j+1<len)
printf("%02x",s[j+1]);
else
printf(" ");
}
printf(" ");
for(int j=i;j<i+16&&j<len;j++)//大小写的转换
{
if(islower(s[j]))
printf("%c",toupper(s[j]));
else
printf("%c",tolower(s[j]));
}
cout<<endl;
}
}
return 0;
}