4

Given the following string:

3132333435363738396162636465666768696a6b6c6d6e6f70

I converted the string to hex and now i want to file write it as hex not a string. I tried converting it to int but Integer.parseInt converts up to 4 only and if go beyond that it would give error already.

1
  • 7
    What does this have to do with swing? Commented Feb 20, 2013 at 7:44

3 Answers 3

8

Have you tried the BigInteger constructor taking a string and a radix?

BigInteger value = new BigInteger(hex, 16);

Sample code:

import java.math.BigInteger;

public class Test {

    public static void main(String[] args) {
        String hex = "3132333435363738396162636465666768696a6b6c6d6e6f70";
        BigInteger number = new BigInteger(hex , 16);
        System.out.println(number); // As decimal...
    }
}

Output:

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

6 Comments

i need to file write it as is.
@user2090151: I have no idea what you mean by that. It's a number - there's no such thing as "as is". You need to represent it somehow - as a decimal number or whatever. You asked how to parse it as a number - I've shown you how to do that. If you just want to parse the hex string into a byte array, that's a completely different matter, and you should have asked that to start with. (Don't ask a new question - there are lots of hex parsing questions on Stack Overflow.)
after convert, its value is changing how to get same value?
@AmitSharma: I'm afraid I don't understand what you're asking. I suggest you ask a new question, with a complete example including what you expect to see and what you actually see.
i convert this value "104917385788526078598" and i am getting after convert this value"1230498387628795168785816"
|
1

Use BigInteger, one of the constructors takes a radix.

BigInteger value = new BigInteger(hexString, 16);

Comments

1

Try:

new BigInteger("3132333435363738396162636465666768696a6b6c6d6e6f70", 16)

Comments

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.