# Flipping Hexadecimal Values



## Dark Void (15. Jun 2012)

Hi,
I need to write a Java program that takes a user input (integer), converts that to hexadecimal, flipps the value and outputs the result.

Flipping goes like this:

00037B54 -> 547B0300

(Last byte comes first, pre-last byte comes second, second byte comes second and first byte comes last).


```
import javax.swing.JOptionPane;

public class ValueFlipping 
{
	public static void main(String[] args) 
	{
		String input = JOptionPane.showInputDialog("Please enter a number with up to 9 digits:");
		
                try 
		{
			int number = Integer.parseInt(input);	
			String hexString = Integer.toHexString(number);
			
			// TODO 8 digited output & flipping
			
			JOptionPane.showMessageDialog(null, "Result: "+hexString);
		} 
		
		catch (Exception e) 
		{
			JOptionPane.showMessageDialog(null, "Invalid input!");
		}
}
```
I´m still missing the part where it flipps the value. The HEX output should always format to have 8 digits -> 00000063 (when 99 is entered) to make flipping easier.

I would use hexString.charAt(_); to talk to each position in the HEX number. It should store the result to a new array because otherwise it would overwrite the value from where it reads from.

Thanks for any tips.


----------



## HelgeW (15. Jun 2012)

Hi,

Have a look at String.format() to make your output string 8 char long.
and after it is ease to flip the string, isn't it?!

Hope it helps,
Helge


----------



## Dark Void (15. Jun 2012)

HelgeW hat gesagt.:


> Hi,
> 
> Have a look at String.format() to make your output string 8 char long.
> and after it is ease to flip the string, isn't it?!
> ...


Hmm...

String.format(hexString, "one", "two"); // ??

How would I put it, that my hexString always is 8 digited?
I didn´t find anything that helps on google.


----------



## SlaterB (15. Jun 2012)

seach 'String.format 8 digited'
first Link
String.Format Method (String, Object) (System)


> // We want the Hexadecimal digit to be 8 characters long


----------



## Ullenboom (15. Jun 2012)

U want to do a big endian <-> little endian encoding. Java has almost no support for this with one exception: with bytebuffers. It will look like this:


```
int i = Integer.parseInt( "37B54", 16 );
BigInteger bi = new BigInteger( ByteBuffer.allocate(4).order( ByteOrder.LITTLE_ENDIAN ).putInt( i ).array() );
System.out.printf( "%X", bi );
```


----------



## nillehammer (15. Jun 2012)

Integer.reverseBytes is the way to go:

```
int number = Integer.parseInt(input);   
int reversedBytes = Integer.reverseBytes(number); //<---- Use this
String hexString = Integer.toHexString(reversedBytes);
```


----------



## Dark Void (4. Jul 2012)

SlaterB hat gesagt.:


> seach 'String.format 8 digited'
> first Link
> String.Format Method (String, Object) (System)


C# isn´t compatible with Java...ueh:


nillehammer hat gesagt.:


> Integer.reverseBytes is the way to go:
> 
> ```
> int number = Integer.parseInt(input);
> ...


Great solution!
We still need a way to format the "hexString" variable to 8 bytes (or any other amount) for any result. Putting 1 would make it 1000000 (7 digits) when using "your" reverseBytes code.


----------

