Get binary file as hex string? - Mozilla
This is a discussion on Get binary file as hex string? - Mozilla ; I need to send the content of binary files (say PNGs, JPGs) over the web
as hex strings. To that end I used @mozilla.org/binaryinputstream;1 and
its method readBytes() and stored the bytes in a var called data.
Then assuming that ...
-
Get binary file as hex string?
I need to send the content of binary files (say PNGs, JPGs) over the web
as hex strings. To that end I used @mozilla.org/binaryinputstream;1 and
its method readBytes() and stored the bytes in a var called data.
Then assuming that in Javascript those bytes are interpreted as Unicode
chars I wrote the following:
[
pad() prepends zeroes to a string, until the string's length equals
the second argument: 8.
]
function byteArrayToHex( data ) {
var hex = "";
for ( i = 0; i < data.length; i++ ) {
// back to 0/1 land
var Byte = pad( data.charCodeAt(i).toString(2), 8 );
// split into chunks of length: 4
var bit1_4 = Byte.substring(0,4);
var bit5_8 = Byte.substring(4);
// convert to hex
bit1_4 = parseInt(bit1_4, 2).toString(16);
bit5_8 = parseInt(bit5_8, 2).toString(16);
// concatenate
hex += bit1_4 + bit5_8;
}
return hex;
}
The web service I'm sending the hex string to however says "Invalid
BinaryData", bummer. So, knowing there are lots more experienced people
out there, I ask: Can you spot some terribly obvious mistakes in the
code above? Is there a built-in way to do this in XPCOM? Will it rain
tomorrow?
Feel free to point out my utter lack of knowledge in this area.
Marc
-
Re: Get binary file as hex string?
Marc Diethelm wrote:
> I need to send the content of binary files (say PNGs, JPGs) over the
> web as hex strings. To that end I used
> @mozilla.org/binaryinputstream;1 and its method readBytes() and stored
> the bytes in a var called data.
> Then assuming that in Javascript those bytes are interpreted as
> Unicode chars I wrote the following:
I don't think they are. In fact, I'm not sure why that API exists,
because it's not binary-safe... Try using readByteArray instead.
--
Warning: May contain traces of nuts.
-
Re: Get binary file as hex string?
On approximately 4/3/2008 3:50 PM, came the following characters from
the keyboard of Marc Diethelm:
>
> Then assuming
> I ask: Can you spot some terribly obvious mistakes in the
> code above?
The first is often the source of the latter... you might try displaying
the content of what you are looking at to see what format it is in,
rather than assuming...
--
Glenn -- http://nevcal.com/
===========================
A protocol is complete when there is nothing left to remove.
-- Stuart Cheshire, Apple Computer, regarding Zero Configuration Networking
-
Re: Get binary file as hex string?
Glenn wrote:
> On approximately 4/3/2008 3:50 PM, came the following characters from
> the keyboard of Marc Diethelm:
>>
>> Then assuming
>
>> I ask: Can you spot some terribly obvious mistakes in the code above?
>
>
> The first is often the source of the latter... you might try displaying
> the content of what you are looking at to see what format it is in,
> rather than assuming...
>
Ah really. That's the first thing I did. Just not sure what I was
looking at... PNG
-
Re: Get binary file as hex string?
Neil wrote:
> Marc Diethelm wrote:
>
>> I need to send the content of binary files (say PNGs, JPGs) over the
>> web as hex strings. To that end I used
>> @mozilla.org/binaryinputstream;1 and its method readBytes() and stored
>> the bytes in a var called data.
>> Then assuming that in Javascript those bytes are interpreted as
>> Unicode chars I wrote the following:
>
> I don't think they are. In fact, I'm not sure why that API exists,
> because it's not binary-safe... Try using readByteArray instead.
Thanks Neil. The type returned by readBytes is string.
void readBytes ( PRUint32 length , out char* string )
However its description says: Read an opaque byte array from the stream.
So I switched to readByteArray. Data is now an array of decimal numbers.
Again I'm converting those to binary strings, padding to 8 digits (1 Byte).
Each Byte is again split into 2 chunks of 4 bits and the chunks
converted to hex strings. The hex strings are concatenated into THE hex
string.
Should work shouldn't it? However the server still rejects my data. If
no one spots an error in my reasoning, I shall try to contact the server
peeps 2morrow.
Marc
-
Re: Get binary file as hex string?
Marc Diethelm wrote:
> Glenn wrote:
>
>> On approximately 4/3/2008 3:50 PM, came the following characters from
>> the keyboard of Marc Diethelm:
>>
>>> I ask: Can you spot some terribly obvious mistakes in the code above?
>>
>> you might try displaying the content of what you are looking at to
>> see what format it is in, rather than assuming...
>
> Ah really. That's the first thing I did. Just not sure what I was
> looking at... PNG
You could run the file through a hexdump utility (e.g. od -t x1) to see
if your string agrees with the output.
--
Warning: May contain traces of nuts.
-
Re: Get binary file as hex string?
Marc Diethelm wrote:
>
> I need to send the content of binary files (say PNGs, JPGs) over the web
> as hex strings. To that end I used @mozilla.org/binaryinputstream;1 and
> its method readBytes() and stored the bytes in a var called data.
> Then assuming that in Javascript those bytes are interpreted as Unicode
> chars I wrote the following:
>
> [
> pad() prepends zeroes to a string, until the string's length equals
> the second argument: 8.
> ]
>
> function byteArrayToHex( data ) {
>
> var hex = "";
>
> for ( i = 0; i < data.length; i++ ) {
>
> // back to 0/1 land
> var Byte = pad( data.charCodeAt(i).toString(2), 8 );
> // split into chunks of length: 4
> var bit1_4 = Byte.substring(0,4);
> var bit5_8 = Byte.substring(4);
> // convert to hex
> bit1_4 = parseInt(bit1_4, 2).toString(16);
> bit5_8 = parseInt(bit5_8, 2).toString(16);
> // concatenate
> hex += bit1_4 + bit5_8;
> }
>
> return hex;
> }
>
> The web service I'm sending the hex string to however says "Invalid
> BinaryData", bummer. So, knowing there are lots more experienced people
> out there, I ask: Can you spot some terribly obvious mistakes in the
> code above? Is there a built-in way to do this in XPCOM? Will it rain
> tomorrow?
>
> Feel free to point out my utter lack of knowledge in this area.
>
> Marc
I think the problem is the code you use to generate the Hex
The nsICryptoHash example features code to convert binary data to hex:
http://developer.mozilla.org/en/docs...Hash_of_a_File
I once implemented this function for a testcase, which jwalden then
commented pretty good:
http://mxr.mozilla.org/seamonkey/sou...gorithms.js#40
Best, I think, would be to use it in conjunction with readBytesArray
like Neil suggested.