-2

Can anyone show me a good method to express a character in binary in C? I would need to see a given character as a sequence of 1 and 0. For example, if I get as an input 'a', I would like to see it as 01100001 and finally return 00010110 (a byte with inverted nibbles). Is it good to use unigned char? Should I use an 8-element array of integers to store a byte?

int main(void){
   unsigned char c = 'a';

   return 0;
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format – Eraklon Feb 11 '20 at 10:21
  • Please [edit] your question and add more details. With "character", do you mean an ASCII character or does this term include other characters that may use single-byte codes with bit 7 set or multi-byte codes? What exactly do you mean with "I would like to see it as 01100001" do you mean output in binary format (like `printf`) or do you mean conversion to a string? `unsigned char` can be used for operations like bit shifting, masking and combining. – Bodo Feb 11 '20 at 10:26
  • @Peter Allen - How would you judge which method is _the best method_? – Armali Feb 11 '20 at 10:30
  • 1
    @Armali "Perfection is achieved, not when there is nothing more to add, but when there is nothing more to take away." - Antoine de Saint-Exupéry – Peter Allen Feb 11 '20 at 10:32
  • 2
    [Is there a printf converter to print in binary format?](https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format): 57 answers to pick "the best" from, enough to fulfill anyone's personal criteria. – Jongware Feb 11 '20 at 10:33
  • @usr2564301 No, it is the same proposed by Eraklon. I'm just looking for a type to express a binary variable. I'm not looking for printing. – Peter Allen Feb 11 '20 at 10:35
  • 1
    Asking for the *best* method is generally frowned upon here, because it involves a lot of opinion. Asking for a *good* method or a method conforming to best pratices would be better. As far as I am concerned, I would never answer a question asking for the best method even I know a good one, because I am pretty sure that it could be improved... – Serge Ballesta Feb 11 '20 at 10:37
  • @SergeBallesta You are right. Just edited. – Peter Allen Feb 11 '20 at 10:38
  • @Peter Allen - If you're _not looking for printing_, what do you mean by _I would need to see a given character as a sequence of 1 and 0_? And what do you mean by _a binary variable_, given that most probably every object within your computer is represented in binary bits? – Armali Feb 11 '20 at 10:39
  • @Armali I was asking which type should I use to store an input as a variable in order to do operations (such as inverting nibbles) on his bits. Should I use an array of integers? – Peter Allen Feb 11 '20 at 10:42
  • Actually a `char` ist just another type of integer. – the busybee Feb 11 '20 at 10:46
  • There is an answer for every question you present here already on the Stack Overflow. – Antti Haapala -- Слава Україні Feb 11 '20 at 10:47
  • `unsigned char` shall be able to represent any character of the basic execution character set and is shift friendly, so it is an appropriate type for bitwise manipulations on characters. – Serge Ballesta Feb 11 '20 at 10:54

1 Answers1

0

a byte with inverted nibbles

Since you seem to not care for characters with other than 8 bits, you could do c = c<<4|c>>4.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • My question was about the type to use, not about the operation. – Peter Allen Feb 11 '20 at 10:45
  • The question about the type has already been answered by Bodo in his comment and the answer incorporated in your post by yourself: `unsigned char`. – Armali Feb 11 '20 at 10:50