Re: byte data manipulation



Niv (KP) wrote:
On 25 Apr, 15:40, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:
"Niv (KP)" <kev.pars...@xxxxxxxxxxxxxxxx> writes:

<snip>

Right, more details:
1. I what to create 2 arrays of 2Mbytes each.
2. Read in the input hex file, stripping of the preamble and checksum
bits, and putting the bytes (2 chars) into array_1.
3. Copy array_1 data to array_2, but in a non-linear fashion, using
lfsr or some such.
Here you mean you don't access array_1 sequentially, yes? That
explains why you need the array.

4. Write out array_2 in a linear address fashion, adding the preamble
(easy) and calculating a new checksum for each line, where the new
output lines have 16 bytes (32 chars). The checksum is just the 8 bit
sum of the preceding values in the preamble and the 16 bytes,
discarding overflows.
Each line has 15 data bytes (30 hex chars) and one byte of checksum?

If so, may not need array_2 at all. The output of the LFSR[1] can be
checksumed as you go and the output generated on the fly. Of course
there may be a reason to copy the data, but its not clear from the
description so far.

Is that enough info?
Except for what bit you are having trouble with. I can offer a few
tips. Use unsigned char for the array (it will avoid problems with
bit-operations if char is signed on your system). Allocate it using
malloc since the size is not know until run-time:

unsigned char *buffer = malloc(<size in bytes goes here>);

The simplest way to read two hex chars and put the result into a
single byte is:

fscanf(file, "%2hhx", buffer + pos);

and increment pos as you go. The result of both malloc and fscanf
should be checked. If malloc returns NULL, the allocation failed.
If fscanf does not return 1 it did not read a number.

[1] Linear feedback shift register.

--
Ben.

The input file is in intel format "hexout", as follows;

colon, 1 byte data length, 2 bytes address, 1 byte data type, <length>
dtat bytes, checksum byte; all in hex,

e.g. : 20 0010 00 <32 data bytes> <checksum> (all without spaces
of course).

I need to read in all the data bytes, up to 2 meg, into an array, re-
arrange that array, easiest to copy to another array by indexing the
array pointer in a non-linear fashion, then write out the full 2 meg
of the new array, again in an intel format, with only 16 data bytes
per line. So, I'll need to calculate the new checksum over the length
byte, address bytes, type byte and data bytes; where the csum is just
the hex sum of all these, ignoring any carry.

Like I said, I have a fully working version in VHDL, which takes about
30 seconds to run, but ties up a simulator, if one was avaialable. So
I think an exe file is better. I can wrap the exe in a tcl frame to
get the input and output files/destination etc, but my C knowledge is
virtually nil. Trying to read "teach yourself in 24 hrs" but i need a
quick answer really, haven't got time to waste on learning allof C
right now, although I shall persevere with the learning!

Respectfully, you don't know what you need to know. Get this. It is the Intel Specification for the hex files you have.

http://microsym.com/editor/assets/intelhex.pdf
GIYF
--
Joe Wright
"Memory is the second thing to go. I forget what the first is."
.



Relevant Pages

  • Re: byte data manipulation
    ... explains why you need the array. ... and calculating a new checksum for each line, ... Each line has 15 data bytes (30 hex chars) and one byte of checksum? ...
    (comp.lang.c)
  • Re: byte data manipulation
    ... Read in the input hex file, stripping of the preamble and checksum ... and putting the bytes (2 chars) into array_1. ... explains why you need the array. ...
    (comp.lang.c)
  • Re: byte data manipulation
    ... I need to read in all the data bytes, up to 2 meg, into an array, re- ... I'll need to calculate the new checksum over the length ... the hex sum of all these, ... of a line that meets the format is 521 characters. ...
    (comp.lang.c)
  • Re: byte data manipulation
    ... well-know format. ... arrange that array, easiest to copy to another array by indexing the ... I'll need to calculate the new checksum over the length ... the hex sum of all these, ...
    (comp.lang.c)
  • Re: Big endian convention in Ruby
    ... if the array holding this is ... messageHex[0].hex.to_i this will ensure that it is a integer in hex. ... Unpack directly to Integers, as shown above. ... It works perfectly fine with in give the loop 64 times.. ...
    (comp.lang.ruby)