I have an sequence of 30 or so hexa decimal values 0x01, 0x02...
My question is how can i store these values in Java. I do not know if it matters to convert it into String and store it. But thats what i am not looking for. I just want to store the hexa decimal as constant data. Please also consider the contents representing the Hex-Decimal form and if/what happens to it ?
Hexadecimal is just a format to display numbers, just like decimal, binary or octal. Numbers are just numbers - hexadecimal is not a property of the numbers themselves, it's only a way to display numbers.
Writing the numbers as 0x01, 0x02, etc. in your source code is exactly the same as writing them in decimal 1, 2 etc.
So, you can store the numbers like you would store any other numbers - for example as an array of ints.
You should store them as Integers using the Integer.valueOf(String s, int radix) method.
In your case, the radix is 16.
Just store them in an integer (if it fits within the value range) ... when displaying them, convert it as hex.
Related
[InputParam1:Decimal Number in String format(for Eg:30),
InputParam2:Integer to denote number of repeating 0's to append(for Eg:6)]
For converting a number from Decimal to Binary and pad digits to its front I need to perform the following steps:
Step1: BigInteger binary=new BigInteger(InputParam1,2);
-->This works when I define a BigInt with a base 16 (which I just tried when base2 failed) but not with a base 2 like above.
It throws a numberformat exception.
Step 2: String pad=StringUtils.repeat("0",InputParam2);
(to repeat 0 'InputParam2' number of times)
-->This works fine
Step 3: Need to append pad in front of binary(from Step1)
For a BigInteger, I'm not able to get something similar to .append.
So I'm trying to a)convert this BigInteger number to String and b)append pad c)convert back to BigInteger (this step I'm not able to get without creating a new BigInteger)
Any pointers on Step1 and Step3-c would be helpful please.
#1 - You haven't said what value you're passing in InputParam1, but my guess is that you're passing in something other than a string containing only '0' in '1' characters. The 'radix' parameter to the constructor tells the code how to interpret the string you're giving it. This works fine for me:
BigInteger binary=new BigInteger("1000",2);
System.out.println(binary);
How you construct the number has nothing to do with how you're eventually going to want to represent or display that number. The key idea here is that the representation of a number (hex, decimal, binary) has nothing to do with the number itself, just like leading zeros don't affect the value of number. 1111 binary, f hex, and 15 decimal are all the same number. If you pass the three of these into BigDecimal's constructor with a radix of 2, 16 and 10 respectively, you'll end up with EXACTLY the same thing in each case. The object will lose any notion of what kind of representation you used to set it to its initial value.
#3 - There is no concept of a number (int, BigInteger, etc.) with zero padding at the front, just as there's no notion of which base/radix you might use to represent a number visually. You can think about it conceptually, but that's just a way of displaying the number. It has nothing to do with the number itself.
I hadn't tried it before, but it seems there's no super simple way to format a binary value in Java with leading 0s, like there is for decimal and hex, since String.format() doesn't give a format specifier for binary. Per this StackOverflow post How to get 0-padded binary representation of an integer in java?, this seems to be about the best way to go, having converted the most accepted answer to work with BigInteger:
String str = String.format("%16s", binary.toString(2)).replace(' ', '0');
So here's all of my sample code, with output:
BigInteger binary=new BigInteger("1000",2);
System.out.println(binary);
String str = String.format("%16s", binary.toString(2)).replace(' ', '0');
System.out.println(str);
Output:
8
0000000000001000
How can i convert a binary fraction or a decimal fraction into hex in Java?
What is the algorithm to do that?
for example i want (11.110)2 be converted into (3.C)16
and which data type do I need to hold the hex fraction ?
plz help.
Thanks.
This answer is not meant to be a complete guide on how to make a fully working program that converts from one radix to another, but it mentions most of the steps you'd have to do if you want to use the built-in methods that are available.
There are no built-in methods for converting floating-point numbers, but there are built-in methods for converting integers from/to any radix between 2-36. If you only need to convert between binary and hexadecimal and nothing else, you could also do it manually (see below the line further down), but I recommend using these methods otherwise.
If you remove the radix point . from the number (in its String-representation), you get a whole number, but keep track of how many digits were to the right of the radix point.
Here are three ways to convert a whole number from a String with any radix:
Integer.parseInt(String val, int radix) will convert to int.
Long.parseLong(String val, int radix) will convert to long.
new BigInteger(String val, int radix) will convert to BigInteger.
You should then convert the number to a double or BigDecimal, and make sure you get the radix point back in place, by dividing by the correct number. For instance, you can turn (1234)6 into (12.34)6 by dividing it by 6².
Now you are halfway done, and basically go backwards to convert the number back to a String (but with some other radix).
At the step where you want to convert from int/long/BigInteger to a String, use toString:
Integer.toString(int val, int radix) will convert from int.
Long.toString(long val, int radix) will convert from long.
val.toString(int radix) will convert from BigInteger.
Remember to insert the radix point again, and you're done.
To convert from binary to hex manually, you do the same as you would do if they were whole numbers, with few differences.
Divide the binary number into sets of 4 consecutive digits, starting at the radix point, going both left and right. Add leading/trailing zeros as needed. The rest is as explained in the link.
For instance: 101011.011 is divided into 0010 1011 . 0110 which becomes 2B.6 after converting each set to hexadecimal.
I am trying to iterate through a 2D array of ints and change all of their values from binary form to decimal form and store those values. To be more clear: I have a bunch of different int values which are all technically in binary form but their values are stored as ints. For instance, I might have a variable int example1 = 10001110 -- where the chars form a binary number but it's still a primitive int. So again, I need to convert those values from binary to decimal form and store them as ints. To make your lives a bit easier/more straightforward: the array that currently contains the ints has 8 rows and 2 columns: all of whose entries are in binary int form.
Thanks guys.
Iterate through your array of binary strings and convert them, then store that in a new array.
Integer.parseInt(binaryString, 2);
To change binary form to decimal form you can use Integer.parseInt(someStringInteger, 2). To make someStringInteger use Integer.parseInt(someInt). For example
//1110 (bin) -> 14 (dec)
System.out.println(Integer.parseInt(Integer.toString(1110), 2));//out -> 14
I have a byte array that can be of size 2,3 or 4. I need to convert this to the correct integer value. I also need to do this in reverse, i.e an 2,3 or 4 character integer to a byte array.
e.g., raw hex bytes are : 54 and 49. The decoded string US-ASCII value is 61. So the integer answer needs to be 61.
I have read all the conversion questions on stackoverflow etc that I could find, but they all give the completely wrong answer, I dont know whether it could be the encoding?
If I do new String(lne,"US-ASCII"), where lne is my byte array, I get the correct 61. But when doing this ((int)lne[0] << 8) | ((int)lne[1] & 0xFF), I get the complete wrong answer.
This may be a silly mistake or I completely don't understand the number representation schemes in Java and the encoding/decoding idea.
Any help would be appreciated.
NOTE: I know I can just parse the String to integer, but I would like to know if there is a way to use fast operations like shifting and binary arithmetic instead?
Here's a thought on how to use fast operations like byte shifting and decimal arithmetic to speed this up. Assuming you have the current code:
byte[] token; // bytes representing a bunch of ascii numbers
int n = Integer.parseInt(new String(token)); // current approach
Then you could instead replace that last line and do the following (assuming no negative numbers, no foreign langauge characters, etc.):
int n = 0;
for (byte b : token)
n = 10*n + (b-'0');
Out of interest, this resulted in roughly a 28% speedup for me on a massive data set. I think this is due to not having to allocate new String objects and then trash them after each parseInt call.
You need two conversion steps. First, convert your ascii bytes to a string. That's what new String(lne,"us-ascii") does for you. Then, convert the string representation of the number to an actual number. For that you use something like Integer.parseInt(theString) -- remember to handle NumberFormatException.
As you say, new String(lne,"US-ASCII") will give you the correct string. To convert your String to an integer, use int myInt = Integer.parseInt(new String(lne,"US-ASCII"));
i have byte array stored by both hexadecimal and decimal value,i want to search for hexadecimal 1 i'e SOH in the how can i do this in java,plz give a sample code.
int SOH=0X01;
if(SOH==1)
Is showing true. Is this correct?
Your byte arrays will just store byte values. The hexadecimal (or decimal, or octal) is just the representation of that value in the source code. Once stored, they're all the same value e.g.
0x01 == 1 == 01
(the last being octal)
So checking for a particular value is the same code. A value won't know if it's been represented as hex/dec/oct.
How is the data in the byte array stored as hexadecimal and decimal?
A byte array contains bytes.
byte[] decimal = new byte[] {1,10 };
byte[] hexa = new byte[] {0x1,0xa };
These contain the same values, you can compare them directly, you don't need any specific code.