How to read/process a binary file as hexadecimal

new BookmarkLockedFalling
jstone
New Member
*

jstone Avatar

Posts: 2

Post by jstone on Sept 8, 2010 9:32:43 GMT -5

The problem I had was how to read hex values from a binary TarGA image file given that RB uses UTF-8 character encoding rather than ASCII. Trying to read hex value 0x80 (decimal value 128) from a binary file is a problem because RB's UTF-8 encoding interprets this character as the Euro symbol with a hex value of 0x20ac (decimal value 8364). The same thing occurs when reading/writing most characters between 128-159 in the ASCII table: the equivalent UTF-8 characters and corresponding hex/decimal values are completely different. Consequently, running the file's contents through RB's native hex functions returns an Unsupported Character error.

The below code demonstrates how RB can read a TarGA image file as hex and correct for the errors introduced by UTF-8 character encoding. The code uses the MID$ function as a substitute for the unsupported SEEK statement.

The attached zip file contains the full source code for my work-in-progress TarGA viewer, the text file used to correct the character encoding issues caused by UTF-8, and some sample flight simulator terrain textures as TarGA images. If you run the full source code then you'll need to amend the DefaultDir$ path to whatever folder you've stored the hexdec.txt file in.


dim hex2dec(32, 2)
dim tgaBytes$(fLen)

fileName$ = "A TarGA file"

[readDecimalFile]

'hexdec.txt is a list of the offending 32 utf-8 decimals and ASCII counterparts

open "hexdec.txt" for input as #hex2dec
for x = 1 to 32
input #hex2dec, hex2dec(x, 1)
input #hex2dec, hex2dec(x, 2)
next x
close #hex2dec

[readTargaFile]

'Get contents of the TarGA file

open fileName$ for input as #tgaFile
fLen = lof(#tgaFile)
redim tgaBytes$(fLen)
fileText$ = input$(#tgaFile, fLen)
close #tgaFile

'Convert bytes to decimal, convert incorrect decimal values, convert to hex

for aLoop = 1 to fLen
tgaBytes$(aLoop) = mid$(fileText,ドル aLoop, 1)
aByte = hexdec(dechex$(asc(tgaBytes$(aLoop))))
if aByte > 255 then
tgaBytes$(aLoop) = dechex$(decConv(aByte))
else
tgaBytes$(aLoop) = dechex$(asc(tgaBytes$(aLoop)))
end if
if len(tgaBytes$(aLoop)) = 1 then
tgaBytes$(aLoop) = "0" + tgaBytes$(aLoop)
end if
next aLoop

wait ' insert code to process/render the image

'Function to convert UTF-8 decimals to ASCII decimals

function decConv(bByte)
for x = 1 to 32
if bByte = hex2dec(x, 1) then
decConv = hex2dec(x, 2)
exit for
end if
next x
end function



Attachments:



StefanPendl
Global Moderator
*****

StefanPendl Avatar

Run for BASIC ...
Posts: 945

[b]Stefan[/b] - [a href=http://stefanpendl.runbasichosting.com/]Homepage[/a][br][br][b]Please give credit if you use code I post, no need to ask for permission.[/b][br][br]Run BASIC 1.01, Fire-/Waterfox (IE11, Edge), Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
jstone
New Member
*

jstone Avatar

Posts: 2

Post by jstone on Sept 9, 2010 2:55:13 GMT -5

StefanPendl Avatar
Open as binary and read the file in chunks.

If it is a binary file, never treat it as a plain text file.


Oops. Yes, I know that.

My tired eyes interpreted runbasic.proboards.com/index.cgi?action=display&board=question&thread=76&page=1 to read "binary files totally unsupported - ingenious workaround required". Looks like I've created a solution to a non-existent problem. Ho hum. I retreat, I withdraw.
StefanPendl
Global Moderator
*****

StefanPendl Avatar

Run for BASIC ...
Posts: 945

[b]Stefan[/b] - [a href=http://stefanpendl.runbasichosting.com/]Homepage[/a][br][br][b]Please give credit if you use code I post, no need to ask for permission.[/b][br][br]Run BASIC 1.01, Fire-/Waterfox (IE11, Edge), Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM