使用条件语句从十六进制显示到char

时间:2013-10-16 23:05:07

标签: c pointers dynamic-memory-allocation srand

所以基本上我希望我的程序显示以下内容:
(存储器地址)(16个字节的十六进制值)(以字符为单位的十六进制值)
现在,我的格式正确,但以下行总是返回'0',因此根本没有显示任何字符:
printf("%c", isgraph(*startPtr)? *startPtr:'.');
最后,我认为我正在使用srandrand,但我的数组并没有被随机填充。它总是一样的。
无论如何,这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

void DumpMem(void *arrayPtr, int numBytes);
void FillMem(void *base, int numBytes);

int main(void)
{
    auto int numBytes;
    auto double *doublePtr;
    auto char *charPtr;
    auto int *intPtr;

    srand(time(NULL));
    // Doubles
    printf("How many doubles? ");
    scanf("%d", &numBytes);
    doublePtr = malloc(numBytes * sizeof(*doublePtr));

    if (NULL == doublePtr)
    {
        printf("Malloc failed!");
    }

    printf("Here's a dynamic array of doubles... \n");
    FillMem(doublePtr, numBytes * sizeof(*doublePtr));
    DumpMem(doublePtr, numBytes * sizeof(*doublePtr));
    // Chars
    printf("\nHow many chars? \n");
    scanf("%d", &numBytes);
    charPtr = malloc(numBytes * sizeof(*charPtr));

    if (NULL == charPtr)
    {
        printf("Malloc failed!");
    }

    printf("Here's a dynamic array of chars... \n");
    FillMem(charPtr, numBytes * sizeof(*charPtr));
    DumpMem(charPtr, numBytes * sizeof(*charPtr));
    // Ints
    printf("\nHow many ints? \n");
    scanf("%d", &numBytes);
    intPtr = malloc(numBytes * sizeof(*intPtr));

    if (NULL == intPtr)
    {
        printf("Malloc failed!");
    }

    printf("Here's a dynamic array of ints... \n");
    FillMem(intPtr, numBytes * sizeof(*intPtr));
    DumpMem(intPtr, numBytes * sizeof(*intPtr));

    // Free memory used
    free(doublePtr);
    free(charPtr);
    free(intPtr);
}

void DumpMem(void *arrayPtr, int numBytes)
{
    auto unsigned char *startPtr = arrayPtr;
    auto int counter = 0;
    auto int asciiBytes = numBytes;

    while (numBytes > 0)
    {
        printf("%p ", startPtr);

        for (counter = 0; counter < 8; counter++)
        {
            if (numBytes > 0)
            {
                printf("%02x ", *startPtr);
                startPtr++;
                numBytes--;
            }
            else
            {
                printf("   ");
            }
        }

        printf(" ");

        for (counter = 0; counter < 8; counter++)
        {
            if (numBytes > 0)
            {
                printf("%02x ", *startPtr);
                startPtr++;
                numBytes--;
            }
            else
            {
                printf("   ");
            }
        }

        printf(" |");
        // 'Rewind' where it's pointing to
        startPtr -= 16;

        for (counter = 0; counter < 16; counter++)
        {
            if (asciiBytes > 0)
            {
                printf("%c", isgraph(*startPtr)? *startPtr:'.');
                asciiBytes--;
            }
            else
            {
                printf(" ");
            }
        }
        puts("| ");
    }
}

void FillMem(void *base, int numBytes)
{
    auto unsigned char *startingPtr = base;

    while (numBytes > 0)
    {
        *startingPtr = (unsigned char)rand;
        numBytes--;
        startingPtr++;
    }
}

为什么我没有在数组中获取随机值?为什么我的条件语句总是'false'

1 个答案:

答案 0 :(得分:1)

您正在使用函数指针的低位字节填充数组rand,而不是随机数。您需要调用函数:

    *startingPtr = (unsigned char)rand();

在打印字符数据时,您也没有递增startPtr。你需要startPtr++

        if (asciiBytes > 0)
        {
            printf("%c", isgraph(*startPtr)? *startPtr:'.');
            startPtr++;
            asciiBytes--;
        }

按照目前的情况,你的程序只是一遍又一遍地打印第一个字节,然后继续下一行并将相同的一行打印到上一行。