java - Conversion from long to four-character hex string -
updated question 01/28/2016:
since question wasn't terribly clear before (i'm learning still), here re-write of post:
here scenario find myself in:
i have code written reads data log file containing gps coordinates formatted in following way in hex:
"x1 x2 x3 x4 y1 y2 y3 y4"
where x's latitude component , y's longitude component. code converts these messages values latitude/longitude in degrees performing following sequence:
string strhexlat = x1 + x2 + x3 + x4; //(x1x2x3x4 viewed 1 hex value) string strhexlon = y1 + y2 + y3 + y4; //(y1y2y3y4 viewed 1 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 mas deg double lon = (double) intlon / 3600000; // convert mas deg despite not being particularly elegant, above code want to. real trouble begins reversing process. trying write code create log files in same format existing files, starting coordinates , generating 8 character hex array representing coordinates.
the following snippets have perform conversion, along sample inputs, resulting outputs, , desired outputs:
(1) convert coordinates in degrees milliarcseconds:
/* test coordinates */ mlat = 42.281422; mlon = -83.748552; /* convert values degrees 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 desired.
(2) account negative values:
/* account 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 desired.
(3) split value 4 values (just trying latitude component now):
/* split 4 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:
***** not entirely negative values.
(4) convert values single hex string:
/* convert 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 valid, , can converted desired value in degrees.
if:
convlat = 152213119 converted "912967f"
want:
convlat = 152213119 become "09 12 96 7f"
where:
of 4 hex values can either 1 or 2 digits , padded on left 0's, in case first digit of four.
(5) convert values 4 hex strings:
/* convert 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 single hex string value "912967f"
which want in form of 4 strings mentioned above, "09 12 96 7f".
when converting directly 4 hex values, however, "9 12 ffffff96 7f".
in case padding operation simple, not want 3 value negative.
questions:
why third value negative when converting directly 4 hex characters , not when converting 1 hex string?
is there 'easy' way convert single string format desire?
write sloppy code run single code through try different ways of splitting values , testing string converts back, seems wrong way go it.
note: have found following helpful, not complete scenario in:
how perform unsigned signed conversion in java?
thanks in advance help, , apologize if have again left out necessary details, please ask , update again possible if need more info.
i haven't tested this, can't use string.format?
string.format ("%02x %02x %02x %02x", (byte) ( ( convlat >>> 24 ) & 0xff ), etc.
that is, break out each byte, have done shifting , masking, , passing them parameters string.format.
the 02 ensures 2 digits, adding leading 0 if necessary. upper case x means use hex , use upper case a-f.
Comments
Post a Comment