2013-03-09 3 views
3

다음 C 문제로 도와 주시겠습니까? 특정 메모리 주소에서 시작하여 30 개의 연속 값 목록을 인쇄하려고합니다. 1 바이트를 메모리 위치 당 16 진수로 인쇄하고 싶습니다. 삽입 할 메모리 주소를 표시하기 위해 프로그램의 시작 부분에 더미의 주소를 인쇄합니다.C에서 1 바이트 16 진수로 메모리 인쇄

문제점은 1 바이트 이상의 값을 얻는 것입니다. 0의 긴 시퀀스의 경우 여전히 00이되지만, 0이 아닌 값이 나타나 자마자 4 바이트의 '창'이 인쇄됩니다. 이것은 다음과 같은 출력 결과 :

Main function address is 0x8048494 
Dummy variable address is 0x9a2e008 
Enter the address (without the 0x):9a2e008 
You entered: 9a2e008 
Address  Byte value 
0x9a2e008 00 
0x9a2e009 00 
0x9a2e00a 00 
0x9a2e00b 00 
0x9a2e00c 00 
0x9a2e00d 00 
0x9a2e00e 00 
0x9a2e00f 00 
0x9a2e010 00 
0x9a2e011 f1000000 
0x9a2e012 ff10000  
0x9a2e013 20ff100 
0x9a2e014 20ff1 
0x9a2e015 3900020f 
0x9a2e016 61390002 
0x9a2e017 32613900 
0x9a2e018 65326139 
0x9a2e019 30653261 
0x9a2e01a 30306532 
0x9a2e01b 38303065 
0x9a2e01c 383030 
0x9a2e01d 3830 
0x9a2e01e 38 
0x9a2e01f 00 
0x9a2e020 00 
0x9a2e021 00 
0x9a2e022 00 
0x9a2e023 00 
0x9a2e024 00 
0x9a2e025 00 

내 코드까지입니다 :

#include <assert.h> 
#include <stdio.h> 
#include <stdlib.h> 

#define NUMBER_OF_BYTES 10 

void showAddresses(void); 
void printMemoryAtAddress(void); 

int * dummy;          
int dumpSize = 30;           

int main(void) 
{ 
    dummy = (int *) malloc  (sizeof(int));          showAddresses();           
    printMemoryAtAddress(); 
    return 0; 
} 

void showAddresses(void) 
{ 
    printf("Main function address is %p \n", main); 
    printf("Dummy variable address is %p \n",(void*)dummy); 
} 

void printMemoryAtAddress(void) 
{ 
    int input; 
    printf("Enter the address (without the 0x):"); 
    scanf("%x", &input); 
    printf("You entered: %x \n", input); 
    printf("Address \tByte value \n"); 

    int i; 
    for(i=0;i<dumpSize;i++) 
    { 
     int* address; 
     address = (int*) (input+i); 
     printf("%p \t", address); 
     printf("%.2x \n", *address);           
    } 
} 

이이 문제에 어떤 도움이 아주 많이

을 주시면 감사하겠습니다! 이 질문이 바보라면 사과드립니다. (나는 아직도 배우고과 노력의 시간 후에 해결책을 찾을 수 없습니다!) 조

+1

'int * 주소;를'unsigned char * 주소; '로 변경하십시오. – wildplasser

+0

빠른 답장을 보내 주셔서 감사합니다! 그게 효과가! 길 옆의 멋진 별명 :) – Joe

답변

7

귀하의 문제는 여기에 있습니다 :

void printMemoryAtAddress(void) 
{ 
    int input; 
    printf("Enter the address (without the 0x):"); 
    scanf("%x", &input); 
    printf("You entered: %x \n", input); 
    printf("Address \tByte value \n"); 

    int i; 
    for(i=0;i<dumpSize;i++) 
    { 
     int* address; 
     address = (int*) (input+i); 
     printf("%p \t", address); 
     printf("%.2x \n", *address);           
    } 
} 

주소이 서명 숯불해야한다 * int가 아닌 *. int *는 정수 인 것처럼 메모리를 읽습니다. char로 읽으려면 1 바이트 만 필요합니다.

행운을 빈다.

+0

와아! 그것은 빨랐다! 도와 줘서 고마워, 그 트릭을 했어! – Joe

관련 문제