在C中将int转换为ASCII字符

标签 c converters

在C语言中如何将整数值转换为ASCII字符? 我想将字符分配给字符数组。

char buff[10];

假设我们有:

int = 93  (HEX: 5D) -> result should be - buff = {']'} 

int = 13398 (HEX: 3456) -> result should be buff = {'4', 'V'}

here做的类似

我不需要关心不可打印的字符。总会有可打印的字符。

最佳答案

只需使用位移位来获取各个字节。

假设 int 大小为 4 的架构:

int someInt = ...

uint8_t first = (someInt >> 24);
uint8_t second = (someInt >> 16);
uint8_t third = (someInt >> 8);
uint8_t fourth = someInt;

现在您可以将生成的字节放入数组中。确保首先检查 firstsecondthird 以确保它们不是 0,然后跳过它们如果他们是。确保按照 C 字符串的要求以空终止符结束数组。

这个答案假定大端排序,因为这是您在示例中指出的。如果你想要小尾数法,只需在将它们放入数组时反转字节的顺序即可。

请注意,这会将 5DC 变为 05DC。如果您想要 5D,您应该检查原始 int 中的第一个数字是否为 0。您可以使用 & 运算符来执行此操作,针对 0xf00000000x00f00000 等测试 int。如果您发现第一个数字是0,将int向右移动4位,然后从中提取字节。

所以,像这样:

void ExtractBytes(int anInt, uint8_t *buf, size_t bufSize) {
    // passing an empty buffer to this function would be stupid,
    // but hey, doesn't hurt to be idiot-proof
    if (bufSize == 0) { return; }

    // Get our sizes
    const int intSize = sizeof(anInt);
    const int digitCount = intSize * 2;

    // find first non-zero digit
    int firstNonZero = -1;
    for (int i = 0; i < digitCount; i++) {
        if ((anInt & (0xf << ((digitCount - 1 - i) * 4))) != 0) {
            firstNonZero = i;
            break;
        }
    }

    if (firstNonZero < 0) {
        // empty string; just bail out.
        buf[0] = 0;
        return;
    }

    // check whether first non-zero digit is even or odd;
    // shift if it's odd
    int intToUse = (firstNonZero % 2 != 0) ? (anInt >> 4) : anInt;

    // now, just extract our bytes to the buffer
    int bufPtr = 0;
    for (int i = intSize - 1; i >= 0; i--) {
        // shift over the appropriate amount, mask against 0xff
        uint8_t byte = (intToUse >> (i * 8));

        // If the byte is 0, we can just skip it
        if (byte == 0) {
            continue;
        }

        // always check to make sure we don't overflow our buffer.
        // if we're on the last byte, make it a null terminator and bail.
        if (bufPtr == bufSize - 1) {
            buf[bufPtr] = 0;
            return;
        }

        // Copy our byte into the buffer
        buf[bufPtr++] = byte;
    }

    // Now, just terminate our string.
    // We can be sure that bufPtr will be less than bufSize,
    // since we checked for that in the loop. So:
    buf[bufPtr] = 0;

    // Aaaaaand we're done
}

现在让我们试一试:

uint8_t buf[10];

ExtractBytes(0x41424344, buf, 10);
printf("%s\n", buf);

ExtractBytes(0x4142434, buf, 10);
printf("%s\n", buf);

和输出:

ABCD
ABC

关于在C中将int转换为ASCII字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48025801/

相关文章:

c - C 中的 union 示例

c - 使用 C 在 OpenMP 中执行后合并要打印的数据

c - 是否可以通过编程方式设置当前的 Linux 运行级别?

python - 如何解释 MIDI 文件分析的参数值,尤其是 "data"的 "midi.NoteOnEvent"字段?

java - 如何自动转换十六进制代码以将其用作 Java 中的 byte[]?

c - 将 Char 数组附加到 Char 指针

c - Define 宏中的两个 __pragma 意味着什么?

mysql - 从 csv 文件导入日期作为 MySQL 数据库中的 DATE 格式

c# - C# 中的 Des 和 3Des 以及 ecb(来自 C)

c# - 在 C# 中将总时间转换为分钟