Converting downloaded binary data to decimal

8 views (last 30 days)
Hey folks,
I'm using a web service that is responding to queries with binary data and I'm really struggling to convert this to a more usable format. Fortunately, the service does provide an example data case and they explain the formats and definitions. The problem is that I'm not familiar with converting data types like this and haven't found the right recipe.
I'm attaching a .mat file with three examples in it. Each example has what the web service query returned (raw), what the web service documentation says should be returned (not sure what this format is called, but I called it hex... maybe it's unicode???), and what the web service documentation says the data represents (decimal values).
Example 1: The Matlab web query returns "A33" (without quotations). The documentation says this should show up as "47 97 33 33". Is this unicode, hex, other??? I call it "hex" in my file format. According to documentation, this represents a float number of length (8-bit bytes) of 4. In this case, the represented number is "18.90". Furthermore, they define "Float" as a single-precision floating point 32-bit IEEE 754 size/format.
I'm struggling to get the raw "A33" to look like the documentation, but I can get close if I use:
dec2hex(unicode2native('A33','US-ASCII')) % returns "41 1A 33 33" (1A element SHOULD be 97)
Example 2: raw returned data is "G" (without quotes). Documentation says this should be "47 0F C6 14" that represents 3,680,608. Here's what I get:
dec2hex(unicode2native('G','US-ASCII')) % returns "47 0F 1A 14" (1A element SHOULD be C6)
Example 3: raw returned data is " jh" (without quotes). Documentation says this should be "00 00 01 16 6A E0 68 80" that represents a timestamp of November 23, 2007 (I assume it's a number of days measured from Jan 1, 1970, or perhaps from year 0. What I get:
dec2hex(unicode2native(' jh','US-ASCII')) % returns "20 20 01 16 6A 1A 68 1A" (four of these elements are wrong)
So here are my questions on the above three examples:
1) Do you have input on how I'm trying to convert the raw data to usable numbers? I'm totally unfamiliar with this area of computing.
2) Once I do get the raw data to look like what the documentation specifies (e.g., "47 97 33 33"), do you know how I convert it to decimal (e.g., 18.90)?
3) Is it possible that the web request itself is not correct and the data I'm getting back does not match what is expected? In this case, could it be something where I need to change the web options in the web request?
Any input on this matter is much appreciated!!!
Thanks,
Paul

Accepted Answer

Walter Roberson
Walter Roberson on 25 Apr 2017
>> examples(2).dec
ans =
3680608
>> bytes = uint8(cellfun(@hex2dec, examples(2).hex)); typecast(fliplr(bytes),'single')
ans =
single
36806.08
This is exactly 1/100 of value listed as expected.
>> typecast(fliplr(uint8(examples(2).raw+0)),'single')
ans =
single
36863.08
>> sprintf('%02x', uint8(examples(2).raw+0))
ans =
'470fff14'
>> examples(2).hex
ans =
1×4 cell array
'47' '0F' 'C6' '14'
The .hex that was given does not agree with the bytes stored in the .raw .
>> examples(1).dec
ans =
18.9
>> typecast(fliplr(uint8(cellfun(@hex2dec, examples(1).hex))),'single')
ans =
single
18.9
>> sprintf('%02x', uint8(examples(1).raw+0))
ans =
'41ff3333'
The hex that was given does not agree with the bytes stored in the .raw
>> examples(3).hex
ans =
1×8 cell array
'00' '00' '01' '16' '6A' 'E0' '68' '80'
>> sprintf('%02x', uint8(examples(3).raw+0))
ans =
'000001166aff68ff'
The hex that was given does not agree with the bytes stored in the .raw .
In each of the three cases of disagreement, the raw has an FF instead of the expected byte.
I have not been able to figure out the structure of the third item.
  10 Comments
Walter Roberson
Walter Roberson on 25 Apr 2017
Yes, milliseconds since that time is 1000 times Posix Time (which is seconds since then.) datetime() with 'posixtime' can convert that.
Paul Shoemaker
Paul Shoemaker on 26 Apr 2017
Walter, I didn't see all of your responses as I typed my messages above (wasn't doing page refreshes), but thanks to your huge help it's solved.
Thank you so much again!

Sign in to comment.

More Answers (1)

Rohit chand
Rohit chand on 9 Jun 2020
Your data logger shows the decimal numbers 47 and 21875.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!