简体   繁体   中英

Convert a c hex value into a char*

How to convert a hex value in c into an equivalent char* value. For example if the hex value is 1df2 the char* should also contain 1df2 .

I am using the VinC compiler and the VinL linker for the VNC2 USB Chip from FTDI . It has these following header files; stdlib , stdio and string . These are however subsets of the main c libraries and don't have the obvious answers such as snprintf or sprintf .

The docs say the following types are valid,

There are certain definitions for variable and function types which are used throughout the kernel and drivers. They are available to applications in the vos.h header file.

Null pointer and logic definitions:

#define NULL                0
#define TRUE                1
#define FALSE               0

Variable type definitions:

#define uint8               unsigned char
#define int8                char
#define int16               short
#define uint16              unsigned short
#define uint32              unsigned int
#define pvoid               unsigned char *

Function type definitions:

typedef uint8 (*PF)(uint8);
typedef void (*PF_OPEN)(void *);
typedef void (*PF_CLOSE)(void *);
typedef uint8 (*PF_IOCTL)(pvoid);
typedef uint8 (*PF_IO)(uint8 *, unsigned short, unsigned short *);
typedef void (*PF_INT)(void);

Any suggestions?

Use snprintf() :

int to_hex(char *output, size_t len, unsigned n)
{    
    return snprintf(output, len, "%.4x", n);
}

Given the new information that it's a fairly basic embedded system, then if you're only interested in 16 bit numbers a minimal solution like this probably suffices:

/* output points to buffer of at least 5 chars */
void to_hex_16(char *output, unsigned n)
{
    static const char hex_digits[] = "0123456789abcdef";

    output[0] = hex_digits[(n >> 12) & 0xf];
    output[1] = hex_digits[(n >> 8) & 0xf];
    output[2] = hex_digits[(n >> 4) & 0xf];
    output[3] = hex_digits[n & 0xf];
    output[4] = '\0';
}

(It should be clear how to extend it to wider numbers).

Try sprintf :

int to_hex(char *output,unsigned n)
{    
    return sprintf(output, "%.4x", n);
}

it's less safe than caf's answer but should work if you have stdio. You must therefore make sure that the output buffer is big enough to hold the resulting string.

Something like this should do it:

void to_hex(char *buffer, size_t size, unsigned n)
{
    size_t i;
    size_t j;
    char c;
    unsigned digit;

    // Print digits in the reverse order
    for (i = 0; i < size - 1; ++i)
    {
        digit = n & 0xf;
        buffer[i] = digit < 10 ? digit + '0' : digit - 10 + 'A';
        n >>= 4;

        if (n == 0)
        {
            break;
        }
    }

    // Append NUL
    buffer[i + 1] = 0;

    // Reverse the string
    for (j = 0; j < i / 2; ++j)
    {
        c = buffer[j];
        buffer[j] = buffer[i - j];
        buffer[i - j] = c;
    }
}

But you are saying you have stdio available, so there's no need write anything like this yourself.

Edit: Could be that the compiler expects K&R style prototype:

void to_hex(buffer, size, n)
    char *buffer;
    size_t size; 
    unsigned n;
{
...

Try this on Codepad .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM