0

This has been bugging me for the last few hours. I have a long string and I want to convert it to a byte array. I want to preserve leading zeroes too. Have tried DatatypeConverter & BigInteger methods and cant seem to get the proper result. which is a byte array (of hex byte values).

byte[] array = str.getBytes();

This also doesn't seem to work, with all these methods I seem to be getting decimal representation of the string. I need it to be hex.

This is what I'm using at the minute:

String line;   
line = file.readLine();   
long seq = Long.parseLong(line);   
String hexx = Long.toHexString(seq);   
byte[] out = hexx.getBytes();

BTW, i am using BufferdReader for input and RandomAccessFile for output. Any help would be appreciated.

7
  • 2
    your question is not clear (what is a decimal representation of the string?). Add inputs expected outputs Commented Oct 30, 2014 at 17:48
  • 2
    Your leading 0s are disappearing cause of Long.parseLong() Commented Oct 30, 2014 at 17:51
  • The input file is an ASCII file filled with number values. I want the output to be a binary file. Commented Oct 30, 2014 at 17:51
  • Decimal representation means the string holds a number of ASCII characters, all numbers 0-9. Commented Oct 30, 2014 at 17:55
  • 1
    So if the file contained 12345, what should the byte representation be? Something like [0x01, 0x02, 0x03, 0x04, 0x05]? Commented Oct 30, 2014 at 18:09

2 Answers 2

1

Ok, to convert from one radix to another. Jeez!

String str = ...
int fromRadix = ...
int toRadix = ...
String out = new BigInteger(str, fromRadix).toString(toRadix);

For example, to convert the decimal number "12345" to hex:

String str = "12345";
int fromRadix = 10;
int toRadix = 16;
String out = new BigInteger(str, fromRadix).toString(toRadix);
// yields "3039"

Go away! :P

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

12 Comments

No, sorry. Maybe I was unclear, I want to convert the ENTIRE string from decimal to hexadecimal and write it to file.
What is in this string? A hex representation of a number (like "011ABF") or something like, "Hello, world"? If it's a hex number, this works.
Actual ASCII characters 0-9.
@resetter: And each individual character goes in its own byte, or is it two characters per byte?
Dude, you need to express yourself more clearly! If I knew where you were (and if I were meaner) I'd throttle you right now! So you just want to convert from one radix (base 16, base 10, etc) to another radix. That means you're converting from text to text. You just want a different text representation.
|
0

Ok, I think I have solved this by using BigInteger and it's toByeArray method.

  BigInteger linenum = new BigInteger(line,10);
  writer.write(linenum.toByteArray());

Actually simple solution, thanks for your efforts guys.

1 Comment

That doesn't do what you ask. See my (new) answer.

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.