Decode Bard's Tale Huffman files

Any developer realated stuff
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Decode Bard's Tale Huffman files

Post by Caracas »

** THIS POST HAS BEEN UPDATED AFTER I FINISHED MY SCRIPT **

Why use pen and paper when you have vbscript? :D

First, I use this script:

Code: Select all

Dim objFSO, objTextFile, objHexFile
Dim strCharacter

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objHexFile = objFSO.OpenTextFile ("C:\Huffman\B0.txt", 1)
Set objTextFile = objFSO.OpenTextFile ("C:\Huffman\Binary.txt", 8, True)

Do Until objHexFile.AtEndOfStream 
  strCharacter = objHexFile.Read(1)
  if strCharacter <= "9" then 
    OneDigit = asc(strCharacter) - asc("0") 
  else 
    OneDigit = asc(strCharacter) - asc("A") + 10 
  end if 
  PowerOfTwo = 8 
  while PowerOfTwo >= 1 
    if OneDigit >= PowerOfTwo then 
      objTextFile.Write("1") 
      OneDigit = OneDigit - PowerOfTwo 
    else 
      objTextFile.Write("0") 
    end if 
    PowerOfTwo = PowerOfTwo / 2 
  wend 
Loop
This script will give me a conversion from hex to binary.
In order to get this work, you need the hex values of any Bard's Tale Huffman file in a text file, except the first 8 bytes since they're not part of the Huffman encoding.

Next I run following script:

Code: Select all

Dim objFSO, objCodeFile, objDecodeFile
Dim strCharacters, code, hexvalue 
Dim nrArray, nrHuffman, nrCode 
Dim HuffmanCode, Huffmanhex
dim lngResult
dim intIndex
dim hexcodearray()
dim bincodearray()
dim hufcodearray()
dim TempBin
dim TotalBin, TempLength, MaxLength
dim FindRight, Number
dim Match
dim FinalHex
dim FoundIt
code = "" 
hexvalue = "" 
nrArray = 0 
HuffmanCode = "" 
nrCode = 0 
Huffmanhex = ""
lngResult = 0
TotalBin = 0
TempLenght = 0
MaxLength = 0
TempBin = ""
FindRight = 0
Number = 1
Match = ""
FinalHex = ""
FoundIt = 0
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\Huffman\Binary.txt", 1) 
Set objCodeFile = objFSO.OpenTextFile ("C:\Huffman\Code.txt", 8, True) 
Do Until objFile.AtEndOfStream 
    strCharacters = objFile.Read(1)       'Start reading the file, bit by bit 
    if strCharacters = "0" then 
      code = code + "0"          'If a bit = 0, add it to the code 
      TotalBin = TotalBin + 1
    else 
      TotalBin = TotalBin + 1
      code = code + "1"            'We hit a leaf! 
        if len(code) = 1 then          'End of the Huffman tree 
        exit do             'Bye bye 
        end if 
      hexvalue = objfile.read(8)       '8 bits = 1 byte, need to translate to Hex still 
      TotalBin = TotalBin + 8
      If nrArray = 0 then         'This is just the first few bits, start of Huffman code 
        nrHuffman = Len(code) - 1 
        Huffmancode = Left(code, nrHuffman) 
      Else               'All the other codes are built here 
        do while Right(Huffmancode, 1)="1"   ' If there's any "1", remove them 
          nrHuffman = Len(Huffmancode) - 1 
          Huffmancode = Left(Huffmancode, nrHuffman) 
        loop 
        nrHuffman = Len(Huffmancode) - 1 
        If nrHuffman < 0 then                                         'Added safety check 
          exit do 
        End if 
        Huffmancode = Left(Huffmancode, nrHuffman) 
        Huffmancode = Huffmancode + "1"      'Replace the last "0" by a "1" 
        nrCode = Len(code) - 2         'Remove 2, the first "0" and the last "1" 
        if nrCode > 0 then 
          for j = 1 to nrCode 
            Huffmancode = Huffmancode + "0"   'Add a "0" for each remaining "0" 
          next 
        End if 
      End if 
      for intIndex = len(hexvalue) to 1 step -1
        strDigit = mid(hexvalue, intIndex, 1)
        select case strDigit
          case "0"
            ' do nothing
          case "1"
            lngResult = lngResult + (2 ^ (len(hexvalue)-intIndex))
          case else
            ' invalid binary digit, so the whole thing is invalid
            lngResult = 0
            intIndex = 0 ' stop the loop
        end select
      next
      Huffmanhex = hex(lngResult)
            If Len(HuffmanHex) = 1 then
               Huffmanhex = "0" + Huffmanhex
            End if
      redim preserve hexcodearray(nrarray)   'rebuild the array and preserve previous data
      redim preserve bincodearray(nrarray)
      redim preserve hufcodearray(nrarray)
      hexcodearray(nrarray) = Huffmanhex    'add values to the array
      bincodearray(nrarray) = hexvalue
      hufcodearray(nrarray) = Huffmancode
      TempLength = len(hufcodearray(nrarray))  'This is used during the actual decoding process
      if TempLength > MaxLength then     'determining which Huffman code has the most bits
         MaxLength = TempLength
      else
         TempLength = 0
      End if
      objCodeFile.WriteLine(chr(40) & code & chr(41) & chr(9) & hexvalue & chr(9) & Huffmancode & chr(9) & Huffmanhex) 
      code = "" 
      hexvalue = "" 
      nrHuffman = 0 
      nrCode = 0 
      nrArray = nrArray + 1
      lngResult = 0
    end if 
