0

I have a TCP Client running on Android. The Server is an OBD2 Dongle. I am getting an InputStream which is saved in a String but it's a hex number.

The Response of the TCP Server looks like that: FD A8 F1 FF 1F D0 03 20 It is a 8byte long hex number but I it is sent as a String, so I need to convert it to hex.

How can I do that?

Edit:

InputStream is; 

is = socket.getInputStream(); 

while(var_Receive){

BufferedReader rd = new BufferedReader(new InputStreamReader(is); 

String line = rd.readLine(); 

System.out.prinln(line); 

} 

And this is exactly what I see on screnn 'FD A8 F1 FF 1F D0 03 20' including the spaces between.

mosambers
  • 21
  • 1
  • 7
  • `How to convert String to hex?`. Wrong question. You have alredy a hex string. You want to convert it to long. Please adapt your post. – greenapps Nov 17 '16 at 12:57
  • So you have String hexString = "FD A8 F1 FF 1F D0 03 20`; Why these spaces in between? I would rather think you have String hexString = "FDA8F1FF1FD00320"; – greenapps Nov 17 '16 at 12:58
  • I am getting an InputStream which is saved in a String`. One would read `bytes` from an input stream to begin with. So if you end up with a string you made a string out of them. So please tell exactly what you receive. – greenapps Nov 17 '16 at 13:02
  • @greenapps I have public InputStream is; is = socket.getInputStream(); while(var_Receive){ BufferedReader rd = new BufferedReader(new InputStreamReader(is); String line = rd.readLine() System.out.prinln(line); } And this is exactly what I see on screnn 'FD A8 F1 FF 1F D0 03 20' including the spaces between. – mosambers Nov 17 '16 at 13:17

3 Answers3

0

try this: Integer.toHexString(Integer.parseInt(String));

MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

Try the below code it will work. The value printed in console is in hexadecimal format. Store it if you want to use it later.

public static void stringToHex(){

    String hexStr = "FD A8 F1 FF 1F D0 03 20";
    char[] chars = hexStr.toCharArray();

      StringBuffer hex = new StringBuffer();
      hex = new StringBuffer("0x");
      int  i =0;
      while(i<chars.length){
          if(chars[i]==' '){
              i++;
              //you can store this value if you want
              System.out.println(hex.toString());
              hex = new StringBuffer("0x");
              continue;
          }
          hex.append(chars[i]);
          i++;
      }
}

Shorter implementation according to requirement:

String hexStr = "FD A8 F1 FF 1F D0 03 20";
String[] hexStrs = hexStr.split(" ");
DecimalFormat df = new DecimalFormat("#.00");
Double[] values = new Double[hexStrs.length];
for(int i=0; i<hexStrs.length; i++){
    values[i] = Double.valueOf(df.format(Integer.valueOf(hexStrs[i], 16)*0.04));
    System.out.println(values[i]);
}
SachinSarawgi
  • 2,632
  • 20
  • 28
  • ????? I wonder what works. And where is the conversion to long? The input was hex already. – greenapps Nov 18 '16 at 16:26
  • Hi greenapps, actually Mike wanted FD in form of 0xFD so I just appended that string. It could have been shorter using split() function of String. – SachinSarawgi Nov 18 '16 at 17:01
  • @greenapps can You give an example for conversion? – mosambers Nov 21 '16 at 12:28
  • "hexStr" is the String input. Example using spli() `String[] hexStrs = hexStr.split(" "); for(String str : hexStrs) System.out.println("0x"+str);` – SachinSarawgi Nov 21 '16 at 12:36
  • @SachinSarawgi I maybe I could not express myself. At the end I Need the single or multiple Bytes in a double. For Example: The second Byte 'A8' delivers informations about pressure in an engine. Hex A8 is a decimal 168. The conversion rate for calculating the pressure is 0.04 bar/bit. So I have to calculate 168 * 0.04 = 6.72 bar. This is what I'm trying to do :( – mosambers Nov 21 '16 at 12:45
  • It can be done (variables are equals to as define in previous comment). DecimalFormat helps in determining the number after decimal point. `DecimalFormat df = new DecimalFormat("#.00"); for(String str : hexStrs) System.out.println(df.format(Integer.valueOf(str, 16)*0.04));` – SachinSarawgi Nov 21 '16 at 13:00
  • @Mike edited the answer according to your requirement. Does it help? – SachinSarawgi Nov 21 '16 at 13:07
  • That works, also the calculating is correct. But I have to save the value before multiplying with 0.04 somewhere but I am not sure which is the best way... – mosambers Nov 21 '16 at 13:23
  • In place of printing it to console store the value `df.format(Integer.valueOf(str, 16)` in some array and use whenever required by iterating over that array. – SachinSarawgi Nov 21 '16 at 13:25
  • With decimalformat it's still a string, right? I think it would be better to convert to double to completly go away from strings after getting the inputstream. You helped me a lot, thank you again. – mosambers Nov 21 '16 at 13:40
0

Do the values in the hex representation represent a single value? If so:

String hexStr = "FD A8 F1 FF 1F D0 03 20";
String hexNoSpace = hexStr.Replace(" ", string.Empty);

long valAsLong = long.Parse(hexNoSpace, NumberStyles.AllowHexSpecifier);
ulong valAsUnsignedLong = ulong.Parse(hexNoSpace, NumberStyles.AllowHexSpecifier);

result:

as long: -168618907973713120

as ulong: 18278125165735838496

Mark W
  • 705
  • 1
  • 9
  • 20