0

I want to convert character type to binary but not char byte to bit.

I'll show you example.

if char = 'A' but actually it's hex! A = 1010 I have a character type but I want to represent it like hexadecimal.

Of course, I have only character data that can be matched hexadecimal. like 9, 8, A, C, D not 47, U, Y ..

And Can I count size or length of 1 in 1010 ?

If you know how to solve this please let me know thank you!

Junburg
  • 350
  • 5
  • 23
  • I'm voting to close this question as off-topic because it's a textbook example of "Do my thing for me" – Shark Jul 06 '18 at 09:59
  • 2
    Do you mean `Long.toString(Long.parseString(str, 16), 2)` ? – Peter Lawrey Jul 06 '18 at 10:00
  • 1
    This is a duplicate of [how to convert a char from alphabetical character to hexadecimal number in java](https://stackoverflow.com/questions/4477714/how-to-convert-a-char-from-alphabetical-character-to-hexadecimal-number-in-java) specifically "To read hex and convert to binary you can do..." (messed up my close vote...) – Michael Jul 06 '18 at 10:05
  • 2
    @PeterLawrey `Long.parseLong` – assylias Jul 06 '18 at 10:21
  • 1
    Please explain more clearly what you mean by _And Can I count size or length of 1 in 1010_? – OldCurmudgeon Jul 06 '18 at 10:27
  • 1
    Possible duplicate of: https://stackoverflow.com/questions/4477714/how-to-convert-a-char-from-alphabetical-character-to-hexadecimal-number-in-java – Mykyta Lebid Jul 06 '18 at 10:34
  • 2
    To "_count size or length of 1 in 1010_" see: [Count bits used in int](https://stackoverflow.com/questions/2935793/count-bits-used-in-int) – LuCio Jul 06 '18 at 10:37

3 Answers3

2

Simple Steps

Here you can convert any Alphabet to the binary number

System.out.println(Long.toString(Long.parseLong("A", 16), 2)); //Output 1010

Thanks

Faiz Akram
  • 559
  • 4
  • 10
1

You can most easily (and probably most efficiently) use Character.digit().

    int a = Character.digit('A', 16);
    System.out.println(Integer.toBinaryString(a));
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1

Since the input is a char, convert using the helper method from Character:

int num = Character.getNumericValue(ch);

Then convert it to a binary string:

String s = Integer.toBinaryString(num);
Andy Turner
  • 137,514
  • 11
  • 162
  • 243