ASCII to Binary

Is there a Java class or method to convert a text file into a Binary Blob to be sent in a message over middleware. The text file contains strings of character and numeric values so I would ideally like a code that will directly convert the text file into binary. Please let me know if you are aware of anything that will perform this task. Thank you for your help!!

[372 byte] By [kbradleya] at [2007-10-2 5:47:26]
# 1

> Is there a Java class or method to convert a text

> file into a Binary Blob to be sent in a message over

> middleware.

I don't think so. Here's an example how to convert a String to an array of chars and bytes and how to convert bytes to binary Strings:

String s = "abcdefg123";

char[] c = s.toCharArray();

byte[] b = s.getBytes();

for(int i = 0; i < s.length(); i++) {

System.out.print("char="+c[i]+"\tbyte="+b[i]);

System.out.print("\tbinary="+Integer.toBinaryString(b[i])+"\n");

}

prometheuzza at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...
# 2

Well that would work... but it would be tedious as the file is going to be more than a few lines. It would be better if there was a way to convert the file directly into binary instead of having to read in the file line by line and then converting to binary. Once the message is received at the other end I will have to convert it back to text to read on the other end so it would better if there was a simplier way to convert. Thanks for your help though!!

kbradleya at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...
# 3

> Well that would work... but it would be tedious as

> the file is going to be more than a few lines.

Errr...you do know what a loop is right?

> It

> would be better if there was a way to convert the

> file directly into binary instead of having to read

> in the file line by line and then converting to

> binary.

What exactly do you think a 'character' is?

> Once the message is received at the other end

> I will have to convert it back to text to read on the

> other end so it would better if there was a simplier

> way to convert.

Is there some reason that you are not just using FTP?

jschella at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...
# 4

Yes I know what a loop is. And I know what a character is. Yes there is a reason why I am not using FTP. Mainly because my project calls for using a middleware and an specific messaging structure. Unfortunately the only way to send a file using this message structure is via a binary blob and the people I am working with what to give me a text file with the information that needs to be sent. It seems to me that a loop would be a very messy way to read the file and convert it to binary especially when I try and read it on the other end on a different computer. So I am essentially looking for a better way to do it than that. Thanks though.

kbradleya at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...
# 5

Perhaps I am missing something here but...

A text file is a blob of binary. It is just a sequence of bits that by convention you interpret as ASCII, or as UNICODE or as some other thing.

If your middle ware is able to put a message wrapper around a binary blob - it should mean that it does not care at all what the pile of bits are that it is sending.

So what exactly do you mean by "convert it to binary" it already is in binary. Just look up this middlewares protocol for a) wrapping a binary blob and for unwrapping it on the other end.

marlin314a at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...
# 6
> Perhaps I am missing something here but...Perhaps I am also missing something.Could it be the OP is looking for FileInputStream in conjunction with a Socket?
rkippena at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...
# 7

> Yes I know what a loop is.

I had to ask because you suggested the previous answer would be 'tedious' because there were more than a couple of lines in the file.

> And I know what a

> character is. Yes there is a reason why I am not

> using FTP. Mainly because my project calls for using

> a middleware and an specific messaging structure.

> Unfortunately the only way to send a file using this

> message structure is via a binary blob and the people

> I am working with what to give me a text file with

> the information that needs to be sent. It seems to me

> that a loop would be a very messy way to read the

> file and convert it to binary especially when I try

> and read it on the other end on a different computer.

> So I am essentially looking for a better way to do it

> than that. Thanks though.

And exactly what is a "binary blob"?

The closest to that in the standard Java API is the binary blob methods in JDBC. Are you using a JDBC driver?

If not the othe possibility is that it is just a byte array.

So which one is the target?

I suspect the second. If you look in the java.io package you will find several classes that specifically deal with streaming byte arrays.

jschella at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...
# 8

Maybe OP means a char set converter.

public byte[] convert(File myFile, Charset inCharset, Charset outCharset) throws IOException {

Reader reader = new InputStreamReader(new FileInputStream(myFile), inCharset);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

Writer writer = new OutputStreamWriter(baos, outCharset);

int c = reader.read();

while (c>=0) {

writer.write(c);

c = reader.read();

}

writer.close();

reader.close();

return baos.toByteArray();

}

parza at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...
# 9

> It seems to me

> that a loop would be a very messy way to read the

> file and convert it to binary especially when I try

> and read it on the other end on a different computer.

This makes no sense. Why would looping over the input have any effect on the computer that reads the result? Any solution you use for this is going to use a loop of some sort, whether you write the loop or not.

dubwaia at 2007-7-16 1:57:03 > top of Java-index,Other Topics,Algorithms...