Displaying hexidecimal numbers

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 585
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Displaying hexidecimal numbers

Post by Simon Knight » Fri Jul 31, 2020 1:34 pm

Hi,

I am working with a binary file. Its a raw image from a camera and I read two bytes of information and convert it to a decimal number with the following two lines of code:

Code: Select all

put byte tTagIDByte to tTagIDByte+1 of pImage into tTagIDHex
put binaryDecode ("S*",tTagIDHex,tTagID) into tItemCount
Ignore tItemcount the call puts the important return value into the specified variable, tTagID in this case.

It is useful to be able to view the value in hex when checking the accuracy of the pointers and display it in a field. To do this I use:

Code: Select all

put baseconvert(tTagID,10,16) into tTagIDHex
This all works but it has taken me a morning to discover the calls so I am wondering if there is another method of displaying the hex values directly from the file?

best wishes
S
best wishes
Skids

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 768
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Displaying hexidecimal numbers

Post by Thierry » Fri Jul 31, 2020 3:55 pm

Simon Knight wrote:
Fri Jul 31, 2020 1:34 pm
I am wondering if there is another method of displaying the hex values directly from the file
Hi Simon,

Looks good to me (I'm using these a lot), and I'm not aware of other functions,
but that's not a proof :)

Regards,

Thierry
Regex LiveCode sunnYrex
https://sunny-tdz.com

Simon Knight
Posts: 585
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Displaying hexidecimal numbers

Post by Simon Knight » Fri Jul 31, 2020 4:02 pm

Thanks! Good to know.
best wishes
Skids

Simon Knight
Posts: 585
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Displaying hexidecimal numbers

Post by Simon Knight » Sat Aug 01, 2020 7:42 am

I am still struggling!

The raw camera files may be either big endian or little endian and the endian is indicated by the first two bytes of the file. So I have written the following routine to read data once the byte order has been determined and stored in a variable "IsLittleEndian":

Code: Select all

Function ReadDataWord pLowByte, pHighByte, pIsLittleEndian, @pImageFile
   put empty into tDataWord
   If pIsLittleEndian  then
      ## Seems the wrong way around to me yet works
      put byte pLowByte to pHighByte of pImageFile into tdataWord
   Else
      Repeat with N = pHighByte down to pLowByte
         put byte N of pImageFile after tDataWord
      end Repeat
   end if
   return tDataWord
end ReadDataWord
My understanding of Endian is that we generally view all numbers as Big Endian i.e the first digit represents the largest multiplier and the last digit the units e.g 1234 decimal the 1 represents thousands the 4 single digits. Little Endian reverses the order of the digits e.g. 4321.

So far I have tested my routine with Little Endian data which means that I should be populating tDataWord from the highest byte position down to the lowest byte position to put the data into man readable big endian format. Yet the function above is working with Little Endian data when it should fail.

The data in tDataWord is passed back and processed by BinaryDecode. I use format types C, S or I. The dictionary describes the multi byte types as
S: convert next amount 2-byte chunks of data to unsigned integers in host byte order
Does "host byte order" mean that the call reads the bytes in the variable passed in a different order depending on the type of computer/processor/OS the application is being run on ? In other words should my routine be determining where it is being run, Apple,PC,Linux and extracting the bytes in the order of the host system in order to obtain the correct decodes from BinaryDecode.

So what is host byte order? The dictionary states
The order in which the local system (the "host") stores bytes when using multi- byte data types. This depends on the operating system and microprocessor your computer has.
Great, so do I need a database of OS and microprocessor combinations in order to use binary decode?

best wishes
perplexed of North Lincolnshire
best wishes
Skids

Simon Knight
Posts: 585
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Displaying hexidecimal numbers

Post by Simon Knight » Sat Aug 01, 2020 9:57 am

So I gave up on BinaryDecode and possible obscure features. Instead I do my conversion with this updated handler :

Code: Select all

Function ReadDataWord pLowByte, pHighByte, pIsLittleEndian, @pImageFile
   If pLowByte > pHighByte then
      put tLowByte into tTemp
      put pHighByte into tLowByte
      put tTemp into pHighByte
   end if
   put 0 into tValue
   put 0 into tPwr
   -- each loop starts with the units, followed by squared, followed by pwr 4, pwr 6 ...
   If pIsLittleEndian  then
      Repeat with N = pLowByte to pHighByte
         put byteToNum(byte N of pImageFile) into tInt
         add tInt*(16^tPwr) to tValue
         add 2 to tPwr
      end Repeat
   Else
      put "Big Endian : " into tdebug
      Repeat with N = pHighByte down to pLowByte
         put byteToNum(byte N of pImageFile) into tInt
         add tInt*(16^tPwr) to tValue
         add 2 to tPwr
      end Repeat
   end if
   return tValue
end ReadDataWord
Pass it two byte positions, a boolean flag for LittleEndian and the data to be read. Returns a base 10 value. Beware it does not check for possible overflow.
best wishes
Skids

Post Reply

Return to “Talking LiveCode”