Re: what is wrong with this code?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: muchan (usenet_at_usenet.usenet)
Date: 02/02/05


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



Relevant Pages

  • Re: c# interview question
    ... particular version of the compiler happens to generate. ... internally will call Concat(string, string). ... shows the second loop runs 5-10pc quicker than the first loop. ... int s1 = System.Environment.TickCount; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: read keyboard input and storing in an array?
    ... When in a loop, how do I simply ... The data returned by 'br.readLine' would always be a String so I've stored ... simply convert it to an int as follows: ... you could copy 'myInt' to your array. ...
    (comp.lang.java.help)
  • Re: easy input functions for get line, 1 int, 1 double, 1 unsigned
    ... If all ok return that string that has to be free from the caller ... int c, r; ... if that line> 8192 chars ... else get one int in the format ...
    (comp.lang.c)
  • Stuck in while loop prison.
    ... The program jumps into the loop which will run forever unless ... String input; ... int inputHex = Integer.parseInt ... String currentChar = usBinary.substring(0, stopCase); ...
    (comp.lang.java.help)
  • Re: comparing two strcasecmp (stricmp) implementations
    ... >>> If the first character is bigger, the first string is bigger ... which is my prefered idiom for comparing int types ... A finite loop that finds the terminal location, ...
    (comp.lang.c)