Convert a String (Like Testing123) to Binary in Java

JAVA converting from String to Binary and then back to String in file

I didnt go into detail on how to read/write files, since you can look that up. However, to achieve what you want:

  • you will need a method to convert a word to binary
  • then you will need another method to convert from binary to words

below is the code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HelloWorld{

public static void main(String []args) throws FileNotFoundException {
File file = new File("input.txt");
Scanner sc = new Scanner(file);
String word=sc.nextLine();
String receivedBinary=stringToBinary(word);
System.out.println();
String receivedWord=BinaryToString(receivedBinary);
}
public static String stringToBinary(String text)
{
String bString="";
String temp="";
for(int i=0;i<text.length();i++)
{
temp=Integer.toBinaryString(text.charAt(i));
for(int j=temp.length();j<8;j++)
{
temp="0"+temp;
}
bString+=temp+" ";
}

System.out.println(bString);
return bString;
}
public static String BinaryToString(String binaryCode)
{
String[] code = binaryCode.split(" ");
String word="";
for(int i=0;i<code.length;i++)
{
word+= (char)Integer.parseInt(code[i],2);
}
System.out.println(word);
return word;
}
}

How can I convert the string value to binary value in java

128 is 10000000 and & is bit sum operator - everything but 8th bit in val will be zeroed so effectively you will fetch the 8th bit of the number so for the val like 01111111 it will be

  01111111
& 10000000
--------
00000000

and the line will return 0

How to convert (English) String to binary form using java?

 String firstblock = “If debugging is thae process of removing software bugs, then programming must be the process of putting them in. Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”;
byte[] bytes = firstblock.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binary.append(' ');
}
System.out.println(binary);

and you can check it binary to string

How to convert string to binary representation in game maker?

Tip: search for GML or other language term, these questions answered many times. Also please check your tag as it is the IDE tag, not language tag.

Im not familiar with GML myself, but a quick search showed this:

At least semi-official method for exactly this: http://www.gmlscripts.com/script/bytes_to_bin

/// bytes_to_bin(str)
//
// Returns a string of binary digits, 1 bit each.
//
// str raw bytes, 8 bits each, string
//
/// GMLscripts.com/license
{
var str, bin, p, byte;
str = argument0;
bin = "";
p = string_length(str);
repeat (p) {
byte = ord(string_char_at(str,p));
repeat (8) {
if (byte & 1) bin = "1" + bin else bin = "0" + bin;
byte = byte >> 1;
}
p -= 1;
}
return bin;
}

GML forum (has several examples) https://www.reddit.com/r/gamemaker/comments/4opzhu/how_could_i_convert_a_string_to_binary/

///string_to_binary(string)
var str = argument0;
var output = "";
for(var i = 0; i < string_length(str); i++){
if(string_char_at(str, i + 1) == "0"){
output += "0";
}
else{
output += "1";
}
}
return real(output);


And other language examples:

C++ Fastest way to Convert String to Binary?

#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main(){
string myString = "Hello World";
for (std::size_t i = 0; i < myString.size(); ++i)
{
cout << bitset<8>(myString.c_str()[i]) << endl;
}
}

Java: Convert A String (like testing123) To Binary In Java

  String s = "foo";
byte[] bytes = s.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binary.append(' ');
}
System.out.println("'" + s + "' to binary: " + binary);

JS: How to convert text to binary code in JavaScript?

function convert() {
var output = document.getElementById("ti2");
var input = document.getElementById("ti1").value;
output.value = "";
for (var i = 0; i < input.length; i++) {
output.value += input[i].charCodeAt(0).toString(2) + " ";
}
}


Related Topics



Leave a reply



Submit