Converting a string of digits into their hex equivalents - java

I tried this
String dataC = Integer.toHexString(dataB);
But of course that wouldn't work with a string...

toHexString requires integer as argument, so how about changing string of digits to integer first?
String dataC = Integer.toHexString(Integer.parseInt(dataB));
(more readable code)
int dataBInteger = Integer.parseInt(dataB);
String dataC = Integer.toHexString(dataBInteger);
If value from string is in range of long (-9223372036854775808; 9223372036854775807) you can use
String dataC = Long.toHexString(Long.parseLong(dataB));
If you don't want to assume any limit of number of digits you can use BigInteger
BigInteger bi = new BigInteger(dataB);
String dataC = bi.toString(16);

You should first convert dataB string to int:
int number = Integer.parseInt(dataB);
String dataC = "0x" + Integer.toHexString(number);

As it's a long string, try
String hex = new BigInteger(dec).toString(16);

Related

Java - Hex String representation to integer Hex

I have a string as "5F2A" as Hex. I would like to convert it as int 0x5F2A.
String str = "5F2A";
int number = someOperation(str);
And the number should be (with 0x)
0x5F2A
Is it possible?
To rephrase and share what I learnt today
Map<Integer, String> map = new HashMap<>();
map.put(0x5F2A, "somevalue");
System.out.println(map.get(24362));
System.out.println(map.get(0b0101111100101010));
Would give the value somevalue for both.
No transformation required:
System.out.println("0x" + str);
And to turn an arbitrary int into HEX representation:
Integer.toHexString(intNumber);
That should be all you need to get going!
int i = 0x5F2A not really means nothing because in memory, all is in binary, it's only when you print that it matters
String str = "5F2A";
int number = Integer.parseInt(str, 16); //alows to store an int, binary 0101111100101010
System.out.println(number); //24362 (decimal by default)
System.out.println(Integer.toHexString(number)); //5f2a (hexa possible too)
By default, it prints in (binary into) decimal format, but you can print in hexa format, but int i = 0x5F2A means at 100% the same as int i = 24362
See here
Integer.parseInt(/*your String*/, 16);
16 is the radix for hexadecimal.

java.lang.NumberFormatException: Converting string to ASCII

I am trying to convert my string to ASCII value through my hash function, which looks like this:
public long hash(String word){
StringBuilder sb = new StringBuilder();
String ascString = null;
long asciiInt;
for(int i=0;i<word.length();i++){
sb.append((int)word.charAt(i));
}
ascString = sb.toString();
asciiInt = Long.parseLong(ascString);
return asciiInt;
}
and later on, I will call it in my insert() method to perform quadratic hashing using a hashTable, and the insert method looks like this:
public void insert(Word word){
int start = (int)(hash(word.text)%tableSize);
int key = start;
int attempt=0;
while(hashTable[key]!=null){
attempt++;
key=(start+(int)Math.pow(attempt,2))%tableSize;
}
hashTable[key]=word;
}
However, it throws the java.lang.NumberFormatException if the string I am trying to convert has more than 6 characters. Can anyone help me fix it or a better ways of coming up with the key value for my hash table?
Thanks!
The value you're attempting to gain (base 10 long) can't be achieved from your string because you've the wrong base. Say the string is "DEADBEEF". Because all digits of DEADBEEF are base 16, you can specify the radix as 16 and use
Long.parseLong(DEADBEEF, 16);
The non-radix method assumes that the string contains a base-10 long when really the number is much longer (DEADBEEF is 3735928559 in base 10). Check your string maybe?

Java Hex number increase by one

