If use variable bin1 it wont convert, however if i replace the parameter with bin2 it seems to work.
I tried using long instead of int. It's still the same.
public class Test{
public static void main(String[] args) {
String bin1 = "11011100000000010001000000000000";
String bin2 = "01100100001000010001000000000000";
int dec = Integer.parseInt(bin1, 2);
String hex = Integer.toString(dec, 16);
System.out.println(hex);
}
}
It actually works fine with longs.
public class Test{
public static void main(String[] args) {
String bin1 = "11011100000000010001000000000000";
String bin2 = "01100100001000010001000000000000";
long dec = Long.parseLong(bin1, 2);
String hex = Long.toString(dec, 16);
System.out.println(hex);
}
}
Result:
dc011000
Your number is simply too big for an int.
Related
My goal is currently to take a string looking like
"######
# #
# # ##"
# is here my "character" and "#" is walls that should be avoided, I want my character to get out of the box and return the pathway, I realize my example is pretty bad but I hope you understand. The idea I have currently is to read in a text-file with that String, then creating an array as a 2-D grid and then work from there, the code I have currently is:
package soko;
import java.io.*;
import java.util.*;
public class Sokoban2 {
static File file;
Scanner input;
static int b;
static int c;
static String[][] array;
public Sokoban2() {
//array = new String [9][9];
}
public int readFile() throws Exception{
Scanner input = new Scanner(System.in);
file = new File("C:/Users/joaki/Desktop/sokoban/readin.txt");
input = new Scanner(file);
while (input.hasNext()) {
b = b + 1;
String line = input.nextLine();
System.out.println(line);
}
input.close();
return b;
//array = new String[5][5];
}
public void insertStuff() {
//array[1][1]="2";
}
public void printStuff() {
for (int r = 0; r<2;r++){
String line2 = "";
for (int d = 0; d <2;d++){
line2+="["+array[d][r]+"]";
}
System.out.println(line2);
}
}
public static void main(String[] args) throws Exception{
Sokoban g = new Sokoban();
g.readFile();
//g.insertStuff();
//g.printStuff();
//g.creatingGrid(c,b);
//System.out.println(b);
}
}
I really want to print my b, at least check it's value, but my program wont return any value, it's not even returning a null or 0, I've tried to print it as well, no luck there either, ideas ?
You aren't returning the value to any variable, I just ran your code and it works fine, it counts the lines in the file.
int b = g.readFile();
System.out.println(b);
If you don't return the value to a variable you won't have access to the value of b in your main because it's out of scope, more about that here:
https://www.cs.umd.edu/~clin/MoreJava/Objects/local.html
Edit:
I actually just realized that you declared b in your class as static and I decided to run it as you have it above, I still get it printing out the amount of lines.
g.readFile();
System.out.println(b);
If you are going to have your function return a value and you don't plan on storing b then get rid of the static declaration in your class and just declare it in the function that returns b.
I am begginer.While compiling this code
class UpperLowerCase
{
static String ini = "I LOVE JAVA";
static char str[] = ini.toCharArray();
static char invr[] = new char[10];
static int len = 0 , len1 = 0, i;
static void toUpper()
{
for(i=0;i<str.length;i++)
{
if(str[i]>=97 && str[i]<=122)
invr[len1++]=(str[i]-32);
else
invr[len++]=str[i];
}
invr[len1]='\0';
System.out.println("Reverse of"+ str+" is \n"+ invr);
}
static void toLower()
{
for(i=0;i<str.length;i++)
{
if(str[i]>=65 && str[i]<=90)
invr[len1++]=str[i]-32;
else
invr[len++]=str[i];
}
invr[len1]='\0';
System.out.println("Reverse of"+ str+" is \n"+ invr);
}
public static void main(String [] args)
{
toUpper();
}
}
I get error "possible lossy conversion from int to char"
How to convert int to char.
Please help me by telling how to resolve this error?
You can have an overflow when doing mathematic opetations between integers and chars, this can affect the expected result since that value is going to be assigned to a char... here you have
invr[len1++] = str[i]-32;
but you should cast to char like:
invr[len1++] = (char)(str[i]-32);
I am converting some integers to hexadecimals but the problem i have is when i pass a negative value to convert it to hex.
Example:
String satpos = "13.0";
String satposs=satpos.replaceAll("\\.", "");
int satposition = Integer.parseInt(satposs);
String nids = Integer.toHexString(satposition);
String finished = nids+"0000";
Application returns "820000" in this case and that is the correct value i need.
But if i pass:
String satpos = "-7.0";
String satposs=satpos.replaceAll("\\.", "");
int satposition = Integer.parseInt(satposs);
String nids = Integer.toHexString(satposition);
String finished = nids+"0000";
Application returns "ffffffba0000" but i should get "DCA0000"
Any help is appreciated.
Based on a comment by Andreas, here is a test program that prints the values you want:
public class Test {
public static void main(String[] args) {
convert("13.0");
convert("-7.0");
}
private static void convert(String satpos) {
String satposs = satpos.replaceAll("\\.", "");
int satposition = Integer.parseInt(satposs);
if (satposition < 0) {
satposition += 3600;
}
String nids = Integer.toHexString(satposition);
String finished = nids + "0000";
System.out.println(finished);
}
}
If the angle is negative, add 3600 because the angle is in tenths of a degree.
I am trying to print reverse of a 32 bit binary number in decimal format:
Example:
x = 3,
00000000000000000000000000000011
=> 11000000000000000000000000000000
return 3221225472
I am getting a number format exception, can anyone please help me whats wrong in my code? I appreciate your help.
public class ReverseBinary {
public long reverse(long a) {
String s1 = String.format("%32s", Long.toBinaryString(a)).replace(' ', '0');
StringBuilder sb = new StringBuilder(s1);
String s = sb.reverse().toString();
long c = Integer.parseInt(s, 2);
return c;
}
public static void main(String args[]) {
ReverseBinary rb = new ReverseBinary();
long ans = rb.reverse(3);
System.out.println(ans);
}
}
Your variable c might be a long variable, but the value delivered by Integer.parseInt(s,2) is still an integer. This call tries to parse an integer value which causes problems, because the value is obviously out of the integer range.
Simply replace Integer.parseInt(s,2) by Long.parseLong(s, 2).
It should be
long c= Long.parseLong(s,2);
Just in case you want the signed integer that corresponds to the reversed bit pattern: there is a method to just do that.
public class Test
{
public static void main (String[] args)
{
// 000...001 -> 100...000, interpret as two's complement 32bit int
int reversed = Integer.reverse(0b0_00000000_00000000_00000000_00000001);
System.out.println(reversed == Integer.MIN_VALUE); // -> true
}
}
This is my code . I want a binary value for c ,but my output is 294977 . How to xor this?
public class Dumm {
public static void main(String []args) {
int a = 01101010;
int b = 00001111;
int c = a ^ b;
System.out.print(c);
}
}
If you want to take a and b as binary value then start with "0b".
for print binary value use "Integer.toBinaryString()" method.
Try this:
public static void main(String [] args)
{
int a = 0b1101010;
int b = 0b0001111;
int c = a ^ b;
System.out.println(Integer.toBinaryString(c));
}
If you want to treat your numbers as binary, you have to start numbers with 0b.
You have prepended o(English Alphabet) instead of 0(Zero).
int a = 0b01101010;
int b = 0b00001111;
int c = a ^ b;
System.out.print(Integer.toBinaryString(c));