Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to convert a byte array to a hex string in C

1 Answer

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

void byteArrayToHexString(const unsigned char* byteArray, int length, char* hexString) {
    char buffer[3]; // for two hex digits + null terminator

    hexString[0] = '\0'; // start with an empty string

    for (int i = 0; i < length; i++) {
        sprintf(buffer, "%02X", byteArray[i]);
        strcat(hexString, buffer);
    }
}

int main() {
    unsigned char byteArray[] = {3, 10, 7, 15, 12, 255};
    int length = sizeof(byteArray) / sizeof(byteArray[0]);

    char hexString[2 * length + 1]; // two characters per byte, plus null terminator
    byteArrayToHexString(byteArray, length, hexString);

    printf("Hex String: %s\n", hexString);

    return 0;
}


   
/*
run:
   
Hex String: 030A070F0CFF
 
*/

 



answered Jun 21, 2025 by avibootz
...