1

I need to convert Hexideciamal numbers represented as a String to a Signed 8-Bit String.

For Example: Given this Code Snippet:

String hexideciaml = new String("50  4b e0  e7");
String signed8Bit = convertHexToSigned8Bit(hexideciaml);
System.out.print(signed8Bit);

The Output should be: "80 75 -32 -25"

So I pretty much want to implement part of this website in Java. https://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html

Update: Solution needs to be for JRE6 with no other Jars.

3
  • 2
    Why would the "50" remain unhexified? I'd expect the output to be "80 75 -32 -25" Commented Jul 14, 2016 at 19:09
  • 1
    Parse the String using space as a delimiter. Then convert each value to an Integer with a radix of 16 via Integer.parseInt(String, int). Cast that value to byte to convert it to a signed value. Commented Jul 14, 2016 at 19:29
  • You are correct @Reimeus. Updating question. Sorry about that. Commented Jul 14, 2016 at 19:40

1 Answer 1

3

Java 1.8 (stream)

import java.util.Arrays;

public class HexToDec {

    public static String convertHexToSigned8Bit(String hex) {
        return Arrays
                .stream(hex.split(" +"))
                .map(s -> "" + (byte) Integer.parseInt(s, 16))
                .reduce((s, s2) -> s + " " + s2)
                .get();
    }


    public static void main(String[] args) {
        String hexidecimal = "50  4b e0  e7";
        String signed8Bit = convertHexToSigned8Bit(hexidecimal);
        System.out.print(signed8Bit);
    }

}

Java <1.8

import java.util.Arrays;

public class HexToDec {

    public static String convertHexToSigned8Bit(String hex) {
        String[] tokens = hex.split(" +");
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < tokens.length - 1; i++) { //append all except last
            result.append((byte) Integer.parseInt(tokens[i], 16)).append(" ");
        }
        if (tokens.length > 1) //if more than 1 item in array, add last one
            result.append((byte) Integer.parseInt(tokens[tokens.length - 1], 16));
        return result.toString();
    }


    public static void main(String[] args) {
        String hexidecimal = "50  4b e0  e7";
        String signed8Bit = convertHexToSigned8Bit(hexidecimal);
        System.out.print(signed8Bit);
    }

}

Output is: 80 75 -32 -25

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm does not seem to compile. I get this error The method stream(String[]) is undefined for the type Arrays, solution needs to be for JRE6. Also where is s2 initialized?
Ofc it won't compile for JRE6. I used streams, which are present since JRE8. Wait a sec, I will rewrite it for JRE6
Works perfectly with JRE 6. I suggest you update your answer with both solutions, 1 for Jre6 and 1 for Jre8. Then I will except your answer. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.