**UPDATED QUESTION 01/28/2016:**

*Since my question wasn't terribly clear before (I'm learning still), here is a re-write of my post:*

Here is the scenario I find myself in:

I have some code written that reads data from a log file containing GPS coordinates formatted in the following way in hex:

"X1 X2 X3 X4 Y1 Y2 Y3 Y4"

Where the X's are the latitude component and the Y's are the longitude component. My code converts these messages into values for latitude/longitude in degrees by essentially performing the following sequence:

```
String strHexLat = X1 + X2 + X3 + X4; //(X1X2X3X4 viewed as one hex value)
String strHexLon = Y1 + Y2 + Y3 + Y4; //(Y1Y2Y3Y4 viewed as one hex value)

int intLat = Integer.decode(strHexLat);
int intLon = Integer.decode(strHexLon);

if ((intLat & 0x40000000) != 0) {
    intLat |= 0x80000000;
}
if ((intLon  & 0x40000000) != 0) {
    intLon  |= 0x80000000;
}

double lat = (double) intLat / 3600000; // convert from MAS to DEG
double lon = (double) intLon / 3600000; // convert from MAS to DEG

```

Despite not being particularly elegant, the above code does what I want it to. Where my real trouble begins is reversing the process. I am trying to write some code that will now create log files in the same format as the existing files, starting with coordinates and generating the 8 character hex array representing the coordinates.

The following snippets are what I currently have to perform this conversion, along with sample inputs, the resulting outputs, and the desired outputs:

**(1) Convert coordinates in degrees to milliarcseconds:**

```
/* TEST COORDINATES */
mLat = 42.281422;
mLon = -83.748552;

/* Convert values from degrees to milliarcseconds*/
long convLat = (long)(mLat * 3600000.0);
long convLon = (long)(mLon * 3600000.0);

```

*Inputs:* 

```
mLat = 42.281422  
mLon = -83.748552  

```

*Outputs:* 

```
convLat = 152213119  
convLon = -301494787  

```

*Desired Outputs:*  

Above output is as desired. 

**(2) Account for negative values:**

```
/* Account for negative values */
if(convLat < 0) convLat = convLat & 0x00000000ffffffffL;
if(convLon < 0) convLon = convLon & 0x00000000ffffffffL;

```

*Inputs:* 

```
convLat = 152213119  
convLon = -301494787  

```

*Outputs:* 

```
convLat = 152213119  
convLon = 3993472509  

```

*Desired Outputs:*  

Above output is as desired. 

**(3) Split value into 4 values (just trying latitude component for now):**

```
/* Split into four values */
byte[] dataLat = new byte[4];
dataLat[0] = (byte) ( ( convLat >>> 24 ) & 0xff );
dataLat[1] = (byte) ( ( convLat >>> 16 ) & 0xff );
dataLat[2] = (byte) ( ( convLat >>> 8 ) & 0xff );
dataLat[3] = (byte) ( ( convLat >>> 0 ) & 0xff );

```

*Inputs:* 

```
convLat = 152213119  

```

*Outputs:* 

```
dataLat[0] = 9  
dataLat[1] = 18  
dataLat[2] = -106  
dataLat[3] = 127  

```

*Desired Outputs:*  

\*\*\*\*\* I am not entirely certain about the negative values. 

**(4) Convert values into single hex string:**

```
/* Convert to 1 hex string per component */
String hexLat = Long.toHexString(convLat);
String hexLon = Long.toHexString(convLon);

```

*Inputs:* 

```
convLat = 152213119  
convLon = 3993472509  

```

*Outputs:* 

```
hexLat = "912967f"  
hexLon = "ee078dfd"  

```

*Desired Outputs:*  

These values are valid, and can be converted back to the desired value in degrees. 

If:  

convLat = 152213119 was converted to "912967f"  

I want:  

convLat = 152213119 to become "09 12 96 7f"  

Where:  

any of the four hex values can be either 1 or 2 digits and will be padded on the left with 0's, in this case it was just the first digit but it could be any of the four. 

**(5) Convert values into 4 hex strings:**

```
/* Convert to 4 hex strings per component */
String[] hexStringLat = new String[4];
hexStringLat[0] = Integer.toHexString(dataLat[0]);
hexStringLat[1] = Integer.toHexString(dataLat[1]);
hexStringLat[2] = Integer.toHexString(dataLat[2]);
hexStringLat[3] = Integer.toHexString(dataLat[3]);

```

*Inputs:* 

```
dataLat[0] = 9  
dataLat[1] = 18  
dataLat[2] = -106  
dataLat[3] = 127  

```

*Outputs:* 

```
hexStringLat[0] = "9"  
hexStringLat[1] = "12"  
hexStringLat[2] = "ffffff96"  
hexStringLat[3] = "7f"  

```

*Desired Outputs:*  

When converting to just a single hex string we get the value "912967f" 

Which I want to be in the form of four strings as mentioned above, "09 12 96 7f". 

When converting directly to four hex values, however, I get "9 12 ffffff96 7f".  

In this case the padding operation is simple, however I do not want the 3 value to be negative. 

**Questions:**  

Why is the third value negative when converting directly to four hex characters and not when converting to just one hex string? 

Is there an 'easy' way to convert the single string to the format I desire?  

I could write some sloppy code to run the single code through that would try different ways of splitting the values and then testing that the string converts back, but that just seems like the wrong way to go about it.

**Note:** 
I have found the following somewhat helpful, but not complete for the scenario I am in:  

[How to perform unsigned to signed conversion in Java?](https://stackoverflow.com/questions/3935304/how-to-perform-unsigned-to-signed-conversion-in-java) 

Thanks in advance for the help, and I apologize if I have again left out any necessary details, please ask and I will update again as soon as possible if you need more info.