Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a binary file 2

Status
Not open for further replies.

LuigiG

Programmer
Jun 9, 2011
8
IT
Hello,

I need to create a binary file inserting a list of numbers 1 2 3 4 5 6 .. is there functions to do it? I have tryed Fopen() Fwrite() but also if I ahve converted number to exadeciaml the result is always in text format.

This is may code, any idea?

Thanks
Luigi


Code:
lnfile=FCREATE('test.bin')

FOR a=1 TO 15

=FWRITE(lnfile,dec2hex(a))

endfor

FCLOSE(lnfile)

PROC Dec2Hex(nDec)
*
LOCAL nResto,cHex,nDig
nResto=nDec
cHex=""
DO WHILE nResto > 15
  nDig=MOD(nResto,16)
  IF nDig < 10
    cHex=STR(nDig,1) + cHex
  ELSE
    cHex=CHR(55+nDig) + cHex
  ENDIF
  nResto=INT(nResto / 16)
ENDDO

nDig=nResto
IF nDig < 10
  cHex=STR(nDig,1) + cHex
ELSE
  cHex=CHR(55+nDig) + cHex
ENDIF

IF LEN(chex)=1
  cHex='0'+Chex
ENDIF

RETURN cHex
*
ENDPROC
 
strtofile(chr(1)+chr(2)+chr(3)+chr(4)+chr(5)+chr(6),"c:\binfile.dat")

You can also do that with FWRITE, just don't write text, if you don't want to write text. :) Your Dec2Hex function also writes text, 1 bytes for each hex digit, while two of them should be in one.

In short, you can have your binary data in a string and simply write that without any conversion. And text is just a special case of binary data, only with bytes that make up text...

Bye, Olaf.
 
Thank you Olaf,

I test strtofile(chr(xx)) andit works fine, I do not understand how write a number more than 255 for example for 256 that hexe is 100 how can write it?

Thank you
Luigi
 
Luigi,

This is the function I use to convert a number to a binary value. Pass it the number that you want to convert, and the number of bytes you want for the result (2, in your case).

Acknowledgement: I got the function from Gary DeWitt (more years ago than I care to remember):

Code:
FUNCTION BinToChar

LPARAMETERS tnWord, tnSize

LOCAL i, x, lnDivisor, lnRemainder, lcString

*-- Set a 4-byte default
tnSize = IIF(EMPTY(tnSize), 4, tnSize)

LOCAL ARRAY laString[tnSize]

*-- Set up remainder for use with MOD()
lnRemainder = tnWord
FOR i = tnSize TO 1 STEP -1
  *-- Get the divisor for this byte
  lnDivisor = 2 ^ (8 * (i - 1))

  *-- Divide for most significant bytes
  lnChar = lnRemainder / lnDivisor
	
  *-- Though theoretically impossible, VFP 3 sometimes tries 256 for ASCII values
  laString[i] = CHR(IIF(lnChar > 255, 255, lnChar))

  *-- Save MOD() for next-most-significant byte
  lnRemainder = MOD(lnRemainder, lnDivisor)
ENDFOR

*-- Concatenate resulting character values
lcString = ""
FOR x = 1 TO tnSize
  lcString = lcString + laString[x]
ENDFOR

RETURN lcString

ENDFUNC

Be aware that with 16-bit binary numbers, the low-order byte is actually the first byte; the high-order byte is the second (the opposite of what you might expect).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Luigi, bytes are 0-255, all larger numbers need a predefined length. eg WORD, INTEGER (4 Byte) LONGINT, whatever, but you'd need to define that beforehand.

If you would start your file with 256 bytes from 0 up to 256 and then continue with 0x100 in two bytes, that two bytes can also be interpreted as 1,0 instead of 256. That is, if you would write 0x100 as 0x01 and 0x00 into the file. There are definitions to write the low value byte before the high value byte, too, so 0x100 can also be written as 0x00, 0x01

The simple answer is using bintoc(), which offers ways to convert any numbers into a (binary) char representation.

The detailed answer is you need a predefined structure to write out and also read the file in the same way, or you get misinterpretations.

Technically a file remeins a byte array, what the bytes mean is up to an application.

Bye, Olaf.
 
Thank you Mike and Olaf for you help, I solved my lack of knowledge on binary data.

Luigi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top