Loop 
objFile.close
Set objDecodeFile = objFSO.OpenTextFile("C:\Huffman\Binary.txt", 1) 
TempBin = objDecodeFile.ReadAll    'Read the entire file and put it in a variable
TempBin = Mid(TempBin, TotalBin)   'Start to read from where the Huffman tree stops
nrarray = nrarray - 1
MaxLength = MaxLength - 1
FindRight = len(TempBin)
do while FindRight > 0 
 for i = 0 to MaxLength
 Match = left(TempBin,Number)     'Start adding bits to the Match variable
   for j = 0 to nrArray          'check all the Huffmancodes if we have a match
     if Match = hufcodearray(j) then
     FinalHex = FinalHex + hexcodearray(j) 'We have a match! Add the decoded hex to the FinalHex variable
     FOundIt = 1                'used for quiting 'for..next', otherwise Match keeps on growing
     exit for
     end if
   next
 if FoundIt = 1 then   'quiting for..next here
  exit for
 end if
 Number = Number + 1
 next
 FindRight = FindRight - Number
 if FindRight < 1 then   'We reached the end of the file!
   exit do
 end if
 TempBin = right(TempBin,FindRight) 'remove the bits we just matched
 Number = 1
 FoundIt = 0
 Match = ""
loop
objCodeFile.Write(FinalHex)  'write the decoded hex to file
objDecodeFile.Close
objCodeFile.Close
This script first builds the Huffman tree and then decodes the remaining bits and translates them to hexadecimal again. The bits come from the file that was created by the first script. As you can see in the first script, I have been experimenting with B0.HUF

EDIT: corrected a little error.
Last edited by Caracas on Tue Oct 25, 2011 11:06 am, edited 3 times in total.
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

** THIS POST HAS BEEN UPDATED **

All you need to do to get this to work is to copy the hexadecimal values from a Bard's Tale Huffman file to a text file, except the first 8 bytes.

So for the CITY.PAT file, that would be:

Code: Select all

10 34 08 80 82 D0 35 0D 08 C4 B0 26 6D 91 58 04 48 23 88 59 78 4A D7 14 85 04 40 39 20 14 1D 14 1C 14 03 94 92 05 24 7F FF FE 47 74 34 0B 87 54 5C 19 0E 1F 61 63 E1 3E CD 9E 6F D7 EA 7B AD 17 DB 89 AE 83 99 87 60 50 DD 8E B3 CC 7F E2 44 14 64 2D 07 B9 42 8B E9 EE AE 92 F6 57 F7 FA FE 1F F3 0A C6 1D C1 53 2A 53 A1 2E 0D F7 EF E6 F6 AC C4 A3 EF E5 0A 21 F8 B4 1E 45 D4 87 1B 45 6D B6 DB 70 4C 6D 44 23 5F BC B7 FE F3 F0 88 AA 34 5D BF B5 8A A7 BA 29 89 CC 3F 4D FD AD F8 6A 6C 6A 49 22 CB F2 25 BF B5 A7 57 32 82 17 82 B7 FF 6B 20 4F CB CF BD 17 0D B6 DB 77 CD 68 BA 7E E6 3A 21 C8 9F BF 91 F9 9E 87 54 0E 0C 64 51 AC 1B 99 0F EF 4D E2 D0 DC 56 9F FA D1 C5 EB DE 34 5C 10 E9 D1 68 F7 AD 50 65 16 5F E8 F8 68 75 2C AD F1 9D 1F 2F F9 AB 4C B0 51 1B 90 EE 3F 91 78 AE FB FF E7 6D 9A 8A 46 C5 B0 90 41 4E 0B AF 67 7A C0 6A B7 1F FC A7 68 FA 97 17 05 2F FF F4 E0 D7
Remove the spaces ('Replace' ftw) and make sure the name of the text file matches the one in the first script.
Last edited by Caracas on Mon Oct 24, 2011 4:09 pm, edited 1 time in total.
Maven
Posts: 138
Joined: Sat Apr 16, 2011 9:39 pm

Post by Maven »

Good job, Caracas. I think we're all fans of making the computer do as much of the work as possible.

Well, if you're not a scripter, and you keep this up, it won't be long until you ARE a scripter.

If you get stuck, or want some pointers with something specific, ask.

This is probably not high on your list of things to do next, but here's a way to get rid of the big select statement in your first script:

Code: Select all

for i = 1 to len(HexNum)
 HexNumCalc = left(HexNum,i)
 HexNumFinal = right(HexNumCalc,1)
  if HexNumFinal <= "9" then
    OneDigit = asc(HexNumFinal) - asc("0")
  else
    OneDigit = asc(HexNumFinal) - asc("A") + 10
  end if
  PowerOfTwo = 8
  while PowerOfTwo >= 1
    if OneDigit >= PowerOfTwo then
      BinNum = BinNum + "1"
      OneDigit = OneDigit - PowerOfTwo
    else
      BinNum = BinNum + "0"
    end if
    PowerOfTwo = PowerOfTwo / 2
  wend
HexNumFinal=""
next
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

Thanks, Maven.

I think I first need to try and complete my script to decode the binary of the Huffman files.
I still need to build the actual Huffman codes. I guess I need to get them into an array so I can use them to decode the remaining of the file.
Another array to store the corresponding hexvalues and I should be there.

I've been stuck so many times already, I lost count :)
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

Wooot, went easier then expected :D

Modified my 2nd script from my first post a bit

Code: Select all

Dim objFSO, objCodeFile 
Dim strCharacters, code, hexvalue
Dim nrArray, nrHuffman, nrCode
Dim HuffmanCode

code = "" 
hexvalue = "" 
nrArray = 0
HuffmanCode = ""
nrCode = 0

Set objFSO = CreateObject("Scripting.FileSystemObject") 

Set objFile = objFSO.OpenTextFile("C:\Huffman\Binary.txt", 1) 
Set objCodeFile = objFSO.OpenTextFile ("C:\Huffman\Code.txt", 8, True) 

