-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBinaryModular.java
More file actions
82 lines (72 loc) · 3.4 KB
/
BinaryModular.java
File metadata and controls
82 lines (72 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.Scanner;
public class BinaryModular {
// Converts a string to its binary representation
static String wordToBinary(String s) {
StringBuilder binaryRepresentation = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
int asciiValue = (int) s.charAt(i);
String binary = Integer.toBinaryString(asciiValue); // Convert ASCII value to binary
binaryRepresentation.append(binary).append(" ");
}
return binaryRepresentation.toString().trim();
}
// Converts a number to its binary representation
static String numberToBinary(long s) {
return Long.toBinaryString(s); // Directly convert number to binary
}
// Converts binary representation back to a string
static String binaryToWord(String binary) {
StringBuilder text = new StringBuilder();
for (String bin : binary.split(" ")) {
int asciiValue = Integer.parseInt(bin, 2); // Parse binary to integer
text.append((char) asciiValue); // Convert ASCII value to character
}
return text.toString();
}
// Converts binary representation back to a number
static long binaryToNumber(String binary) {
return Long.parseLong(binary, 2); // Parse binary to long
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose an option: ");
System.out.println("1. Encode a message to binary");
System.out.println("2. Decode a binary message");
int choice = sc.nextInt();
sc.nextLine(); // Consume the newline character
if (choice == 1) {
// Encoding
System.out.println("Enter the message: ");
String s = sc.nextLine(); // Read the entire line
StringBuilder message = new StringBuilder();
for (String token : s.split("\\s+")) { // Split input by spaces
if (token.matches("\\d+")) { // Check if the token is a number
long num = Long.parseLong(token);
message.append(numberToBinary(num)).append(" ");
} else {
message.append(wordToBinary(token)).append(" ");
}
}
System.out.println("Binary Encoded Message: " + message.toString().trim());
} else if (choice == 2) {
// Decoding
System.out.println("Enter the binary message to decode: ");
String binaryMessage = sc.nextLine();
StringBuilder decodedMessage = new StringBuilder();
for (String token : binaryMessage.split("\\s+")) { // Split input by spaces
if (token.matches("[01]+")) { // Check if the token is binary
try {
// Decode as a character
decodedMessage.append(binaryToWord(token));
} catch (NumberFormatException e) {
System.out.println("Invalid binary input: " + token);
}
}
decodedMessage.append(" "); // Preserve spaces between words
}
System.out.println("Decoded Message: " + decodedMessage.toString().trim());
} else {
System.out.println("Invalid choice. Please restart the program.");
}
}
}