D347th
D347th

Reputation: 707

Append two hexadecimal long values in Java or C

I am implementing the SHA-2 algorithm in Java. However, I have run into a problem. I need to append two hex values into one, but I am unable to do so. I tried appending the two as a string and using Long.parseLong(appendedString) but that causes a number format exception. Is there anyway I can do this in Java? If not is there anyway to do this in C and I'll just implement it in C? Thanks for reading.

Here is the code:

    String temp = h[0] + "" + h[1]; //Where h[0] and h[1] are two hex values stored as Long
    //I also tried String temp = String.valueOf(h[0]) + String.valueOf(h[1]); but no dice
    Long appended = Long.parseLong(temp); //Number format exception here

When I say append I mean something like: 0x6a09e667 + 0xbb67ae85 = 0x6a09e667bb67ae85

Upvotes: 1

Views: 3039

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328614

0x6a09e667 + 0xbb67ae85 gives 0x6a09e6670xbb67ae85 which isn't valid. Try this code instead:

String temp = h[0] + h[1].substring( 2 ); // Strip "0x" from second string

Upvotes: 1

Richard J. Ross III
Richard J. Ross III

Reputation: 55573

I'm assuming your code looks something like this:

long hex1 = 0x6a09e667;
long hex2 = 0xbb67ae85;

and you want the output of 0x6a09e667bb67ae85.

You can do this with some bit-shifting, such as the following:

long result = hex2 | (hex1 << 32);

Upvotes: 4

Related Questions