-4
1.char chCharacter = 'A';  
2.int iBinaryCode;   
  1. Storing the character in a char
  2. Where I want to store the binary value
red_star_12
  • 37
  • 1
  • 8
  • 1
    When you say "ASCII" do you mean that `chCharacter` will only be assigned values that the Unicode character set has in common with the ASCII characters set? (`char` is a UTF-16 code unit. UTF-16 is one of several encodings for the Unicode character set.) – Tom Blodget Apr 17 '17 at 16:41
  • 2
    Prefixing a variable name with letters indicating its type is known as Systems Hungarian notation, and it is highly frowned upon. See http://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation . – VGR Apr 17 '17 at 16:54
  • When I say "ASCII" I mean only ASCII. I am not sure how to explain it but if it still does not make sense look at this link.https://www.google.co.uk/search?q=basic+ascii&espv=2&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjxwOD1-6vTAhVHDcAKHaqNCtYQ_AUIBigB&biw=1280&bih=830#tbm=isch&q=ascii+table – red_star_12 Apr 17 '17 at 17:04
  • @VGR Thanks for letting me know. – red_star_12 Apr 17 '17 at 17:05
  • Well, I guess you are in luck because the designers of Unicode thoughtfully made Unicode a superset of ASCII and mapped the characters in common to the same codepoints and the designers of UTF-16 thoughtfully made those codepoints a single UTF-16 code unit and the designers of Java thoughtfully made `char` a UTF-16 code unit (eventually). (This is the same for .NET, VB6, etc.) So, if all your `char` values are [C0 Controls and Basic Latin](http://www.unicode.org/charts/nameslist/index.html), you can convert them directly to integers that are the same value as if they were ASCII. – Tom Blodget Apr 17 '17 at 17:56
  • @Tom Blodget, thanks for that information it helps a lot :-) – red_star_12 Apr 18 '17 at 17:07

1 Answers1

4

You can get a String with its binary value as follows

Integer a = Character.getNumericValue(chCharacter);
String binary = Integer.toBinaryString(a)

EDIT: and get it as an Integer doing:

Integer binaryInteger = Integer.valueOf(binary);
Aldeguer
  • 821
  • 9
  • 32
  • 1
    I don't think you want to use [getNumericValue](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#getNumericValue-char-) ("For example, the character '\u216C' (the roman numeral fifty) will return an int with a value of 50.") – Tom Blodget Apr 17 '17 at 16:39
  • Thanks but what does "Integer.valueOf(binary);" do? – red_star_12 Apr 17 '17 at 17:08
  • Java docs will explain it much better than me https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(java.lang.String) – Aldeguer Apr 17 '17 at 17:10
  • 1
    @Aldeguer, thanks that document is very helpful :-) – red_star_12 Apr 18 '17 at 17:06