Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Reversing bytes

I'm reading 16bit integers from a binary file and putting the values into a
spreadsheet. Unfortunately the binary file is big-endian, so all my extracted
integer values are wrong. What is the neatest way of reversing the bytes then
converting the number to an integer?

Grateful for help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Reversing bytes

You can use DEC2BIN() to convert the value to a bit string(s), then use
MID()'s to separate the individual bits. Apply a conversion algorithm and
then re-construct the bits.
--
Gary''s Student - gsnu200731


"simonc" wrote:

I'm reading 16bit integers from a binary file and putting the values into a
spreadsheet. Unfortunately the binary file is big-endian, so all my extracted
integer values are wrong. What is the neatest way of reversing the bytes then
converting the number to an integer?

Grateful for help.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Reversing bytes

Thanks for this but I'd prefer to do the conversion in the code. If I read
the two bytes into a byte array with

Get #1, , read_two_bytes(2)
Get #1, , read_two_bytes(1)

is there a way of converting the byte array to the integer value?

"Gary''s Student" wrote:

You can use DEC2BIN() to convert the value to a bit string(s), then use
MID()'s to separate the individual bits. Apply a conversion algorithm and
then re-construct the bits.
--
Gary''s Student - gsnu200731


"simonc" wrote:

I'm reading 16bit integers from a binary file and putting the values into a
spreadsheet. Unfortunately the binary file is big-endian, so all my extracted
integer values are wrong. What is the neatest way of reversing the bytes then
converting the number to an integer?

Grateful for help.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Reversing bytes

One way, simple but not the fastest -

Dim ig As Integer

ig = "&H" & Right$("0" & read_two_bytes(2), 2) & _
Right$("0" & read_two_bytes(1), 2)

Assumes read_two_bytes(2) is the high value byte. Also 'read_two_bytes' is
never an empty string, but if it might be include "00", or if always two
characters you wouldn't need the Right functions.

Are you absolutely sure your values represent integers and not 2-byte longs.
If longs add an "&" to the end of the string, but only if truly longs (also
assign the value to a variable declared as Long or directly to cell).

Regards,
Peter T



"simonc" wrote in message
...
Thanks for this but I'd prefer to do the conversion in the code. If I read
the two bytes into a byte array with

Get #1, , read_two_bytes(2)
Get #1, , read_two_bytes(1)

is there a way of converting the byte array to the integer value?

"Gary''s Student" wrote:

You can use DEC2BIN() to convert the value to a bit string(s), then use
MID()'s to separate the individual bits. Apply a conversion algorithm

and
then re-construct the bits.
--
Gary''s Student - gsnu200731


"simonc" wrote:

I'm reading 16bit integers from a binary file and putting the values

into a
spreadsheet. Unfortunately the binary file is big-endian, so all my

extracted
integer values are wrong. What is the neatest way of reversing the

bytes then
converting the number to an integer?

Grateful for help.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Reversing bytes

The values read from the file are 16bit two's complement integers with most
significant byte first.

Solved it in the end with:

dim integer_value as long
dim integer_16bit as integer
dim read_two_bytes(1 to 2) as byte
Get #1, , read_two_bytes
integer_value = read_two_bytes(1) * CLng(256) + read_two_bytes(2)
If read_two_bytes(1) 127 Then integer_16bit = integer_value - 2 ^ 16 Else
integer_16bit = integer_value


"Peter T" wrote:

One way, simple but not the fastest -

Dim ig As Integer

ig = "&H" & Right$("0" & read_two_bytes(2), 2) & _
Right$("0" & read_two_bytes(1), 2)

Assumes read_two_bytes(2) is the high value byte. Also 'read_two_bytes' is
never an empty string, but if it might be include "00", or if always two
characters you wouldn't need the Right functions.

Are you absolutely sure your values represent integers and not 2-byte longs.
If longs add an "&" to the end of the string, but only if truly longs (also
assign the value to a variable declared as Long or directly to cell).

Regards,
Peter T



"simonc" wrote in message
...
Thanks for this but I'd prefer to do the conversion in the code. If I read
the two bytes into a byte array with

Get #1, , read_two_bytes(2)
Get #1, , read_two_bytes(1)

is there a way of converting the byte array to the integer value?

"Gary''s Student" wrote:

You can use DEC2BIN() to convert the value to a bit string(s), then use
MID()'s to separate the individual bits. Apply a conversion algorithm

and
then re-construct the bits.
--
Gary''s Student - gsnu200731


"simonc" wrote:

I'm reading 16bit integers from a binary file and putting the values

into a
spreadsheet. Unfortunately the binary file is big-endian, so all my

extracted
integer values are wrong. What is the neatest way of reversing the

bytes then
converting the number to an integer?

Grateful for help.






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Reversing bytes

Ignore this!
wrongly assumed the bytes were written as hex instead (of course) as
numbers.

Peter T

"Peter T" <peter_t@discussions wrote in message
...
One way, simple but not the fastest -

Dim ig As Integer

ig = "&H" & Right$("0" & read_two_bytes(2), 2) & _
Right$("0" & read_two_bytes(1), 2)

Assumes read_two_bytes(2) is the high value byte. Also 'read_two_bytes' is
never an empty string, but if it might be include "00", or if always two
characters you wouldn't need the Right functions.

Are you absolutely sure your values represent integers and not 2-byte

longs.
If longs add an "&" to the end of the string, but only if truly longs

(also
assign the value to a variable declared as Long or directly to cell).

Regards,
Peter T



"simonc" wrote in message
...
Thanks for this but I'd prefer to do the conversion in the code. If I

read
the two bytes into a byte array with

Get #1, , read_two_bytes(2)
Get #1, , read_two_bytes(1)

is there a way of converting the byte array to the integer value?

"Gary''s Student" wrote:

You can use DEC2BIN() to convert the value to a bit string(s), then

use
MID()'s to separate the individual bits. Apply a conversion algorithm

and
then re-construct the bits.
--
Gary''s Student - gsnu200731


"simonc" wrote:

I'm reading 16bit integers from a binary file and putting the values

into a
spreadsheet. Unfortunately the binary file is big-endian, so all my

extracted
integer values are wrong. What is the neatest way of reversing the

bytes then
converting the number to an integer?

Grateful for help.





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
0 bytes andy Excel Discussion (Misc queries) 6 May 9th 08 01:28 PM
How many bytes? GARY Excel Discussion (Misc queries) 0 April 15th 06 09:31 PM
file 0 bytes Lyndon Baysic via OfficeKB.com Excel Discussion (Misc queries) 2 May 7th 05 12:47 PM
Get bytes from file Luis Amezcua Excel Programming 2 October 13th 04 09:15 PM
File with too Bytes? Pier Excel Programming 1 July 26th 03 03:28 AM


All times are GMT +1. The time now is 12:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"