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

发布于 2024-10-07 18:48:43 字数 1175 浏览 1 评论 0原文

如何将 c 中的 hex 值转换为等效的 char* 值。例如,如果十六进制值为 1df2,则 char* 还应包含 1df2

我正在使用来自 FTDIVNC2 USB 芯片VinC 编译器和 VinL 链接器。它有以下头文件; stdlibstdio字符串。然而,这些是主要 C 库的子集,没有明显的答案,例如 snprintfsprintf

文档说以下类型是有效的,

变量和函数类型有某些定义,它们在整个内核和驱动程序中使用。它们可在 vos.h 头文件中供应用程序使用。

空指针和逻辑定义:

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

变量类型定义:

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

函数类型定义:

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);

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞美少年 2024-10-14 18:48:43

使用 snprintf()

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

鉴于新信息表明它是一个相当基本的嵌入式系统,那么如果您只对 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';
}

应该清楚如何将其扩展到更广泛的数字)。

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).

胡大本事 2024-10-14 18:48:43

尝试sprintf

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

它比caf的答案安全性低,但如果你有stdio,应该可以工作。因此,您必须确保输出缓冲区足够大以容纳生成的字符串。

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.

孤凫 2024-10-14 18:48:43

像这样的东西应该可以做到:

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;
    }
}

但是你说你有 stdio 可用,所以不需要自己写这样的东西。

编辑:可能是编译器需要 K&R 风格原型:

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

Codepad 上尝试一下。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文