Do Until objFile.AtEndOfStream 
    strCharacters = objFile.Read(1) 		'Start reading the file, bit by bit
    if strCharacters = "0" then 
      code = code + "0" 			'If a bit = 0, add it to the code
    else 
      code = code + "1"				'We hit a leaf! 
        if len(code) = 1 then 			'End of the Huffman tree
        msgbox ("Done!") 
        wscript.quit 				'Bye bye
        end if 
      hexvalue = objfile.read(8) 		'8 bits = 1 byte, need to translate to Hex still
      If nrArray = 0 then			'This is just the first few bits, start of Huffman code
        nrHuffman = Len(code) - 1
        Huffmancode = Left(code, nrHuffman)
      Else					'All the other codes are built here
        do while Right(Huffmancode, 1)="1"	' If there's any "1", remove them
          nrHuffman = Len(Huffmancode) - 1
          Huffmancode = Left(Huffmancode, nrHuffman)
        loop
        nrHuffman = Len(Huffmancode) - 1
        If nrHuffman < 0 then                                         'Added safety check
          msgbox ("Done")
          wscript.quit
        End if 
        Huffmancode = Left(Huffmancode, nrHuffman)
        Huffmancode = Huffmancode + "1"		'Replace the last "0" by a "1"
        nrCode = Len(code) - 2			'Remove 2, the first "0" and the last "1"
        if nrCode > 0 then
          for j = 1 to nrCode
            Huffmancode = Huffmancode + "0"	'Add a "0" for each remaining "0"
          next
        End if
      End if
      
      objCodeFile.WriteLine(chr(40) & code & chr(41) & chr(9) & hexvalue & chr(9) & Huffmancode) 
      code = "" 
      hexvalue = ""
      nrHuffman = 0
      nrCode = 0
      nrArray = nrArray + 1 
    end if 
Loop 

objCodeFile.Close
This gives me following result on the CITY.NAM file

Code: Select all

(000001)	00010011	00000
(01)	00100001	00001
(001)	00010001	00010
(0001)	00001000	0001100
(0001)	00011100	000110100
(001)	00110001	0001101010
(01)	00101011	0001101011
(01)	00001110	00011011
(01)	00001010	000111
(01)	00000100	001
(00001)	00001101	01000
(01)	00000110	01001
(001)	00010010	01010
(001)	00010000	010110
(01)	00001111	010111
(01)	00000011	011
(0001)	00000010	100
(0001)	11111111	10100
(01)	00001011	10101
(00001)	00001001	1011000
(01)	00000101	1011001
(01)	00000111	101101
(01)	00001100	10111
(001)	00000001	110
(01)	00000000	111
tpth
Posts: 128
Joined: Tue Feb 02, 2010 6:39 am

Post by tpth »

Hey, so what's a Huffman file...?
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

http://en.wikipedia.org/wiki/Huffman_coding

Quite a few files seem to be encoded with a Huffman table:
- B0.HUF, B1.HUF, B2.HUF and B3.HUF: these are the pictures of the different houses ingame.
- the 47 file. Not sure what that file holds. It only gets loaded once, when the game begins.
- BARDSCR and BARDTIT, both get loaded only once, when the game begins.
- BIGPIC: pictures of all the monsters ingame
- CITY.NAM and CITY.PAT: map of Skara Brae and the map of the streetnamed.
- CITYPICS.BIN: not sure what this file holds.
- DPICS0, DPICS1 and DPICS2: the different walls in the dungeons.
- LEVS: maps and other info of all the dungeons.
Maven
Posts: 138
Joined: Sat Apr 16, 2011 9:39 pm

Post by Maven »

Nice job, Caracas.

Don't try to tell us you got THAT off the webz. Maybe you're already more of a scripter than you are letting on.

What's next? Converting Binary to Hex? You can do that with another big case statement, but consider using a lookup table. Something like this:

Code: Select all

HexTable = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F")
...
BinaryNumber = 0
for i = 1 to 8
  BinaryDigit = objfile.read(1)
  BinaryNumber = BinaryNumber * 2
  if BinaryDigit = "1" then
    BinaryNumber = BinaryNumber + 1
  end if
next
HexValue = HexTable(BinaryNumber / 16) + HexTable(BinaryNumber mod 16)
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

