Re: what is wrong with this code?
From: muchan (usenet_at_usenet.usenet)
Date: 02/02/05
- Next message: J.G.: "Using GRETA in VS 6 C++"
- Previous message: Larry Brasfield: "Re: Deriving a class from more than one interface"
- In reply to: MA: "Re: what is wrong with this code?"
- Next in thread: Tom Widmer: "Re: what is wrong with this code?"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 02 Feb 2005 17:31:00 +0100
MA wrote:
> Thanks for your reply.
>
> If I change the opening as "rt" does it solve the problem?
>
> the file is a text file with hex number in it for example the file is:
>
> fff112dffe.....
>
> Which is 0xff 0xf1 0x12 0xdf 0xfe ...
>
I'm not sure what you're saying.
You mean, (1)you can open the file in notepad.exe and see the characters
fff112dffe?
or (2) you mean you see it in binary dump something like
FF F1 12 DF FE .... ........ ........
???
In the case of (1) you should open it with "r" (text mode) and
read with fgetc or fgets as string, and convert every two characters to integer.
In the case of (2) you open it with "rb" and read them into integer,
one by one or in a batch into array.
Reading fff112dfe... directly into integer is IMHO not possible, first you need
to tell how much byte that hex number suppose to be... 0xff 0xf1 or 0xfff1
or 0xfff112 or 0xfff112df or... this interpretation must be in the code, right?
> When I am reading from this file, I should read string and then convert them
> to binary. How can convert a two character hex data (for example 0xff) into
> one byte of binary data (for example 256)?
>
If it's "0xff", then
char buf[5] = "0xff";
int i;
sscan(buf, "%x", &i); // i gets 255
If it's "ff", then
char buf[3] = "ff";
int i;
sscan(buf, "%x", &i); // i gets 255
But if the buffer is "fff1"
char buf[5] = "fff1";
int i;
sscan(buf, "%x", &i); // i gets 65522
and so if the buffer is "fff112"
char buf[7] = "fff112";
int i;
sscan(buf, "%x", &i); // i gets 16773394
Suppose you have four hex numbers 0xff 0xf1 0x12 0xdf,
char buf[9] = "fff112df";
int i0, i1, i2, i3;
sscan(buf, "%2x%2x%2x%2x", &i0, &i1, &i2, &i3); // you get for integers
You can use loop to convert 2 chars each, but you need to know when to exit
the loop... either the size of the string or the ending condition...
You can read 2 chars at once from the file, or you can read a chunk of
20 (for example) and convert it to 10 integers (for example) at once.
> Why it is corrupting stack?
>
Because the function tried to read the number string until it meets a whitespace...
>
> Best regards
>
Hope it helped...
muchan
- Next message: J.G.: "Using GRETA in VS 6 C++"
- Previous message: Larry Brasfield: "Re: Deriving a class from more than one interface"
- In reply to: MA: "Re: what is wrong with this code?"
- Next in thread: Tom Widmer: "Re: what is wrong with this code?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|