简体   繁体   English

将ac十六进制值转换为char *

[英]Convert a c hex value into a char*

How to convert a hex value in c into an equivalent char* value. 如何将c中的hex值转换为等效的char*值。 For example if the hex value is 1df2 the char* should also contain 1df2 . 例如,如果十六进制值为1df2则char *还应包含1df2

I am using the VinC compiler and the VinL linker for the VNC2 USB Chip from FTDI . 我正在使用FTDIVNC2 USB ChipVinC编译器和VinL链接器。 It has these following header files; 它具有以下这些头文件; stdlib , stdio and string . stdlibstdiostring These are however subsets of the main c libraries and don't have the obvious answers such as snprintf or sprintf . 但是,这些是主要c库的子集,没有明显的答案,例如snprintfsprintf

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. 它们可用于vos.h头文件中的应用程序。

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() : 使用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: 有了新的信息,它是一个相当基本的嵌入式系统,那么,如果您只对16位数字感兴趣,那么这样的最小解决方案可能就足够了:

/* 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 : 尝试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. 它比caf的答案安全,但是如果您有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. 但是您是说您有stdio可用,因此您无需自己编写类似的内容。

Edit: Could be that the compiler expects K&R style prototype: 编辑:可能是编译器期望K&R样式原型:

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

Try this on Codepad . Codepad上尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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