将char转换为c中的二进制

发布于 2025-01-22 02:24:56 字数 328 浏览 2 评论 0原文

我试图将角色转换为其二进制表示(因此字符 - > ascii hex - >二进制)。

我知道要做,我需要移动和。但是,由于某种原因,我的代码无法正常工作。

这是我拥有的。 *temp指向C字符串中的索引。

char c;
int j;
for (j = i-1; j >= ptrPos; j--) {
    char x = *temp;
    c = (x >> i) & 1;
    printf("%d\n", c);
    temp--;
}

I am trying to convert a character to its binary representation (so character --> ascii hex --> binary).

I know to do that I need to shift and AND. However, my code is not working for some reason.

Here is what I have. *temp points to an index in a C string.

char c;
int j;
for (j = i-1; j >= ptrPos; j--) {
    char x = *temp;
    c = (x >> i) & 1;
    printf("%d\n", c);
    temp--;
}

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

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

发布评论

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

评论(3

且行且努力 2025-01-29 02:24:56

我们显示了两个将单个字符打印到二进制的功能。

void printbinchar(char character)
{
    char output[9];
    itoa(character, output, 2);
    printf("%s\n", output);
}

PrintBinchar(10)将写入控制台

    1010

ITOA是一个库函数,可将单个整数值转换为带有指定基础的字符串。
例如,ITOA(1341,输出,10)将在输出字符串“ 1341”中写入。
当然,ITOA(9,输出,2)将在输出字符串“ 1001”中写入。

下一个功能将打印到标准输出字符的完整二进制表示形式中,即,如果较高的位为零,它将打印所有8位。

void printbincharpad(char c)
{
    for (int i = 7; i >= 0; --i)
    {
        putchar( (c & (1 << i)) ? '1' : '0' );
    }
    putchar('\n');
}

printbincharpad(10)现在将写入控制台

    00001010

,现在我提出了一个打印整个字符串(没有最后一个null字符)的函数。

void printstringasbinary(char* s)
{
    // A small 9 characters buffer we use to perform the conversion
    char output[9];

    // Until the first character pointed by s is not a null character
    // that indicates end of string...
    while (*s)
    {
        // Convert the first character of the string to binary using itoa.
        // Characters in c are just 8 bit integers, at least, in noawdays computers.
        itoa(*s, output, 2);

        // print out our string and let's write a new line.
        puts(output);

        // we advance our string by one character,
        // If our original string was "ABC" now we are pointing at "BC".
        ++s;
    }
}

但是,请考虑ITOA不会添加填充零,因此PrintStringasbinary(“ AB1”)会打印出类似的内容:

1000001
1000010
110001

We show up two functions that prints a SINGLE character to binary.

void printbinchar(char character)
{
    char output[9];
    itoa(character, output, 2);
    printf("%s\n", output);
}

printbinchar(10) will write into the console

    1010

itoa is a library function that converts a single integer value to a string with the specified base.
For example... itoa(1341, output, 10) will write in output string "1341".
And of course itoa(9, output, 2) will write in the output string "1001".

The next function will print into the standard output the full binary representation of a character, that is, it will print all 8 bits, also if the higher bits are zero.

void printbincharpad(char c)
{
    for (int i = 7; i >= 0; --i)
    {
        putchar( (c & (1 << i)) ? '1' : '0' );
    }
    putchar('\n');
}

printbincharpad(10) will write into the console

    00001010

Now i present a function that prints out an entire string (without last null character).

void printstringasbinary(char* s)
{
    // A small 9 characters buffer we use to perform the conversion
    char output[9];

    // Until the first character pointed by s is not a null character
    // that indicates end of string...
    while (*s)
    {
        // Convert the first character of the string to binary using itoa.
        // Characters in c are just 8 bit integers, at least, in noawdays computers.
        itoa(*s, output, 2);

        // print out our string and let's write a new line.
        puts(output);

        // we advance our string by one character,
        // If our original string was "ABC" now we are pointing at "BC".
        ++s;
    }
}

Consider however that itoa don't adds padding zeroes, so printstringasbinary("AB1") will print something like:

1000001
1000010
110001
罪歌 2025-01-29 02:24:56
unsigned char c;

for( int i = 7; i >= 0; i-- ) {
    printf( "%d", ( c >> i ) & 1 ? 1 : 0 );
}

printf("\n");

说明:

在每次迭代时,通过将其移动并与1进行比较,从字节中读取了最重要的位


将其转移7将给出0000 0001,因此得出结论,最重要的位是1。0000 0001&amp; 1 = 1。这是第一个在控制台中打印的位。接下来的迭代将导致0 ... 0。

unsigned char c;

for( int i = 7; i >= 0; i-- ) {
    printf( "%d", ( c >> i ) & 1 ? 1 : 0 );
}

printf("\n");

Explanation:

With every iteration, the most significant bit is being read from the byte by shifting it and binary comparing with 1.

For example, let's assume that input value is 128, what binary translates to 1000 0000.
Shifting it by 7 will give 0000 0001, so it concludes that the most significant bit was 1. 0000 0001 & 1 = 1. That's the first bit to print in the console. Next iterations will result in 0 ... 0.

风轻花落早 2025-01-29 02:24:56

您的代码非常模糊,无法理解,但是我可以为您提供替代方案。

首先,如果您想要temp要浏览整个字符串,则可以执行类似的操作:

char *temp;
for (temp = your_string; *temp; ++temp)
    /* do something with *temp */

术语*temp作为的条件只需检查您是否已达到字符串的末端。如果有的话,*temp将为'\ 0'nul)和结束的

现在,在for内部,您想找到构成*temp的位。假设我们打印了一些位:

for (as above)
{
    int bit_index;
    for (bit_index = 7; bit_index >= 0; --bit_index)
    {
        int bit = *temp >> bit_index & 1;
        printf("%d", bit);
    }
    printf("\n");
}

要使它更通用,即将任何类型转换为位,您可以将bit_index = 7更改为bit_index = sizeof(*temp)* 8-1

Your code is very vague and not understandable, but I can provide you with an alternative.

First of all, if you want temp to go through the whole string, you can do something like this:

char *temp;
for (temp = your_string; *temp; ++temp)
    /* do something with *temp */

The term *temp as the for condition simply checks whether you have reached the end of the string or not. If you have, *temp will be '\0' (NUL) and the for ends.

Now, inside the for, you want to find the bits that compose *temp. Let's say we print the bits:

for (as above)
{
    int bit_index;
    for (bit_index = 7; bit_index >= 0; --bit_index)
    {
        int bit = *temp >> bit_index & 1;
        printf("%d", bit);
    }
    printf("\n");
}

To make it a bit more generic, that is to convert any type to bits, you can change the bit_index = 7 to bit_index = sizeof(*temp)*8-1

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