Well, in my younger days (loooong ago :( ) I used to program in Basic. A lot of those commands and logics are in scripting as well. But for exact commands and formatting, I need the internet.
I'm finding www.w3schools.com a very useful site.

2 more things I need to do in my script: I need to convert the decoded binary associated to each huffman code into hexadecimal and after that decode the rest of the binary with that Huffman code.

I was looking yesterday at using dynamic arrays. That's one of the reasons I already have that nrArray variable in my script. I think I can use that variable to build my array. I hope the ReDim preserve does what it's supposed to do :)
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

script version 564327.54 v456 build 236:

Code: Select all

Dim objFSO, objCodeFile 
Dim strCharacters, code, hexvalue 
Dim nrArray, nrHuffman, nrCode 
Dim HuffmanCode, Huffmanhex
dim lngResult
dim intIndex

code = "" 
hexvalue = "" 
nrArray = 0 
HuffmanCode = "" 
nrCode = 0 
Huffmanhex = ""
lngResult = 0

Set objFSO = CreateObject("Scripting.FileSystemObject") 

Set objFile = objFSO.OpenTextFile("C:\Huffman\Binary.txt", 1) 
Set objCodeFile = objFSO.OpenTextFile ("C:\Huffman\Code.txt", 8, True) 

Do Until objFile.AtEndOfStream 
    strCharacters = objFile.Read(1)       'Start reading the file, bit by bit 
    if strCharacters = "0" then 
      code = code + "0"          'If a bit = 0, add it to the code 
    else 
      code = code + "1"            'We hit a leaf! 
        if len(code) = 1 then          'End of the Huffman tree 
        exit do             'Bye bye 
        end if 
      hexvalue = objfile.read(8)       '8 bits = 1 byte, need to translate to Hex still 
      If nrArray = 0 then         'This is just the first few bits, start of Huffman code 
        nrHuffman = Len(code) - 1 
        Huffmancode = Left(code, nrHuffman) 
      Else               'All the other codes are built here 
        do while Right(Huffmancode, 1)="1"   ' If there's any "1", remove them 
          nrHuffman = Len(Huffmancode) - 1 
          Huffmancode = Left(Huffmancode, nrHuffman) 
        loop 
        nrHuffman = Len(Huffmancode) - 1 
        If nrHuffman < 0 then                                         'Added safety check 
          exit do 
        End if 
        Huffmancode = Left(Huffmancode, nrHuffman) 
        Huffmancode = Huffmancode + "1"      'Replace the last "0" by a "1" 
        nrCode = Len(code) - 2         'Remove 2, the first "0" and the last "1" 
        if nrCode > 0 then 
          for j = 1 to nrCode 
            Huffmancode = Huffmancode + "0"   'Add a "0" for each remaining "0" 
          next 
        End if 
      End if 

      for intIndex = len(hexvalue) to 1 step -1       'first convert binary to decimal
        strDigit = mid(hexvalue, intIndex, 1)
        select case strDigit
          case "0"
            ' do nothing
          case "1"
            lngResult = lngResult + (2 ^ (len(hexvalue)-intIndex))
          case else
            ' invalid binary digit, so the whole thing is invalid
            lngResult = 0
            intIndex = 0 ' stop the loop
        end select
      next
      Huffmanhex = hex(lngResult)                  'Then convert decimal to hex
            If Len(HuffmanHex) = 1 then
               Huffmanhex = "0" + Huffmanhex       'Otherwise the first "0" doesn't show
            End if
      objCodeFile.WriteLine(chr(40) & code & chr(41) & chr(9) & hexvalue & chr(9) & Huffmancode & chr(9) & Huffmanhex) 
      code = "" 
      hexvalue = "" 
      nrHuffman = 0 
      nrCode = 0 
      nrArray = nrArray + 1
      lngResult = 0
    end if 
Loop 

msgbox ("Done!!")

