String to binary converter
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I did research about binary counting, and wanted to make an application in Java that converts a string to binary code:
/**
* This class converts strings by looping through the characters, gets the
* decimal code for each, then divides the number by 2 untill the quotient is 0
*
* @author Mr Pro Pop
*
*/
public class Binary
/**
* This function takes a string as a parameter and converts it to binary code
*
* @param string The string to be converted to binary code
* @return The binary code of the input string
*/
public String getBinaryCode(String string)
if (string.length() == 0)
return null;
char characters = string.toCharArray();
StringBuilder result = new StringBuilder();
int number = 0;
for (int i = 0; i < characters.length; i++)
number = getDecimal(characters[i]);
for (int j = 0; j < 8; j++)
if (number % 2 == 0)
result.insert(i, 0);
else
result.insert(i, 1);
number = (int) (number / 2);
result.append(" ");
return result.toString();
/**
* This function gets the decimal number of a character
*
* @param character The character that we want the decimal value of
* @return The decimal of the character from the ascii table
*/
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
public static void main(String args)
String string = "a";
Binary b = new Binary();
System.out.println("Binary code for" + string + " is " + b.getBinaryCode(string));
Output:
Binary code for a is 01100001
Here it works well, however, spacing doesn't work. I do acknowledge that I could simply code this in a different easier method by Integer.toBinaryString(character[i])
but tried doing it my way to learn and especially with this method.
Any improvements I could do or anything to pay attention for?
java reinventing-the-wheel number-systems
add a comment |Â
up vote
1
down vote
favorite
I did research about binary counting, and wanted to make an application in Java that converts a string to binary code:
/**
* This class converts strings by looping through the characters, gets the
* decimal code for each, then divides the number by 2 untill the quotient is 0
*
* @author Mr Pro Pop
*
*/
public class Binary
/**
* This function takes a string as a parameter and converts it to binary code
*
* @param string The string to be converted to binary code
* @return The binary code of the input string
*/
public String getBinaryCode(String string)
if (string.length() == 0)
return null;
char characters = string.toCharArray();
StringBuilder result = new StringBuilder();
int number = 0;
for (int i = 0; i < characters.length; i++)
number = getDecimal(characters[i]);
for (int j = 0; j < 8; j++)
if (number % 2 == 0)
result.insert(i, 0);
else
result.insert(i, 1);
number = (int) (number / 2);
result.append(" ");
return result.toString();
/**
* This function gets the decimal number of a character
*
* @param character The character that we want the decimal value of
* @return The decimal of the character from the ascii table
*/
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
public static void main(String args)
String string = "a";
Binary b = new Binary();
System.out.println("Binary code for" + string + " is " + b.getBinaryCode(string));
Output:
Binary code for a is 01100001
Here it works well, however, spacing doesn't work. I do acknowledge that I could simply code this in a different easier method by Integer.toBinaryString(character[i])
but tried doing it my way to learn and especially with this method.
Any improvements I could do or anything to pay attention for?
java reinventing-the-wheel number-systems
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I did research about binary counting, and wanted to make an application in Java that converts a string to binary code:
/**
* This class converts strings by looping through the characters, gets the
* decimal code for each, then divides the number by 2 untill the quotient is 0
*
* @author Mr Pro Pop
*
*/
public class Binary
/**
* This function takes a string as a parameter and converts it to binary code
*
* @param string The string to be converted to binary code
* @return The binary code of the input string
*/
public String getBinaryCode(String string)
if (string.length() == 0)
return null;
char characters = string.toCharArray();
StringBuilder result = new StringBuilder();
int number = 0;
for (int i = 0; i < characters.length; i++)
number = getDecimal(characters[i]);
for (int j = 0; j < 8; j++)
if (number % 2 == 0)
result.insert(i, 0);
else
result.insert(i, 1);
number = (int) (number / 2);
result.append(" ");
return result.toString();
/**
* This function gets the decimal number of a character
*
* @param character The character that we want the decimal value of
* @return The decimal of the character from the ascii table
*/
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
public static void main(String args)
String string = "a";
Binary b = new Binary();
System.out.println("Binary code for" + string + " is " + b.getBinaryCode(string));
Output:
Binary code for a is 01100001
Here it works well, however, spacing doesn't work. I do acknowledge that I could simply code this in a different easier method by Integer.toBinaryString(character[i])
but tried doing it my way to learn and especially with this method.
Any improvements I could do or anything to pay attention for?
java reinventing-the-wheel number-systems
I did research about binary counting, and wanted to make an application in Java that converts a string to binary code:
/**
* This class converts strings by looping through the characters, gets the
* decimal code for each, then divides the number by 2 untill the quotient is 0
*
* @author Mr Pro Pop
*
*/
public class Binary
/**
* This function takes a string as a parameter and converts it to binary code
*
* @param string The string to be converted to binary code
* @return The binary code of the input string
*/
public String getBinaryCode(String string)
if (string.length() == 0)
return null;
char characters = string.toCharArray();
StringBuilder result = new StringBuilder();
int number = 0;
for (int i = 0; i < characters.length; i++)
number = getDecimal(characters[i]);
for (int j = 0; j < 8; j++)
if (number % 2 == 0)
result.insert(i, 0);
else
result.insert(i, 1);
number = (int) (number / 2);
result.append(" ");
return result.toString();
/**
* This function gets the decimal number of a character
*
* @param character The character that we want the decimal value of
* @return The decimal of the character from the ascii table
*/
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
public static void main(String args)
String string = "a";
Binary b = new Binary();
System.out.println("Binary code for" + string + " is " + b.getBinaryCode(string));
Output:
Binary code for a is 01100001
Here it works well, however, spacing doesn't work. I do acknowledge that I could simply code this in a different easier method by Integer.toBinaryString(character[i])
but tried doing it my way to learn and especially with this method.
Any improvements I could do or anything to pay attention for?
java reinventing-the-wheel number-systems
java reinventing-the-wheel number-systems
edited 10 mins ago
200_success
124k14144401
124k14144401
asked 2 hours ago
Mr Pro Pop
1204
1204
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
I would change
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
To just
public int getDecimal(char character)
if(character<0
, As char
is a number, too.
Also try using >>
operator to get n
th binary digit, which means division by 2.
number = (int) (number / 2);
To
number = number >>1;
I don't know if it would be faster in java, but it works faster in old computer's assembly program(Division was expensive than just shifting)
If you use assembly you can access carry flags and optimize it.
For more information about it, refer here.
Provided that al
holds the target number and bx
holds the target string address, and cx
holds 8,
L1: ; This is the loop
mov dl, '0' ; Ascii character zero
shl al, 1 ; Upper bit now in carry flag
adc dl, 0 ; Adds carry flag - 0 or 1
mov [bx], dl ; Save digit to current position
inc bx ; Next position
loop L1 ; Counts down cx
mov [bx], 0 ; Zero terminate (might need to use register)
New contributor
Oh thanks!!! Changed the getDecimal method. One thing I didn't quite get is the >> operator, what do you mean? And also one more question please, why is my append(" ") putting space after the result? So say I pass "sup" it would give the binary code of each character not separated and at the end put 3 spaces.
â Mr Pro Pop
1 hour ago
@MrProPop Maybe because of the insert method?
â KYHSGeekCode
1 hour ago
Aha, thanks for the tip. Btw, yes your right, the problem is because of insert, but if I don't use insert, then I would get the binary code the other way round, like flipped. Do I run a for loop to order them back? Just thought of it would consume more resources considering a large string looping through each character.
â Mr Pro Pop
1 hour ago
1
@MrProPop Maybe you can do that. Or if you use assembly language you can access carry flag which is set when it is shifted in certain conditions like the ooerand is even or its highst bit is 1, etc. Then you can naturally find bits from its highest bits.
â KYHSGeekCode
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
I would change
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
To just
public int getDecimal(char character)
if(character<0
, As char
is a number, too.
Also try using >>
operator to get n
th binary digit, which means division by 2.
number = (int) (number / 2);
To
number = number >>1;
I don't know if it would be faster in java, but it works faster in old computer's assembly program(Division was expensive than just shifting)
If you use assembly you can access carry flags and optimize it.
For more information about it, refer here.
Provided that al
holds the target number and bx
holds the target string address, and cx
holds 8,
L1: ; This is the loop
mov dl, '0' ; Ascii character zero
shl al, 1 ; Upper bit now in carry flag
adc dl, 0 ; Adds carry flag - 0 or 1
mov [bx], dl ; Save digit to current position
inc bx ; Next position
loop L1 ; Counts down cx
mov [bx], 0 ; Zero terminate (might need to use register)
New contributor
Oh thanks!!! Changed the getDecimal method. One thing I didn't quite get is the >> operator, what do you mean? And also one more question please, why is my append(" ") putting space after the result? So say I pass "sup" it would give the binary code of each character not separated and at the end put 3 spaces.
â Mr Pro Pop
1 hour ago
@MrProPop Maybe because of the insert method?
â KYHSGeekCode
1 hour ago
Aha, thanks for the tip. Btw, yes your right, the problem is because of insert, but if I don't use insert, then I would get the binary code the other way round, like flipped. Do I run a for loop to order them back? Just thought of it would consume more resources considering a large string looping through each character.
â Mr Pro Pop
1 hour ago
1
@MrProPop Maybe you can do that. Or if you use assembly language you can access carry flag which is set when it is shifted in certain conditions like the ooerand is even or its highst bit is 1, etc. Then you can naturally find bits from its highest bits.
â KYHSGeekCode
1 hour ago
add a comment |Â
up vote
3
down vote
accepted
I would change
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
To just
public int getDecimal(char character)
if(character<0
, As char
is a number, too.
Also try using >>
operator to get n
th binary digit, which means division by 2.
number = (int) (number / 2);
To
number = number >>1;
I don't know if it would be faster in java, but it works faster in old computer's assembly program(Division was expensive than just shifting)
If you use assembly you can access carry flags and optimize it.
For more information about it, refer here.
Provided that al
holds the target number and bx
holds the target string address, and cx
holds 8,
L1: ; This is the loop
mov dl, '0' ; Ascii character zero
shl al, 1 ; Upper bit now in carry flag
adc dl, 0 ; Adds carry flag - 0 or 1
mov [bx], dl ; Save digit to current position
inc bx ; Next position
loop L1 ; Counts down cx
mov [bx], 0 ; Zero terminate (might need to use register)
New contributor
Oh thanks!!! Changed the getDecimal method. One thing I didn't quite get is the >> operator, what do you mean? And also one more question please, why is my append(" ") putting space after the result? So say I pass "sup" it would give the binary code of each character not separated and at the end put 3 spaces.
â Mr Pro Pop
1 hour ago
@MrProPop Maybe because of the insert method?
â KYHSGeekCode
1 hour ago
Aha, thanks for the tip. Btw, yes your right, the problem is because of insert, but if I don't use insert, then I would get the binary code the other way round, like flipped. Do I run a for loop to order them back? Just thought of it would consume more resources considering a large string looping through each character.
â Mr Pro Pop
1 hour ago
1
@MrProPop Maybe you can do that. Or if you use assembly language you can access carry flag which is set when it is shifted in certain conditions like the ooerand is even or its highst bit is 1, etc. Then you can naturally find bits from its highest bits.
â KYHSGeekCode
1 hour ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
I would change
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
To just
public int getDecimal(char character)
if(character<0
, As char
is a number, too.
Also try using >>
operator to get n
th binary digit, which means division by 2.
number = (int) (number / 2);
To
number = number >>1;
I don't know if it would be faster in java, but it works faster in old computer's assembly program(Division was expensive than just shifting)
If you use assembly you can access carry flags and optimize it.
For more information about it, refer here.
Provided that al
holds the target number and bx
holds the target string address, and cx
holds 8,
L1: ; This is the loop
mov dl, '0' ; Ascii character zero
shl al, 1 ; Upper bit now in carry flag
adc dl, 0 ; Adds carry flag - 0 or 1
mov [bx], dl ; Save digit to current position
inc bx ; Next position
loop L1 ; Counts down cx
mov [bx], 0 ; Zero terminate (might need to use register)
New contributor
I would change
public int getDecimal(char character)
for (int i = 0; i <= 255; i++)
if (character == (char) i)
return i;
return -1;
To just
public int getDecimal(char character)
if(character<0
, As char
is a number, too.
Also try using >>
operator to get n
th binary digit, which means division by 2.
number = (int) (number / 2);
To
number = number >>1;
I don't know if it would be faster in java, but it works faster in old computer's assembly program(Division was expensive than just shifting)
If you use assembly you can access carry flags and optimize it.
For more information about it, refer here.
Provided that al
holds the target number and bx
holds the target string address, and cx
holds 8,
L1: ; This is the loop
mov dl, '0' ; Ascii character zero
shl al, 1 ; Upper bit now in carry flag
adc dl, 0 ; Adds carry flag - 0 or 1
mov [bx], dl ; Save digit to current position
inc bx ; Next position
loop L1 ; Counts down cx
mov [bx], 0 ; Zero terminate (might need to use register)
New contributor
edited 1 hour ago
New contributor
answered 1 hour ago
KYHSGeekCode
1464
1464
New contributor
New contributor
Oh thanks!!! Changed the getDecimal method. One thing I didn't quite get is the >> operator, what do you mean? And also one more question please, why is my append(" ") putting space after the result? So say I pass "sup" it would give the binary code of each character not separated and at the end put 3 spaces.
â Mr Pro Pop
1 hour ago
@MrProPop Maybe because of the insert method?
â KYHSGeekCode
1 hour ago
Aha, thanks for the tip. Btw, yes your right, the problem is because of insert, but if I don't use insert, then I would get the binary code the other way round, like flipped. Do I run a for loop to order them back? Just thought of it would consume more resources considering a large string looping through each character.
â Mr Pro Pop
1 hour ago
1
@MrProPop Maybe you can do that. Or if you use assembly language you can access carry flag which is set when it is shifted in certain conditions like the ooerand is even or its highst bit is 1, etc. Then you can naturally find bits from its highest bits.
â KYHSGeekCode
1 hour ago
add a comment |Â
Oh thanks!!! Changed the getDecimal method. One thing I didn't quite get is the >> operator, what do you mean? And also one more question please, why is my append(" ") putting space after the result? So say I pass "sup" it would give the binary code of each character not separated and at the end put 3 spaces.
â Mr Pro Pop
1 hour ago
@MrProPop Maybe because of the insert method?
â KYHSGeekCode
1 hour ago
Aha, thanks for the tip. Btw, yes your right, the problem is because of insert, but if I don't use insert, then I would get the binary code the other way round, like flipped. Do I run a for loop to order them back? Just thought of it would consume more resources considering a large string looping through each character.
â Mr Pro Pop
1 hour ago
1
@MrProPop Maybe you can do that. Or if you use assembly language you can access carry flag which is set when it is shifted in certain conditions like the ooerand is even or its highst bit is 1, etc. Then you can naturally find bits from its highest bits.
â KYHSGeekCode
1 hour ago
Oh thanks!!! Changed the getDecimal method. One thing I didn't quite get is the >> operator, what do you mean? And also one more question please, why is my append(" ") putting space after the result? So say I pass "sup" it would give the binary code of each character not separated and at the end put 3 spaces.
â Mr Pro Pop
1 hour ago
Oh thanks!!! Changed the getDecimal method. One thing I didn't quite get is the >> operator, what do you mean? And also one more question please, why is my append(" ") putting space after the result? So say I pass "sup" it would give the binary code of each character not separated and at the end put 3 spaces.
â Mr Pro Pop
1 hour ago
@MrProPop Maybe because of the insert method?
â KYHSGeekCode
1 hour ago
@MrProPop Maybe because of the insert method?
â KYHSGeekCode
1 hour ago
Aha, thanks for the tip. Btw, yes your right, the problem is because of insert, but if I don't use insert, then I would get the binary code the other way round, like flipped. Do I run a for loop to order them back? Just thought of it would consume more resources considering a large string looping through each character.
â Mr Pro Pop
1 hour ago
Aha, thanks for the tip. Btw, yes your right, the problem is because of insert, but if I don't use insert, then I would get the binary code the other way round, like flipped. Do I run a for loop to order them back? Just thought of it would consume more resources considering a large string looping through each character.
â Mr Pro Pop
1 hour ago
1
1
@MrProPop Maybe you can do that. Or if you use assembly language you can access carry flag which is set when it is shifted in certain conditions like the ooerand is even or its highst bit is 1, etc. Then you can naturally find bits from its highest bits.
â KYHSGeekCode
1 hour ago
@MrProPop Maybe you can do that. Or if you use assembly language you can access carry flag which is set when it is shifted in certain conditions like the ooerand is even or its highst bit is 1, etc. Then you can naturally find bits from its highest bits.
â KYHSGeekCode
1 hour ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f204212%2fstring-to-binary-converter%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password