0

char array gives inappropriate and unworthy output. Here I was making a binary converter everything is well but the output like this

'a ■wô▀╓vè▄╨ 0000 0000 0000 0101'

Don't know why but I just want simply '0000 0000 0000 0101'. Can you tell me where I'm wrong?

int number = 5;

int temp_num, remain, quotient;
char result[40];

// *Pointers
char *result_ptr = result;

int output_size = 16;

temp_num = number;
for (int  i = 0; i < output_size; i++)
{
    remain = temp_num % 2;
    quotient = temp_num / 2;

    if(remain == 0){*result_ptr++ = '\x30';}
    else if(remain == 1){*result_ptr++ = '\x31';}
    if((i + 1) % 4 == 0){*result_ptr++ = '\x20';} // separate every 4 bits

    temp_num = quotient;
}
strrev(result);
puts(result);

return 0;
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
  • 4
    For one thing, you probably need to null-terminate your constructed string. – Steve Summit Jun 26 '22 at 14:48
  • 1
    There's a much easier way to construct your string than with those cryptic hexadecimal constants. I would use things like `*result_ptr++ = '1';` and `*result_ptr++ = ' ';`. – Steve Summit Jun 26 '22 at 14:49
  • @SteveSummit But I'm still gettting this 'a ■wô▀╓vè▄╨ 0000 0000 0000 0101' ?? and I have no idea ! – SUD pythonism Jun 26 '22 at 14:53
  • 1
    Adding three characters to your code will fix it: `char result[40];` -> `char result[40]="";`. As it stands, your `result` array doesn't have a guaranteed `nul`-terminator. (Initialising to an empty string will set **all** elements to zero.) – Adrian Mole Jun 26 '22 at 14:55
  • Use `'0'`, `'1'` and `' '` rather than the obfuscatory `'\x30'`, `'\x31'` and `'\x20'`. – Jonathan Leffler Jun 26 '22 at 14:55
  • Add `*result_ptr = '\0';` after the loop and before the call to `strrev()`. You haven't null-terminated your string, so you you get undefined behaviour when you pass it to a function that expects a null-terminated string. – Jonathan Leffler Jun 26 '22 at 14:58
  • Thanks @AdrianMole initializing an empty string to "result" solve my problem. – SUD pythonism Jun 26 '22 at 15:01
  • @JonathanLeffler it works even I write values in hex or decimal. Thanks – SUD pythonism Jun 26 '22 at 15:02
  • I didn't say "they don't work". They do, but the conceal the meaning of the code — and it is better to use clearer values. You'd use `'\x41'` instead of `'A'`, too, no doubt. Don't! ***Don't!*** – Jonathan Leffler Jun 26 '22 at 15:27
  • 1
    @SUDpythonism One of the prices you have to pay for the free help you get here on Stack Overflow is that we tend to comment on *all* aspects of your code, not just the part that overtly caused your problem. Your problem was caused by the missing null termination. But those hexadecimal constants are, in our opinion, a really bad idea. Presumably you were using them because something had given you the idea that they were a good way to do it. We wanted to correct that misinformation for you. – Steve Summit Jun 26 '22 at 15:45
  • 1
    Having code that works is only part of the problem. You also need code that other people can read and make sense of, so that they can maintain it. – Steve Summit Jun 26 '22 at 15:45

2 Answers2

0

Your string is not terminated by '\0'. As a result, strrev seems to do funny things, it reverses too long string (or whatever, this is likely to be undefined behavior due to buffer overflow).

Simplest fix is to initialize your string to all zeroes:

char result[40] = "";

The buffer length 40 is just big enough even for 32 bit integer, I think, 32 bits, 7 spaces and final 0 to terminate the string. However, you might want to add assert or something to make sure you don't have buffer overflow.

hyde
  • 60,639
  • 21
  • 115
  • 176
0

If you add the string terminator at output_size+(output_size/4)-1 then you have the right output size to end as your output size only goes up to 16 which in your number (0000 0000 0000 0101) is 0 0000 0000 0101 (a string with the length of 16) so if you add the output_size / 4 then you should get the actual size of the output and because it is binary the output size should always be a multiple of 4 and if not you could always add an if statement to fix it. The -1 is to remove the extra character as for your number (0000 0000 0000 0101) output_size+(output_size/4) is equal to 20 so you'd be assigning at the index of 20 so if you change it to -1 then you are accounting for the 0 index too.

    int number = 5;

    int temp_num, remain, quotient;
    char result[40];

    // *Pointers
    char *result_ptr = result;

    int output_size = 16;

    temp_num = number;

    for (int  i = 0; i < output_size; i++)
    {
        remain = temp_num % 2;
        quotient = temp_num / 2;

        if(remain == 0){*result_ptr++ = '\x30';}
        else if(remain == 1){*result_ptr++ = '\x31';}
        if((i + 1) % 4 == 0){*result_ptr++ = '\x20';} // separate every 4 bits

        temp_num = quotient;

        result[output_size+(output_size/4)-1] = '\0';
    }

    puts(result);
    strrev(result);
    puts(result);

Hope it helps

Electric fox
  • 35
  • 1
  • 8