How do you convert a byte array to a hexadecimal string in C?
Asked Answered
M

19

119

I have:

uint8 buf[] = {0, 1, 10, 11};

I want to convert the byte array to a string such that I can print the string using printf:

printf("%s\n", str);

and get (the colons aren't necessary):

"00:01:0A:0B"

Any help would be greatly appreciated.

Mendy answered 15/6, 2011 at 11:30 Comment(1)
buf[i] must be casted to unsigned char, or it will overflow if buf[i] > 127, that is:buf_ptr += sprintf(buf_ptr, "%02X", (unsigned char)buf[i]);Titivate
C
120
printf("%02X:%02X:%02X:%02X", buf[0], buf[1], buf[2], buf[3]);

For a more generic way:

int i;
for (i = 0; i < x; i++)
{
    if (i > 0) printf(":");
    printf("%02X", buf[i]);
}
printf("\n");

To concatenate to a string, there are a few ways you can do this. I'd probably keep a pointer to the end of the string and use sprintf. You should also keep track of the size of the array to make sure it doesn't get larger than the space allocated:

int i;
char* buf2 = stringbuf;
char* endofbuf = stringbuf + sizeof(stringbuf);
for (i = 0; i < x; i++)
{
    /* i use 5 here since we are going to add at most 
       3 chars, need a space for the end '\n' and need
       a null terminator */
    if (buf2 + 5 < endofbuf)
    {
        if (i > 0)
        {
            buf2 += sprintf(buf2, ":");
        }
        buf2 += sprintf(buf2, "%02X", buf[i]);
    }
}
buf2 += sprintf(buf2, "\n");
Contrary answered 15/6, 2011 at 11:32 Comment(8)
Thank you Mark - my problem is a bit more complicated. I actually have a buffer with a length of X bytes. I was hoping to find a generic way of doing this for X bytes and having a string as the result.Mendy
Just updated to add code for handling any given number of bytes... assuming x is the length.Contrary
Thank you again Mark, but the thing I was finding most tricky for this problem is how to print this to a string.Mendy
This answer should still be sufficient then. Do you mean that you want to store a string with the hex values? If so checkout snprintf instead of printf. It allows you to print to a char * string.Report
yeah, that would be a little more difficult. i updated the post to use sprintf, using an extra pointer to keep track of where i want to write to (ie the end of the string).Contrary
@Mark I just submitted an answer, but since you were first I'll only point out that you may want to tack on a \0 byte at the end there.Report
printf("%02X", (unsigned char)buf[i]); should be used as the original will cause overflow for unsigned charsPetit
Why not printf("%02hhX", buf[i])?Telegu
V
44

For completude, you can also easily do it without calling any heavy library function (no snprintf, no strcat, not even memcpy). It can be useful, say if you are programming some microcontroller or OS kernel where libc is not available.

Nothing really fancy you can find similar code around if you google for it. Really it's not much more complicated than calling snprintf and much faster.

#include <stdio.h>

int main(){
    unsigned char buf[] = {0, 1, 10, 11};
    /* target buffer should be large enough */
    char str[12];

    unsigned char * pin = buf;
    const char * hex = "0123456789ABCDEF";
    char * pout = str;
    int i = 0;
    for(; i < sizeof(buf)-1; ++i){
        *pout++ = hex[(*pin>>4)&0xF];
        *pout++ = hex[(*pin++)&0xF];
        *pout++ = ':';
    }
    *pout++ = hex[(*pin>>4)&0xF];
    *pout++ = hex[(*pin)&0xF];
    *pout = 0;

    printf("%s\n", str);
}

Here is another slightly shorter version. It merely avoid intermediate index variable i and duplicating laste case code (but the terminating character is written two times).

#include <stdio.h>
int main(){
    unsigned char buf[] = {0, 1, 10, 11};
    /* target buffer should be large enough */
    char str[12];

    unsigned char * pin = buf;
    const char * hex = "0123456789ABCDEF";
    char * pout = str;
    for(; pin < buf+sizeof(buf); pout+=3, pin++){
        pout[0] = hex[(*pin>>4) & 0xF];
        pout[1] = hex[ *pin     & 0xF];
        pout[2] = ':';
    }
    pout[-1] = 0;

    printf("%s\n", str);
}

Below is yet another version to answer to a comment saying I used a "trick" to know the size of the input buffer. Actually it's not a trick but a necessary input knowledge (you need to know the size of the data that you are converting). I made this clearer by extracting the conversion code to a separate function. I also added boundary check code for target buffer, which is not really necessary if we know what we are doing.

#include <stdio.h>

void tohex(unsigned char * in, size_t insz, char * out, size_t outsz)
{
    unsigned char * pin = in;
    const char * hex = "0123456789ABCDEF";
    char * pout = out;
    for(; pin < in+insz; pout +=3, pin++){
        pout[0] = hex[(*pin>>4) & 0xF];
        pout[1] = hex[ *pin     & 0xF];
        pout[2] = ':';
        if (pout + 3 - out > outsz){
            /* Better to truncate output string than overflow buffer */
            /* it would be still better to either return a status */
            /* or ensure the target buffer is large enough and it never happen */
            break;
        }
    }
    pout[-1] = 0;
}

int main(){
    enum {insz = 4, outsz = 3*insz};
    unsigned char buf[] = {0, 1, 10, 11};
    char str[outsz];
    tohex(buf, insz, str, outsz);
    printf("%s\n", str);
}
Venter answered 11/10, 2012 at 12:46 Comment(9)
It's not a trick, merely a constant. In the context of the question it is clear that the length of the source we want to convert to hexadecimal is well known (I could have put some hardcoded 4 instead of sizeof). In the general case the function should be called on some input of known length and the target buffer have 3 times + 1 bytes available. This must be ensured by the caller, there is no reason for the conversion function to perform that task. Calling strlen() may be a way to find the source size in some cases, but not always. What if the number to convert to hex contains zeroes ?Venter
Inspired by your function, I wrote a version which also returns the number of bytes written to the output buffer, similar to snprintf, etc. gist.github.com/cellularmitosis/0d8c0abf7f8aa6a2dff3Vaca
I think you should make your output buffer the correct size automatically using char str[ sizeof(buf)*3 + 1 ];Jessiajessica
Also a lot more consts would safeguard you. Eg "const unsigned char const * p" so that you can make sure that input buffers are not written to. One makes the address (or 'pointer') constant, or variable, and the other makes the memory at that address read-only or not. Often stops you from getting the pointers mixed up. And also, having meaningful names which document which buffers and pointers are for input and output would help too.Jessiajessica
@Cecil War: unless my code is bogus using const won't safeguard much except as you say mixing up pointers or using the same pointer for input and output (ok, still possible). But it will also help compiler to optimize code. Even better would be to also use restrict keyword (too bad C99 not C++, but often exists as a compiler extension). What do you want more meaninfull as to call input buffer in and output buffer out ? I could also opt for using a string and returning a copy instead of providing output buffer, in modern C++ optimizers are good enough to don't care much.Venter
The advantage of my explicit sizeof expression above is that it protects you by resizing the output buffer automatically if you add one more value into the initialiser list. In your updated version, you have to hope the constants are correct/maintained. An assert would be another route, more flexible in some situations.Jessiajessica
The thing with truncating the buffer is that the user would end up with half a data structure missing in some cases, so might read on past the end of the output into garbage land. It also makes all kinds of odd assumptions about the presence or absence of zero-terminators and corrupts the output by writing a zero over the last byte (even if the user does not want one). The best thing would be to assume nothing and not have any zero-terminators assumed or added. That's for the caller.Jessiajessica
@Cecil Ward: depends on the kind of project you are working on. In some context all memory allocation may be forbidden, in such cases resizing is a no go. I'm not sure what you suggest with your sizeof. Either it has to be be inside caller code besause in callee it just won't work (because of pointer decay), or the input parameters could be defined as reference to arrays in which case the size can even be implicit. But in non trivial cases the same function could be used for instance to append an hex value to some text message then the actual memory checks become different.Venter
@Cecil Ward: doing nothing is indeed an option. That's what I wrote in my code comment.Venter
E
32

Similar answers already exist above, I added this one to explain how the following line of code works exactly:

ptr += sprintf(ptr, "%02X", buf[i])

It's quiet tricky and not easy to understand, I put the explanation in the comments below:

uint8 buf[] = {0, 1, 10, 11};

/* Allocate twice the number of bytes in the "buf" array because each byte would
 * be converted to two hex characters, also add an extra space for the terminating
 * null byte.
 * [size] is the size of the buf array */
char output[(size * 2) + 1];

/* pointer to the first item (0 index) of the output array */
char *ptr = &output[0];

int i;

for (i = 0; i < size; i++) {
    /* "sprintf" converts each byte in the "buf" array into a 2 hex string
     * characters appended with a null byte, for example 10 => "0A\0".
     *
     * This string would then be added to the output array starting from the
     * position pointed at by "ptr". For example if "ptr" is pointing at the 0
     * index then "0A\0" would be written as output[0] = '0', output[1] = 'A' and
     * output[2] = '\0'.
     *
     * "sprintf" returns the number of chars in its output excluding the null
     * byte, in our case this would be 2. So we move the "ptr" location two
     * steps ahead so that the next hex string would be written at the new
     * location, overriding the null byte from the previous hex string.
     *
     * We don't need to add a terminating null byte because it's been already 
     * added for us from the last hex string. */  
    ptr += sprintf(ptr, "%02X", buf[i]);
}

printf("%s\n", output);
Endopeptidase answered 15/12, 2016 at 20:59 Comment(1)
Brilliant logic. Was looking for an hour for an elegant non C++ string answer to this challenge!Confabulate
M
15

Here is a method that is way way faster :

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

unsigned char *     bin_to_strhex(const unsigned char *bin, unsigned int binsz,
                                  unsigned char **result)
{
  unsigned char     hex_str[]= "0123456789abcdef";
  unsigned int      i;

  if (!(*result = (unsigned char *)malloc(binsz * 2 + 1)))
    return (NULL);

  (*result)[binsz * 2] = 0;

  if (!binsz)
    return (NULL);

  for (i = 0; i < binsz; i++)
    {
      (*result)[i * 2 + 0] = hex_str[(bin[i] >> 4) & 0x0F];
      (*result)[i * 2 + 1] = hex_str[(bin[i]     ) & 0x0F];
    }
  return (*result);
}

int                 main()
{
  //the calling
  unsigned char     buf[] = {0,1,10,11};
  unsigned char *   result;

  printf("result : %s\n", bin_to_strhex((unsigned char *)buf, sizeof(buf), &result));
  free(result);

  return 0
}
Maggio answered 17/6, 2013 at 12:49 Comment(6)
This code contains a bug which manifests itself only on strange non-printable inputs (haven't had time to dig into exactly what's going on mathematically). Try to encode the binary of hexadecimal ca9e3c972f1c5db40c0b4a66ab5bc1a20ca4457bdbe5e0f8925896d5ed37d726 and you'll get ÌaÌe3cÌ72f1c5dÌ40c0b4a66Ìb5bÌ1Ì20cÌ4457bÌbÌ5Ì0Ì8Ì258Ì6Ì5Ìd37Ì726 out. To fix this, the bit inside hex_str in the first line of the for loop needs to be changed to (input[i] >> 4) & 0x0F as in @kriss's answer. Then it works fine.Protrude
Bug - doesn't check for the malloc() failure.Jessiajessica
It is always better to use unsigned char absolutely everywhere as nobody wants the risk of signed chars (a mad DEC PDP11 hardware feature), and that way you don't run the risk of signed comparisons going wrong or signed right shifts corrupting values. In this case, to be fair, the code does defensively do an & 0x0F everywhere which protects you here.Jessiajessica
The bin input parameter ought to be const unsigned char const * bin, to declare the memory as read-only for the purposes of this routine.Jessiajessica
I would much prefer to return the address of the result buffer as the function return value - it would be much more efficient, for numerous reasons, and easier to read.Jessiajessica
I've integrated the propositions of Cecil Ward, thanks for feedbackMaggio
P
14

Solution

Function btox converts arbitrary data *bb to an unterminated string *xp of n hexadecimal digits:

void btox(char *xp, const char *bb, int n) 
{
    const char xx[]= "0123456789ABCDEF";
    while (--n >= 0) xp[n] = xx[(bb[n>>1] >> ((1 - (n&1)) << 2)) & 0xF];
}

Example

#include <stdio.h>

typedef unsigned char uint8;

void main(void) 
{
    uint8 buf[] = {0, 1, 10, 11};
    int n = sizeof buf << 1;
    char hexstr[n + 1];

    btox(hexstr, buf, n);
    hexstr[n] = 0; /* Terminate! */
    printf("%s\n", hexstr);
}

Result: 00010A0B.

Live: Tio.run.

Pesthouse answered 29/12, 2018 at 3:0 Comment(1)
naming could be betterFumed
K
7

I just wanted to add the following, even if it is slightly off-topic (not standard C), but I find myself looking for it often, and stumbling upon this question among the first search hits. The Linux kernel print function, printk, also has format specifiers for outputting array/memory contents "directly" through a singular format specifier:

https://www.kernel.org/doc/Documentation/printk-formats.txt

Raw buffer as a hex string:
    %*ph    00 01 02  ...  3f
    %*phC   00:01:02: ... :3f
    %*phD   00-01-02- ... -3f
    %*phN   000102 ... 3f

    For printing a small buffers (up to 64 bytes long) as a hex string with
    certain separator. For the larger buffers consider to use
    print_hex_dump(). 

... however, these format specifiers do not seem to exist for the standard, user-space (s)printf.

Keffer answered 30/10, 2013 at 13:39 Comment(0)
P
1

This is one way of performing the conversion:

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

#define l_word 15
#define u_word 240

char *hex_str[]={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};

main(int argc,char *argv[]) {


     char *str = malloc(50);
     char *tmp;
     char *tmp2;

     int i=0;


     while( i < (argc-1)) {
          tmp = hex_str[*(argv[i]) & l_word];
          tmp2 = hex_str[*(argv[i]) & u_word];

          if(i == 0) { memcpy(str,tmp2,1); strcat(str,tmp);}
          else { strcat(str,tmp2); strcat(str,tmp);}
          i++;
    }

    printf("\n*********  %s  *************** \n", str);

}
Phonsa answered 26/7, 2012 at 10:12 Comment(0)
M
1

Slightly modified Yannith version. It is just I like to have it as a return value

typedef struct {
   size_t len;
   uint8_t *bytes;
} vdata;

char* vdata_get_hex(const vdata data)
{
   char hex_str[]= "0123456789abcdef";

   char* out;
   out = (char *)malloc(data.len * 2 + 1);
   (out)[data.len * 2] = 0;
   
   if (!data.len) return NULL;
   
   for (size_t i = 0; i < data.len; i++) {
      (out)[i * 2 + 0] = hex_str[(data.bytes[i] >> 4) & 0x0F];
      (out)[i * 2 + 1] = hex_str[(data.bytes[i]     ) & 0x0F];
   }
   return out;
}
Malca answered 10/1, 2016 at 1:27 Comment(0)
N
1

This function is suitable where user/caller wants hex string to be put in a charactee array/buffer. With hex string in a character buffer, user/caller can use its own macro/function to display or log it to any place it wants (e.g. to a file). This function also allows caller to control number of (hex) bytes to put in each line.

/**
 * @fn 
 * get_hex
 *
 * @brief 
 * Converts a char into bunary string 
 *
 * @param[in]   
 *     buf Value to be converted to hex string
 * @param[in]   
 *     buf_len Length of the buffer
 * @param[in]   
 *     hex_ Pointer to space to put Hex string into
 * @param[in]   
 *     hex_len Length of the hex string space
 * @param[in]   
 *     num_col Number of columns in display hex string
 * @param[out]   
 *     hex_ Contains the hex string
 * @return  void
 */
static inline void
get_hex(char *buf, int buf_len, char* hex_, int hex_len, int num_col)
{
    int i;
#define ONE_BYTE_HEX_STRING_SIZE   3
  unsigned int byte_no = 0;

  if (buf_len <= 0) {
      if (hex_len > 0) {
        hex_[0] = '\0';
      }
      return;
  }

  if(hex_len < ONE_BYTE_HEX_STRING_SIZE + 1)
  {
      return;
  }

  do {
         for (i = 0; ((i < num_col) && (buf_len > 0) && (hex_len > 0)); ++i )
         {
            snprintf(hex_, hex_len, "%02X ", buf[byte_no++] & 0xff);
            hex_ += ONE_BYTE_HEX_STRING_SIZE;
            hex_len -=ONE_BYTE_HEX_STRING_SIZE;
            buf_len--;
         }
         if (buf_len > 1)
         {
             snprintf(hex_, hex_len, "\n");
             hex_ += 1;
         }
  } while ((buf_len) > 0 && (hex_len > 0));

}

Example: Code

#define DATA_HEX_STR_LEN 5000
    char      data_hex_str[DATA_HEX_STR_LEN];

    get_hex(pkt, pkt_len, data_hex_str, DATA_HEX_STR_LEN, 16);
    //      ^^^^^^^^^^^^                                  ^^
    //      Input byte array                              Number of (hex) byte
    //      to be converted to hex string                 columns in hex string

    printf("pkt:\n%s",data_hex_str) 

OUTPUT

pkt:
BB 31 32 00 00 00 00 00 FF FF FF FF FF FF DE E5 
A8 E2 8E C1 08 06 00 01 08 00 06 04 00 01 DE E5 
A8 E2 8E C1 67 1E 5A 02 00 00 00 00 00 00 67 1E 
5A 01 
Natasha answered 21/12, 2016 at 13:53 Comment(0)
L
1

Based on Yannuth's answer but simplified.

Here, length of dest[] is implied to be twice of len, and its allocation is managed by the caller.

void create_hex_string_implied(const unsigned char *src, size_t len, unsigned char *dest)
{
    static const unsigned char table[] = "0123456789abcdef";

    for (; len > 0; --len)
    {
        unsigned char c = *src++;
        *dest++ = table[c >> 4];
        *dest++ = table[c & 0x0f];
    }
}
Lavonia answered 16/8, 2017 at 12:44 Comment(0)
P
1

You can solve with snprintf and malloc.

char c_buff[50];

u8_number_val[] = { 0xbb, 0xcc, 0xdd, 0x0f, 0xef, 0x0f, 0x0e, 0x0d, 0x0c };

char *s_temp = malloc(u8_size * 2 + 1);

for (uint8_t i = 0; i < u8_size; i++)
{
    snprintf(s_temp  + i * 2, 3, "%02x", u8_number_val[i]);
}

snprintf(c_buff, strlen(s_temp)+1, "%s", s_temp );

printf("%s\n",c_buff);

free(s);

OUT: bbccdd0fef0f0e0d0c

Panjandrum answered 15/11, 2019 at 5:52 Comment(0)
S
0

There's no primitive for this in C. I'd probably malloc (or perhaps alloca) a long enough buffer and loop over the input. I've also seen it done with a dynamic string library with semantics (but not syntax!) similar to C++'s ostringstream, which is a plausibly more generic solution but it may not be worth the extra complexity just for a single case.

Slide answered 15/6, 2011 at 11:43 Comment(0)
M
0

ZincX's solution adapted to include colon delimiters:

char buf[] = {0,1,10,11};
int i, size = sizeof(buf) / sizeof(char);
char *buf_str = (char*) malloc(3 * size), *buf_ptr = buf_str;
if (buf_str) {
  for (i = 0; i < size; i++)
    buf_ptr += sprintf(buf_ptr, i < size - 1 ? "%02X:" : "%02X\0", buf[i]);
  printf("%s\n", buf_str);
  free(buf_str);
}
Mllly answered 9/12, 2011 at 12:4 Comment(0)
K
0

I'll add the C++ version here for anyone who is interested.

#include <iostream>
#include <iomanip>
inline void print_bytes(char const * buffer, std::size_t count, std::size_t bytes_per_line, std::ostream & out) {
    std::ios::fmtflags flags(out.flags()); // Save flags before manipulation.
    out << std::hex << std::setfill('0');
    out.setf(std::ios::uppercase);
    for (std::size_t i = 0; i != count; ++i) {
        auto current_byte_number = static_cast<unsigned int>(static_cast<unsigned char>(buffer[i]));
        out << std::setw(2) << current_byte_number;
        bool is_end_of_line = (bytes_per_line != 0) && ((i + 1 == count) || ((i + 1) % bytes_per_line == 0));
        out << (is_end_of_line ? '\n' : ' ');
    }
    out.flush();
    out.flags(flags); // Restore original flags.
}

It will print the hexdump of the buffer of length count to std::ostream out (you can make it default to std::cout). Every line will contain bytes_per_line bytes, each byte is represented using uppercase two digit hex. There will be a space between bytes. And at end of line or end of buffer it will print a newline. If bytes_per_line is set to 0, then it will not print new_line. Try for yourself.

Kushner answered 1/12, 2013 at 8:9 Comment(0)
G
0

For simple usage I made a function that encodes the input string (binary data):

/* Encodes string to hexadecimal string reprsentation
    Allocates a new memory for supplied lpszOut that needs to be deleted after use
    Fills the supplied lpszOut with hexadecimal representation of the input
    */
void StringToHex(unsigned char *szInput, size_t size_szInput, char **lpszOut)
{
    unsigned char *pin = szInput;
    const char *hex = "0123456789ABCDEF";
    size_t outSize = size_szInput * 2 + 2;
    *lpszOut = new char[outSize];
    char *pout = *lpszOut;
    for (; pin < szInput + size_szInput; pout += 2, pin++)
    {
        pout[0] = hex[(*pin >> 4) & 0xF];
        pout[1] = hex[*pin & 0xF];
    }
    pout[0] = 0;
}

Usage:

unsigned char input[] = "This is a very long string that I want to encode";
char *szHexEncoded = NULL;
StringToHex(input, strlen((const char *)input), &szHexEncoded);

printf(szHexEncoded);

// The allocated memory needs to be deleted after usage
delete[] szHexEncoded;
Gnotobiotics answered 28/11, 2015 at 11:36 Comment(0)
G
0

I know this question already has an answer but I think my solution could help someone.

So, in my case I had a byte array representing the key and I needed to convert this byte array to char array of hexadecimal values in order to print it out in one line. I extracted my code to a function like this:

char const * keyToStr(uint8_t const *key)
{
    uint8_t offset = 0;
    static char keyStr[2 * KEY_SIZE + 1];

    for (size_t i = 0; i < KEY_SIZE; i++)
    {
        offset += sprintf(keyStr + offset, "%02X", key[i]);
    }
    sprintf(keyStr + offset, "%c", '\0');

    return keyStr;
}

Now, I can use my function like this:

Serial.print("Public key: ");
Serial.println(keyToStr(m_publicKey));

Serial object is part of Arduino library and m_publicKey is member of my class with the following declaration uint8_t m_publicKey[32].

Greensward answered 28/10, 2018 at 13:46 Comment(0)
F
0

The following might be helpful.

/**
 * @brief Convert an arbitrary length byte array to a hex representation
 * 
 * @param bytes 
 * @param array_length 
 * @param output 
 */
void bytes_to_hex(uint8_t *bytes, uint8_t array_length, char *output)
{

    for (int i = 0, j = 0; i < array_length; i++, j+=2)
    {
        uint8_t bottom = bytes[i] % 16;
        uint8_t top = bytes[i] / 16;
        output[j] = (top > 9 ? 'A' - 10 : '0') + top;
        output[j+1] = (bottom > 9 ? 'A' - 10 : '0') + bottom;
    }
}
Floury answered 6/6, 2023 at 21:38 Comment(0)
R
-1

If you want to store the hex values in a char * string, you can use snprintf. You need to allocate space for all the printed characters, including the leading zeros and colon.

Expanding on Mark's answer:

char str_buf* = malloc(3*X + 1);   // X is the number of bytes to be converted

int i;
for (i = 0; i < x; i++)
{
    if (i > 0) snprintf(str_buf, 1, ":");
    snprintf(str_buf, 2, "%02X", num_buf[i]);  // need 2 characters for a single hex value
}
snprintf(str_buf, 2, "\n\0"); // dont forget the NULL byte

So now str_buf will contain the hex string.

Report answered 15/6, 2011 at 12:5 Comment(1)
this overwrites the first 2 characters over and over.. right?Desta
A
-2

What complex solutions!
Malloc and sprints and casts oh my. (OZ quote)
and not a single rem anywhere. Gosh

How about something like this?

main()
{
    // the value
    int value = 16;

    // create a string array with a '\0' ending ie. 0,0,0
    char hex[]= {0,0,'\0'}; 
    char *hex_p=hex;

    //a working variable
    int TEMP_int=0;

    // get me how many 16s are in this code
    TEMP_int=value/16;

    // load the first character up with 
    // 48+0 gives you ascii 0, 55+10 gives you ascii A
    if (TEMP_int<10) {*hex_p=48+TEMP_int;}
        else {*hex_p=55+TEMP_int;}

    // move that pointer to the next (less significant byte)<BR>
    hex_p++;

    // get me the remainder after I have divied by 16
    TEMP_int=value%16;

    // 48+0 gives you ascii 0, 55+10 gives you ascii A
    if (TEMP_int<10) {*hex_p=48+TEMP_int;}
        else {*hex_p=55+TEMP_int;}

    // print the result
    printf("%i , 0x%s",value,hex);

}
Aurita answered 13/10, 2014 at 10:20 Comment(2)
OK, now you have two hex digits. It remains to add separators and take care of the other bytes to converts. Maybe with a loop ? Make it a function and you'll have something similar to mine (but rather verbose and hard to read). Maybe you should at least finish the job before calling names on other posters?Venter
And a word about comments in source code (not REM, that's BASIC keyword for comments, plese avoid that): comments saying in english what the code is doing is very very bad practice! Yes programmers are supposed to know what modulo operators mean (give the remains) and that division count the number of times a number appears in another one... and that printf print the result. Oh my!Venter

© 2022 - 2024 — McMap. All rights reserved.