簡體   English   中英

將無符號字符數組轉換為十六進制

[英]Converting unsigned char array to hex

我想將unsigned char轉換為十六進制(使用unsigned int)。 到目前為止,這是我的代碼。 我有一個program1,它產生一個無符號char數組,而另一個program2只接受十六進制(使用unsigned int),所以我要實現的是獲取無符號char數組的輸入並將該數組轉換為16進制。

(例如,program1輸出“ 1234567812345678”,program2應輸出“ 31323334353637383383132333435363738”)

抱歉,這個問題似乎很愚蠢。 在這里四處尋找答案,但這似乎不是我想要的。

uint64_t phex (unsigned char[16], long);



int main (void) {

int i;
unsigned char key[16] = "1234567812345678";


uint64_t* keyHex = phex(key,16); //store into an array here

for(i = 0; i < 16; i++)
printf("%.2x ", keyHex[i]);

free(keyHex);

return 0;

}


uint64_t phex(unsigned char* string, long len)
{

int i;
//allocate memory for your array
uint64_t* hex = (uint64_t*)malloc(sizeof(uint64_t) * len);

for(i = 0; i < len; ++i) {
    //do char to int conversion on every element of char array
    hex[i] = string[i] - '0';
}

//return integer array
return hex;

}

如果您只需要打印值,那么您就不需要進行任何轉換。 只需在原始陣列上使用printf %.2x

int main (void) {
    int i;
    unsigned char key[16] = "1234567812345678";
    for(i = 0; i < 16; i++)
        printf("%.2x", key[i]);
    return 0;
}

即使您想在其他功能中使用數組, key中存儲的實際字節也是ascii字符,即0x31 0x32等。通常,您可以直接使用數組key

編輯:要將輸出存儲在字符數組中,可以使用sprintf函數。

char hex[33];
for(i = 0; i < 16; i++)
    sprintf(hex+2*i, "%.2x", key[i]);

另請注意,原始數組key應為17個字節,以末尾占\\0

這是我的phex()函數將內存中的任何數據轉換為包含十六進制表示形式的新分配的字符串。

main()函數顯示了用法示例。 示例data的輸出為“ 31323334353637383930 ”。

#include <stdlib.h> /* malloc() */
#include <stdio.h>  /* sprintf() */
#include <string.h> /* strlen(), in the example main() */

/*
 * Return a hex string representing the data pointed to by `p`,
 * converting `n` bytes.
 *
 * The string should be deallocated using `free()` by the caller.
 */
char *phex(const void *p, size_t n)
{
    const unsigned char *cp = p;              /* Access as bytes. */
    char *s = malloc(2*n + 1);       /* 2*n hex digits, plus NUL. */
    size_t k;

    /*
     * Just in case - if allocation failed.
     */
    if (s == NULL)
        return s;

    for (k = 0; k < n; ++k) {
        /*
         * Convert one byte of data into two hex-digit characters.
         */
        sprintf(s + 2*k, "%02X", cp[k]);
    }

    /*
     * Terminate the string with a NUL character.
     */
    s[2*n] = '\0';

    return s;
}

/*
 * Sample use of `phex()`.
 */
int main(void)
{
    const char *data = "1234567890";               /* Sample data */
    char *h = phex(data, strlen(data));  /* Convert to hex string */

    if (h != NULL)
        puts(h);                                  /* Print result */

    free(h);                             /* Deallocate hex string */
    return 0;
}

我看到功能簽名為

 uint64_t phex (unsigned char[16], long);

所以我認為,您不需要uint64_t數組來轉換一個字符串,表示一個數字(也許我錯了,您希望將每個字符從其char表示形式轉換為int並顯示為十六進制數字)。

首先,讓我們考慮以下代碼以十進制轉換(實際上,示例中的數字1234567812345678看起來像十進制數字):

uint64_t phex(unsigned char* string, long len)
{

    int i;
    //you do not need to allocate memory for array
    uint64_t hex = 0; // just one variable

    for (i = 0; i < len; ++i) {
        hex *= 10; // shift
        hex += string[i] - '0'; // add to the smallest rank
    }

    //return integer Value
    return hex;

}

然后對於十六進制,程序將是:

#include <stdio.h>
#include <stdint.h>
#include <string.h>
uint64_t phex(unsigned char[16], long);

int main(void) {

    int i;
    unsigned char key[16] = "A123B00CF1";


    uint64_t keyHex = phex(key, strlen(key));

    printf("%lld(dec) = %llx(hex)", keyHex, keyHex);

    return 0;
}


uint64_t phex(unsigned char* string, long len)
{
    int i;
    uint64_t hex = 0;
    for (i = 0; i < len; ++i) {
        hex *= 0x10; // shift for one hexadecimal digit
        // -'0' for digits 0..9 and -('A'-10) for digits `A`..`F`
        hex += toupper(string[i]) - ((string[i] >= 'A') ? ('A' - 10) : '0');
        // TODO: Consider making check for characters that are not hexadecimal
        // e.g. use isxdigit(string[i]) in some if statement
    }
    return hex;
}

注意:您的示例代碼中有一個錯誤uint64_t* keyHex從返回uint64_t函數中獲取值(不是指針``uint64_t *`''),但是如果您接受我的想法,則根本不需要指針。

如果任務是將字符('1','2'等)轉換為十六進制表示形式(對於'1'為31,對於'2'為32,等等),很難理解為什么需要uint64_t

但是對於您的任務代碼,可以如下所示(不帶uint64_t ):

#include <stdio.h>
#include <string.h>

unsigned int * phex(unsigned char[16], long);

int main(void) {
    int i;
    unsigned char key[16] = "1234567812345678";
    unsigned* keyHex = phex(key, strlen(key)); // strlen(key) to determine number of characters 
    for (i = 0; i < 16; i++)
        printf("%.2x", keyHex[i]); // you do need space as I see from your example
    free(keyHex);
    return 0;

}


unsigned int * phex(unsigned char* string, long len)
{
    int i;
    //allocate memory for array
    unsigned int * hex = (unsigned int *)malloc(sizeof(unsigned int) * len);
    for (i = 0; i < len; ++i) {
        //no special conversion needed
        hex[i] = string[i];
    }
    //return array with hexadecimal representation for each character in string
    return hex;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM