HDU 4054 Hexadecimal View

來源:互聯網
上載者:User
Problem DescriptionHexadecimal 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.  


InputThere 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.  


OutputFor 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 dUMP0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE <CSTDIO0010: 3e                                      >0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w0010: 6f72 6c64 215c 6e22 293b                ORLD!\N");0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W0030: 6f72 6473                               ORDS類比,需注意最後一列不用補齊,還有注意當字元個數為16的整數倍時的行數。language:c++code:
#include<iostream>#include<cctype>#include<cstring>#include<cstdio>using namespace std;int main(){    char s[4100];    while(gets(s))    {        int len=strlen(s);        int line;        if(len%16==0)line=len/16;        else line=len/16+1;        for(int i=0;i<line;i++)        {            int count=0;            printf("%.4x: ",i<<4);            for(int j=i*16;s[j]&&j<i*16+16;j++)            {                printf("%.2x",s[j]);                count+=2;                if(j&1){putchar(' ');count++;}            }            while(count++<40)putchar(' ');            for(int j=i*16;s[j]&&j<i*16+16;j++)            {                if(islower(s[j]))                putchar(toupper(s[j]));                else                putchar(tolower(s[j]));            }            printf("\n");        }    }    return 0;}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.