0

I am trying to convert a character to its binary using inbuilt library (itoa) in C(gcc v-5.1) using example from Conversion of Char to Binary in C , but i'm getting a 7-bit output for a character input to itoa function.But since a character in C is essentially an 8-bit integer, i should get an 8-bit output.Can anyone explain why is it so??

code performing binary conversion:

enter for (temp=pt;*temp;temp++)
{
    itoa(*temp,opt,2);        //convert to binary
    printf("%s \n",opt);            
    strcat(s1,opt);           //add to encrypted text
}

PS:- This is my first question in stackoverflow.com, so sorry for any mistakes in advance.

Community
  • 1
  • 1
harry
  • 11
  • 3
  • 2
    What do you mean by "getting a 7-bit output"? – a3f Jun 25 '16 at 20:44
  • 2
    Suppressed leading zeros? How many leading zeros would you expect if you asked for radix 10? Anyway, the function argument is `int` not `char`, so your arguemnt is promoted to `int`. The function will not know or care how many leading zeros you expected. – Weather Vane Jun 25 '16 at 20:57
  • Even if `itoa()` did print leading zeros to match the size of the type, it'd be more than 8 binary digits anyway... since it takes an `int`. – Dmitri Jun 25 '16 at 21:04
  • @Dmitri even if it is using more than 8 binary digits ,the array 'op' in the code has length 8,so the last 8 bits(counting from LSB) would contain an added 0,which it doesnt.. – harry Jun 26 '16 at 09:02
  • @harry but the `itoa()` function can't tell how long the array is, and will happily write past the end of it if it's not large enough for all the digits and maybe cause a crash or corrupt the stack. If you only leave room for 8 digits, you need to make sure whatever number you pass it won't need more (fortunately, it doesn't actually add leading zeros -- since `itoa()` can't tell your number came from a `char` instead of an `int`, or that your buffer is only big enough for 8 digits). Also, a signed `char` with the high bit set would be a negative number, with at most 7 significant digits. – Dmitri Jun 26 '16 at 09:31
  • Also, the end of a string is marked by the null terminator at the end -- it doesn't have to fill the whole array it's stored in. A buffer big enough for 8 digits can still hold a string with fewer. – Dmitri Jun 26 '16 at 09:46

3 Answers3

0

The function itoa takes an int argument for the value to be converted. If you pass it a value of char type it will be promoted to int. So how would the function know how many leading zeros you were expecting? And then if you had asked for radix 10 how many leading zeros would you expect? Actually, it suppresses leading zeros.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

See ASCII Table, note the hex column and that for the ASCII characters the msb is 0. Printable ASCII characters range from 0x20 thru 0x7f. Unicode shares the characters 0x00 thru 0x7f.

Hex 20 thru 7f are binary 00100000 thru 01111111.

Not all binary values are printable characters and in some encodings are not legal values.

ASCII, hexadecimal, octal and binary are just ways of representing binary values. Printable characters are another way but not all binary values can be displayed, this is the main data that needs to be displayed or treated as character text is generally converted to hex-ascii or Base64.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • if someone wants to work on the binary values? I am actually trying to implement DES algorithm.so to cover all characters i need to use unicode encoding in C.any ideas? – harry Jun 26 '16 at 08:56
0

You could use printf( "%2X\n", *opt ); to print a 8-bit value as 2 hexadecimal symbols.

It would print the first char of opt. Then, you must increment the pointer to the next char with opt++;.

The X means you want it to be printed as uppercase hexadecimal characters (use x for lowercase) and the 2 will make sure it will print 2 symbols even if opt is lesser than 0x10.
In other words, the value 0xF will be printed 0x0F... (actually 0F, you could use printf( "%#2X\n", *opt ); to print the 0x).

If you absolutely want a binary value you have to make a function that will print the right 0 and 1. There are many of them on the internet. If you want to make yours, reading about bitwise operations could help you (you have to know about bitwise operations if you want to work with binaries anyways).

Now that you can print it as desired (hex as above or with your own binary function), you can redirect the output of printf with the sprintf function.

Its prototype is int sprintf( char* str, const char* format, ... ). str is the destination.

In your case, you will just need to replace the strcat line with something like sprintf( s1, "%2X\n", *opt); or sprintf( s1, "%s\n", your_binary_conversion_function(opt) );.

Note that using the former, you would have to increment s1 by 2 for each char in opt because one 8-bit value is 2 hexadecimal symbols.

You might also have to manage s1's memory by yourself, if it was not the case before.

Sources :
MK27's second answer
sprintf prototype

DRz
  • 1,078
  • 1
  • 11
  • 29