I have some auto generated ids represented as a HEX String. I want to find the next 1000 values. For instance, let's suppose I have the following string
String keyFrom = "536a11dae4b062cab536549d";
How can I get from a java code the following, into String?
536a11dae4b062cab536549e
536a11dae4b062cab536549f
536a11dae4b062cab53654a0
536a11dae4b062cab53654a1
536a11dae4b062cab53654a2 ... etc.
Use BigInteger as below
BigInteger decimal = new BigInteger("536a11dae4b062cab536549d",16);
for ( int i=0;i<1000;i++){
decimal = decimal.add(BigInteger.ONE);
System.out.println(decimal.toString(16));
}
Convert your String to BigInteger and increment it:
BigInteger bigInt = new BigInteger(hexString, 16);
for(int i = 0 ; i < 1000 ; ++i) {
// do something with bigInt...
System.out.println(bigInt.toString(16));
bigInt = bigInt.add(BigInteger.ONE);
}
EDIT: If your using hex strings longer than ~8 characters, use the solution using BigInteger above.
Use Integer#parseInt(String, 16) to parse the hex string into an integer, add one to it, and then use Integer#toHexString to turn it back to hexadecimal.
String hexString = "A953CF";
// 16 sepcifies the string to be in base 16, hexadecimal
int hexAsInt = Integer.parseInt(hexString, 16);
hexAsInt += 6; // Add 6
String newHexString = Integer.toHexString(hexAsInt);
System.out.println(newHexString);
--> A953D4
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toHexString(int)

Split a string all alpha and all numeric

I have a List and I would like to split the first two characters (alpha characters) into a different string and then all the numbers that follow (they vary in length).
How could I do that?
String wholeString == "AB4578";
String alpha; // this has to be AB
String num; // this has to be 4578
Thank you very much in advance!
Tested and works:
String wholeString = "AB4578";
String alpha = null;
String num = null;
for (int i = 0; i < wholeString.length(); i++) {
if (wholeString.charAt(i) < 65) {
alpha = wholeString.substring(0, i);
num = wholeString.substring(i);
break;
}
}
With this approach both the A-z part and the 0-9 part can vary in size, it might not be very effective though considering it's calling charAt(...) for every char in the String.
Hope this helps.
String wholeString = "AB4578";
String alpha = wholeString.substring(0,2);
String num = wholeString.substring(2);
Must See
String.substring(int, int)
If the format is the same, then the answer is already provided. But if the format is not same than you can convert the string into char array and check each character against the ASCII values to check if it is an alphabet or a number.
char[] ch=wholestring.toCharArray();
Now you can apply a for loop for checking each character individually.
for(int l=0; l<ch.length;l++)
{
//code to check the characters
}
And you can separate both types in different strings using StringBuilder or forming two char arrays and then converting them to strings using
String.valueOf(chArray);
ASCII values - http://www.asciitable.com/
Try using the substring method for Strings.
Example:
String alpha = wholeString.substring(0,2);
String num = wholeString.substring(2);
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#substring%28int%29
If the format is always the same you can just do this:
String wholeString = "AB4578";
String alpha = wholeString.substring(0, 2);
String num = wholeString.substring(2);
Recommend String API. You would need to use substring operations.

Integer to String conversion methods

What are the alternative methods for converting and integer to a string?
Integer.toString(your_int_value);
or
your_int_value+"";
and, of course, Java Docs should be best friend in this case.
String one = Integer.toString(1);
String myString = Integer.toString(myInt);
Here are all the different versions:
a) Convert an Integer to a String
Integer one = Integer.valueOf(1);
String oneAsString = one.toString();
b) Convert an int to a String
int one = 1;
String oneAsString = String.valueOf(one);
c) Convert a String to an Integer
String oneAsString = "1";
Integer one = Integer.valueOf(oneAsString);
d) Convert a String to an int
String oneAsString = "1";
int one = Integer.parseInt(oneAsString);
There is also a page in the Sun Java tutorial called Converting between Numbers and Strings.
String.valueOf(anyInt);
There is Integer.toString() or you can use string concatenation where 1st operand is string (even empty): String snr = "" + nr;. This can be useful if you want to add more items to String variable.
You can use String.valueOf(thenumber) for conversion. But if you plan to add another word converting is not nessesary. You can have something like this:
String string = "Number: " + 1
This will make the string equal Number: 1.

Categories