objCodeFile.Close
Now also translates the decoded binary to its corresponding hexadecimal value. Gives me following result on the binaries of the CITY.NAM file;

Code: Select all

(000001)	00010011	00000	13
(01)	00100001	00001	21
(001)	00010001	00010	11
(0001)	00001000	0001100	08
(0001)	00011100	000110100	1C
(001)	00110001	0001101010	31
(01)	00101011	0001101011	2B
(01)	00001110	00011011	0E
(01)	00001010	000111	0A
(01)	00000100	001	04
(00001)	00001101	01000	0D
(01)	00000110	01001	06
(001)	00010010	01010	12
(001)	00010000	010110	10
(01)	00001111	010111	0F
(01)	00000011	011	03
(0001)	00000010	100	02
(0001)	11111111	10100	FF
(01)	00001011	10101	0B
(00001)	00001001	1011000	09
(01)	00000101	1011001	05
(01)	00000111	101101	07
(01)	00001100	10111	0C
(001)	00000001	110	01
(01)	00000000	111	00
next up... decoding the remaining of the CITY.NAM binaries. This should be the final stage of the decoding process.

EDIT: changed the way the script ends. There were 2 wscript.quit commands in there. Now I just exit the loop and only have 1 message box with done!!
I think I'll need this to continue with the rest of the decoding.
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

I updated the first 2 posts, since I think I completed my scripts.
Not going to spam these forums with more code then necessary :D

Thanks Maven for the suggestions, they were realy helpful :!:
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

Just tried my scripts on the CITY.PAT file and created following little script (specificaly for CITY.PAT file)

Code: Select all

Dim objFSO, objTextFile, objHexFile
Dim strCharacter

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objHexFile = objFSO.OpenTextFile ("C:\Huffman\code.txt", 1)
Set objTextFile = objFSO.OpenTextFile ("C:\Huffman\Map.txt", 8, True)

Do Until objHexFile.AtEndOfStream 
  for a = 0 to 29
   for i = 0 to 29
    strCharacter = objHexFile.Read(2)
    Select case strCharacter
      case "00"
        objTextFile.write("  ")
      case "01"
        objTextFile.write("Ho")
      case "02"
        objTextFile.write("Ho")
      case "03"
        objTextFile.write("Ho")
      case "04"
        objTextFile.write("04")
      case "0B"
        objTextFile.write("0B")
      case "1C"
        objTextFile.write("1C")
      case "2B"
        objTextFile.write("2B")
      case "21"
        objTextFile.write("21")
      case "12"
        objTextFile.write("12")
      case "60"
        objTextFile.write("60")
      case "68"
        objTextFile.write("68")
      case "71"
        objTextFile.write("71")
      case "78"
        objTextFile.write("78")
      case "81"
        objTextFile.write("81")
      case "89"
        objTextFile.write("89")
      case "91"
        objTextFile.write("91")
      case "9B"
        objTextFile.write("9B")
      case "A1"
        objTextFile.write("A1")
      case "A8"
        objTextFile.write("A8")
    End Select
   next
  objTextFile.WriteLine()
  next
loop
Running this script on the decoded CITY.PAT file gives following result:

Code: Select all

      HoHoHoHoHoHoHo04HoHoHoHo04HoHoHoHoHo04HoHoHoHoHoHoHoHo
