WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Conversion UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Unicode Endian Flipper UDF

 Keywords: Unicode Big Little Endian Flipper UDF 

; This file contains a "Unicode Endian Flipper" UDF. and some sample
; code that makes a Unicode file and "Flips" it.  WinBatch can read
; either a litttle endian or a big endian tyoe file, but can only create
; a little endian (MS Windows default) tyoe file.
; Tne UnicodeFilipper UDF will reverse the endian type of the file


;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


;UDF starts here
#DefineSubRoutine UnicodeFlipper(ftc) ; file to convert in place

fs=FileSize(ftc)
dwordcount= ((fs+8) / 4)  ; integer division (remainder dropping math)
bbsize = dwordcount * 4
dataA  = BinaryAlloc(bbsize)
dataB  = BinaryAlloc(bbsize)

maskbb = BinaryAlloc(bbsize)

;fill mask
mask4=( 255 << 16 ) | ( 255  )     ; 0x00FF00FF

; file mask BB
For xx4 = 0 To dwordcount-1
    BinaryPoke4(maskbb,xx4*4,mask4)
Next

; Read Data
BinaryReadEx(DataA,0,ftc,1,fs-1)
BinaryEodSet(DataA,fs)    ;expand as first char ignored and on is added later from DataB
BinaryReadEx(DataB,1,ftc,0,fs)

;MaskData
BinaryAnd(DataA,0,maskbb,0,fs)
BinaryAnd(DataB,1,maskbb,0,fs)

;merge data
BinaryOr(DataA,0,DataB,0,fs)

BinaryWrite(DataA,ftc)

BinaryFree(dataA)
BinaryFree(dataB)
BinaryFree(maskbb)
Return
#EndFunction

; to increase execution speed for *many* files, it is possible to slightly rewrite
; of avoid having to BinaryAllocate and BinaryFree buffers for each file.




;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


;Example starts here

;Set working folder to same folder WBT file is in
DirChange(DirScript())

fn="example-input.txt"
fn2="example-output.txt"
handle=FileOpen(fn, "WRITE", @TRUE) ; write a little endian unicode file

FileWrite(handle,"1a2b3c4d5e6f")
FileWrite(handle,"old pond")
FileWrite(handle,"a frog jumps")
FileWrite(handle,"the sound of water")

FileClose(handle)

;aa=BinaryAlloc(32)
;for aaa=0 to 15
;   BinaryPoke(aa,aaa*2,aaa)
;   BinaryPoke(aa,aaa*2+1,240+aaa)
;next
;
;BinaryWrite(aa,fn)
FileCopy(fn,fn2,0)

UnicodeFlipper(fn2)

Run("Browser.exe",fn)
Run("Browser.exe",fn2)
Message("UnicodeFlipper","Done")

Article ID:   W18350
Filename:   Unicode Endian Flipper UDF.txt
File Created: 2008:04:25:13:20:22
Last Updated: 2008:04:25:13:20:22