This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

How do I covert an int to a binary string?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method I am trying to create is: public static String IntToBinary(int inputInt).

This essentially should take an int as a single input , convert this int to a binary version of itself (e.g. 3 becomes “11”) and output that binary number as a string.

I have written some code but it honestly doesn't do what the question says i need to do.

Any help would be greatly appreciated.

What I have tried:

//This is the only way i know how to convert an it doesn't do what i need it to do in the question as i have to insert a single integer convert it to the binary and then make it output the string the method below doesn't do this



Comment
 
Rancher
Posts: 3989
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a challenge, because otherwise there is a toBinaryString static method on the Integer class.
It takes an int as a parameter and returns a String.
 
Kaid Elliott
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I wish I could just use that but for this question they specifically specify not use this. I have to essentially do the whole thing with loops and such.
 
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Look up standard techniques for converting decimal numbers to binary. You should find many links if you search. It is something like: if it ends 13579 (odd numbers) add a 1 to the number, otherwise (even numbers end 24680) add a 0. Halve the number and remember you are adding the new numbers to the left of the text.
It isn't an INT but an int, please, and remember that all numbers are stored in binary already. An odd number ends 1 and an even number 0 in binary. You might have to use different techniques if any of your arguments are negative because ints are stored in two's complement format and their leftmost digit is 1.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also here for the answer. Thanks for the question mate.
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MB: welcome to the Ranch

How far have you got?
 
Kaid Elliott
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I have not got far to be honest I am still trying to figure it out, also can someone tell me how to directly reply to people.
 
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Kaid,

if you have an array, say a{0, 1}, then "" + a[1] + a[0] would deliver the String "10". Your array might have a length that is not known beforehand, how would you tackle that problem?


Edit: assuming your problem is converting your array to a String....      
 
Kaid Elliott
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Kaid Daik wrote:also can someone tell me how to directly reply to people.


Kaid, either quote the relevant part of the post that you are replying to, or just name them in your reply, as I demonstrated in this reply.



Thanks for telling me appreciate it.
 
Kaid Elliott
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:@Kaid,

if you have an array, say a{0, 1}, then "" + a[1] + a[0] would deliver the String "10". Your array might have a length that is not known beforehand, how would you tackle that problem?


Edit: assuming your problem is converting your array to a String....      



The whole issue with my method is that the question wants me to input a single integer convert it then set the output to string. my method inputs a whole array which is an easy fix the only issue I am having is 1 converting to binary without using integer.parse and instead using for loops and then having the output return a string format.
 
Kaid Elliott
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:MB: welcome to the Ranch

How far have you got?



well ive changed it from being an array now I am trying to convert the input without using integer.parse and instead using for loops since I cant use the previous method. I am also not sure how to have the output return as a string.
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave an example of how to convert a two-digit array into a String, with the question how to proceed if you did not know the length of the array upfront. How about using some loop?
 
Kaid Elliott
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:I gave an example of how to convert a two-digit array into a String, with the question how to proceed if you did not know the length of the array upfront. How about using some loop?


For this specific question I have to input a single integer not an array so converting an array into string wouldnt work. Thank You Tho
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay,

see your opening post. You convert a number into an array of zeroes and ones, so all that is left to do, at least that's what I thought, is turning that array into a String. Sorry for my misunderstanding.
 
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see you showed your work. You can mix and match your own code with mine.

So here's is my code.  


 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In his/her opening post, OP writes

Kaid Elliott wrote:The method I am trying to create is: public static String IntToBinary(int inputInt). (...)


Now, sofar the input int has been transformed into an array of zeroes and ones, nothing wrong with that, so logically, the next and last step is to convert that array to a String and return that. It seems like a hard hurdle to take, that last step.

Well, anyway, here is a solution, one of many, probably of no use to OP, but one that I dedicate to Campbell    
 
Message for you sir! I think it is a tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!