HoHoHo                                          HoHo      04
HoHo    HoHoHo04Ho04Ho  HoHo04HoHo  HoHo04Ho04    68  91  Ho
Ho04    60  60      Ho  04      Ho            Ho  Ho      04
Ho    HoHo04    21  Ho  Ho  Ho      HoHo04Ho  Ho  Ho0468HoHo
Ho    049B  60      Ho  Ho                    Ho  HoHo  HoHo
HoHo  HoHoHo    HoHo04  04HoHoHo  HoHo04    Ho04  04Ho  04Ho
HoHo        60Ho              04          Ho              Ho
04            Ho  HoHo  89Ho    04HoHoHo  Ho  HoHo  HoHo04Ho
Ho  Ho04HoHo  04    04      04        Ho  04  2BHo  Ho  HoHo
Ho        04        HoHoHo  HoHoHo04      Ho  12Ho        Ho
Ho  04Ho  HoHo04Ho    12Ho  2121212104HoHoHo  Ho    1C  HoHo
Ho  Ho            04  Ho21                04  Ho          Ho
HoHoHo  Ho  HoHo  HoHo0421          71Ho  Ho  Ho04  04HoHoHo
A8      Ho  Ho04      Ho21          7104      Ho0B  81HoHoHo
HoHoHo6004      HoHo  Ho21          71Ho04Ho  04Ho  HoHoHoHo
Ho  04  HoHoHo    04                71HoHoHo  Ho          Ho
04  Ho        04  HoHo04Ho21212121  04    Ho  Ho    HoHo04Ho
04      04Ho  Ho      HoHoHo04HoHo  Ho          04        Ho
Ho            Ho  Ho      HoHo      Ho  HoHoHo    HoHo  Ho  
  HoHo  HoHo  Ho    HoHo  04Ho  HoHo04        04  Ho21    Ho
  12Ho  04Ho  Ho    Ho04              Ho    HoHo    Ho  04  
Ho        04  Ho    HoHo04HoHoHo04Ho04HoHo1204  04  Ho    Ho
  HoHo60HoHo6004          HoHo        Ho12Ho    Ho  Ho60Ho  
  04    Ho    Ho    HoHo        Ho04            Ho  Ho  12  
  Ho68Ho  Ho  Ho    04HoHo0404Ho              HoHo  04  Ho  
Ho      04    04                    Ho    2160  Ho  Ho  HoHo
Ho  A1  68  HoHoHoHoHoHo04HoHo04Ho        60    04  Ho    Ho
Ho78    04Ho                      HoHo041204    Ho  Ho04Ho  
04HoHoHoHo                                Ho04HoHo  Ho      
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

If you ever need to remove the high order bit on a serie of hexadecimals, you can use this script (i'm on a roll!):

Code: Select all

Dim objFSO, objTextFile, objHexFile
Dim strCharacter, strReplaced

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objHexFile = objFSO.OpenTextFile ("C:\Huffman\HighOrder_bin.txt", 1)
Set objTextFile = objFSO.OpenTextFile ("C:\Huffman\No_HighOrder_bin.txt", 8, True)

Do Until objHexFile.AtEndOfStream 
  strCharacter = objHexFile.Read(8)
  strReplaced = Replace(strCharacter,"1","0",1,1)
  for intIndex = len(strReplaced) to 1 step -1
    strDigit = mid(strReplaced, intIndex, 1)
    select case strDigit
      case "0"
      case "1"
        lngResult = lngResult + (2 ^ (len(strReplaced)-intIndex))
      case else
        lngResult = 0
        intIndex = 0
    end select
  next
  ReplacedHex = hex(lngResult)
    If Len(ReplacedHex) = 1 then
       ReplacedHex = "0" + ReplacedHex
    End if
  objTextFile.write(ReplacedHex)
  strCharacter = ""
  strReplaced = ""
  ReplacedHex = ""
  lngResult = 0
  intIndex = 0
Loop

objHexFile.Close
objTextFile.Close
Make sure you have translated the hexadeciml values to binaries first.
tomas498
Posts: 8
Joined: Tue Nov 29, 2011 10:05 pm

Post by tomas498 »

hi :) i would be very grateful if someone could help me with this :/

i need to make something like this for my homework:

Compression/Decompression script using Huffman coding.

1. script) From .TXT(ascii) to .PAC file
2. script) From .PAC file to .TXT(ascii)

I hope someone help me :(
Caracas
Posts: 89
Joined: Thu Jan 20, 2011 9:16 am
Location: Belgium

Post by Caracas »

Hi Tomas,

Not sure this forum is the right place for you.
What exactly do you need? Huffman compression/decompression or converting from txt to pac?
Post Reply