在C中将int转换为ASCII字符

时间:2017-12-29 16:55:25

标签: c converter

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

char buff[10];

让我们说:

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

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

类似于here

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

3 个答案:

答案 0 :(得分:4)

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

假设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,而是先删除5DC。确保根据C字符串的要求使用空终止符结束数组。

这个答案假设是大端排序,因为那是你在例子中指出的内容。如果你想要little-endian,只需在将它们放入数组时反转字节的顺序。

请注意,这会将05变为DC5D。如果您想要int,则应检查原始0中的第一个数字是否为&。您可以使用int运算符执行此操作,针对0xf00000000x00f00000等对0进行测试。如果您发现第一个数字为int,则转移在从中提取字节之前,将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 } 向右移4位。

所以,像这样:

uint8_t buf[10];

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

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

现在让我们进行一次旋转:

ABCD
ABC

和输出:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Test</title>
<style>

* {
    margin: 0;
    padding: 20px;
    border: none;
    }

p {
    color: black;
    }

body.state-2 p {
    color: red;
    }

h2 {
    position: static;
    }

@media only screen and (max-width: 1250px) {

h2 {
    position: relative;
    }

}

</style>

<body>

<h2>Headline</h2>

<p>Hello</p>

<script>

function toggleState()
{
    var toggle = document.querySelector('h2');
    var container = document.querySelector('body');
    if (window.getComputedStyle(toggle,null).getPropertyValue('position') == 'relative')
    {
        toggle.setAttribute('role', 'button');
    }
    else
    {
        toggle.removeAttribute('role');
    }
    toggle.addEventListener('click', function()
    {
        container.classList.toggle('state-2');
    }
    );
}

toggleState();
window.onload = toggleState;
window.onresize = toggleState;

</script>

答案 1 :(得分:1)

用C语言将整数值转换为ASCII字符?...

参考 ASCII table ,C中']'的值将始终被解释为0x5D或十进制值93.而C中的“]”的值将始终被解释为NULL终止的char数组,即由值组成的字符串表示:

|93|\0|  

(如 This Answer 所示,类似的解释对所有ASCII字符都有效。)

要将任何整数(char)值转换为看起来像“]”的值,可以使用字符串函数将char值转换为字符串表示形式。例如,所有这些变体都将执行该转换:

char strChar[2] = {0};

sprintf(strChar, "%c", ']'); 
sprintf(strChar, "%c", 0x5D); 
sprintf(strChar, "%c", 93);  

并且每个产生相同的C字符串:"]"

我想将字符分配给字符数组 ...

如何创建char数组的示例,以NULL char结尾,例如“ABC ... Z”:

int i;
char strArray[27] = {0};
for(i=0;i<26;i++)
{
     strArray[i] = i+'A';
}
strArray[i] = 0;
printf("Null terminated array of char: %s\n", strArray);

答案 2 :(得分:0)

unsigned u = ...;

if (0x10 > u)
  exit(EXIT_FAILURE);

while (0x10000 < u) u /= 2;
while (0x1000 > u) u *= 2;

char c[2] = {u / 0x100, u % 0x100);