Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > help needed on coversin of an char array to an integer

Reply
Thread Tools

help needed on coversin of an char array to an integer

 
 
MAx
Guest
Posts: n/a
 
      02-21-2008
Hi,
Im kinda stuck in a project at a point where i need an array to
be converted to a
integer using some kind of math.
This board does not support functions like scanf, sscanf etc as
it does not have enough memory to hold their stack.

given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
'c' , 'a' };
i need a function which can convert this array to an integer
( 0x0001fbca )

Expecting a quick response.

Thanks
MAx
 
Reply With Quote
 
 
 
 
MAx
Guest
Posts: n/a
 
      02-21-2008
On Feb 21, 2:21*pm, MAx <(E-Mail Removed)> wrote:
> Hi,
> * * * Im kinda stuck in a project at a point where i need an array to
> be converted to a
> integer using some kind of math.
> * * * This board does not support functions like scanf, sscanf etc as
> it does not have enough memory to hold their stack.
>
> * * * given a string *char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
> 'c' , 'a' };
> * * * i need a function which can convert this array to an integer
> ( 0x0001fbca )
>
> *Expecting a quick response.
>
> Thanks
> MAx


I forgot to mention that the contents of the array will be in the
range 0 to 9 and a to f,
its a hex number read inone digit at a time
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      02-21-2008
On 21 Feb, 09:21, MAx <(E-Mail Removed)> wrote:
> Hi,
> * * * Im kinda stuck in a project at a point where i need an array to
> be converted to a
> integer using some kind of math.
> * * * This board does not support functions like scanf, sscanf etc as
> it does not have enough memory to hold their stack.
>
> * * * given a string *char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
> 'c' , 'a' };
> * * * i need a function which can convert this array to an integer
> ( 0x0001fbca )
>
> *Expecting a quick response.


really?
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      02-21-2008
On Feb 21, 11:21 am, MAx <(E-Mail Removed)> wrote:
> Hi,
> Im kinda stuck in a project at a point where i need an array to
> be converted to a
> integer using some kind of math.
> This board does not support functions like scanf, sscanf etc as
> it does not have enough memory to hold their stack.

Which "board" is that?
> given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
> 'c' , 'a' };

That is not a string as it is not terminated with a 0.
> i need a function which can convert this array to an integer
> ( 0x0001fbca )

Trivial
> Expecting a quick response.

Is it you who expects an answer, or your teacher?
 
Reply With Quote
 
MisterE
Guest
Posts: n/a
 
      02-21-2008

"MAx" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> Im kinda stuck in a project at a point where i need an array to
> be converted to a
> integer using some kind of math.
> This board does not support functions like scanf, sscanf etc as
> it does not have enough memory to hold their stack.
>
> given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
> 'c' , 'a' };
> i need a function which can convert this array to an integer
> ( 0x0001fbca )
>
> Expecting a quick response.
>
> Thanks
> MAx


int i;
int r = 0;
for (i=0;i<8;i++)
{
if ((str[i] >= '0')&&(str[i]<='9')) r |= str[i] - '0';
else if ((str[i] >= 'A')&&(str[i]<='F')) r |= str[i] - 'A' + 10;
else if ((str[i] >= 'a') && (str[i]<='f')) r |= str[i] - 'a' + 10;
if (i != 7) r <<=4;
}


 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      02-21-2008
On Feb 21, 11:34 am, "MisterE" <(E-Mail Removed)> wrote:
> "MAx" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > Hi,
> > Im kinda stuck in a project at a point where i need an array to
> > be converted to a
> > integer using some kind of math.
> > This board does not support functions like scanf, sscanf etc as
> > it does not have enough memory to hold their stack.

>
> > given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
> > 'c' , 'a' };
> > i need a function which can convert this array to an integer
> > ( 0x0001fbca )

>
> > Expecting a quick response.

>
> > Thanks
> > MAx

>
> int i;
> int r = 0;
> for (i=0;i<8;i++)
> {
> if ((str[i] >= '0')&&(str[i]<='9')) r |= str[i] - '0';
> else if ((str[i] >= 'A')&&(str[i]<='F')) r |= str[i] - 'A' + 10;
> else if ((str[i] >= 'a') && (str[i]<='f')) r |= str[i] - 'a' + 10;
> if (i != 7) r <<=4;
>
> }

What makes you think 'A'..'F' (and lowercase) are indeed sequential?
If you plan on posting C code for the OP, then.. at least post code
that works.

Given the restrictions of this, here's a solution I suggest:
--
#include <stdio.h>

int main(void) {

char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' , 'c' , 'a' };
unsigned int i;
FILE * foo;

foo = tmpfile();
if(foo != NULL) {
fwrite(str, 1, sizeof str, foo);
rewind(foo);
fscanf(foo, "%x", &i);
fclose(foo);
}

return 0;
}


 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      02-21-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Feb 21, 11:34 am, "MisterE" <(E-Mail Removed)> wrote:
>> "MAx" <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed)...
>>
>>
>>
>>> Hi,
>>> Im kinda stuck in a project at a point where i need an array to
>>> be converted to a
>>> integer using some kind of math.
>>> This board does not support functions like scanf, sscanf etc as
>>> it does not have enough memory to hold their stack.

>>
>>> given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b'
>>> , 'c' , 'a' };
>>> i need a function which can convert this array to an integer
>>> ( 0x0001fbca )

>>
>>> Expecting a quick response.

>>
>>> Thanks
>>> MAx

>>
>> int i;
>> int r = 0;
>> for (i=0;i<8;i++)
>> {
>> if ((str[i] >= '0')&&(str[i]<='9')) r |= str[i] - '0';
>> else if ((str[i] >= 'A')&&(str[i]<='F')) r |= str[i] - 'A' + 10;
>> else if ((str[i] >= 'a') && (str[i]<='f')) r |= str[i] - 'a' +
>> 10; if (i != 7) r <<=4;
>>
>> }

> What makes you think 'A'..'F' (and lowercase) are indeed sequential?
> If you plan on posting C code for the OP, then.. at least post code
> that works.
>
> Given the restrictions of this, here's a solution I suggest:

Oops, your "-- " makes the rest a signature and snips your code...
Restored manually:
>#include <stdio.h>
>
>int main(void) {
>
> char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' , 'c' , 'a' };
> unsigned int i;
> FILE * foo;
>
> foo = tmpfile();
> if(foo != NULL) {
> fwrite(str, 1, sizeof str, foo);
> rewind(foo);
> fscanf(foo, "%x", &i);
> fclose(foo);
> }
>
> return 0;
>}

the OP claimed not to be able to use scanf and sscanf as his board doesn't
have this, so what makes you thing he can created files and/or use fscanf?

Bye, Jojo


 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      02-21-2008
On Feb 21, 12:00 pm, "Joachim Schmitz" <nospam.j...@schmitz-
digital.de> wrote:
> (E-Mail Removed) wrote:
> > On Feb 21, 11:34 am, "MisterE" <(E-Mail Removed)> wrote:
> >> "MAx" <(E-Mail Removed)> wrote in message

>
> >>news:(E-Mail Removed)...

>
> >>> Hi,
> >>> Im kinda stuck in a project at a point where i need an array to
> >>> be converted to a
> >>> integer using some kind of math.
> >>> This board does not support functions like scanf, sscanf etc as
> >>> it does not have enough memory to hold their stack.

>
> >>> given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b'
> >>> , 'c' , 'a' };
> >>> i need a function which can convert this array to an integer
> >>> ( 0x0001fbca )

>
> >>> Expecting a quick response.

>
> >>> Thanks
> >>> MAx

>
> >> int i;
> >> int r = 0;
> >> for (i=0;i<8;i++)
> >> {
> >> if ((str[i] >= '0')&&(str[i]<='9')) r |= str[i] - '0';
> >> else if ((str[i] >= 'A')&&(str[i]<='F')) r |= str[i] - 'A' + 10;
> >> else if ((str[i] >= 'a') && (str[i]<='f')) r |= str[i] - 'a' +
> >> 10; if (i != 7) r <<=4;

>
> >> }

> > What makes you think 'A'..'F' (and lowercase) are indeed sequential?
> > If you plan on posting C code for the OP, then.. at least post code
> > that works.

>
> > Given the restrictions of this, here's a solution I suggest:

>
> Oops, your "-- " makes the rest a signature and snips your code...
> Restored manually:

Ah, that was silly.
I should've written "-- snip.c --" as I usually do. Oh well.
> >#include <stdio.h>

>
> >int main(void) {

>
> > char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' , 'c' , 'a' };
> > unsigned int i;
> > FILE * foo;

>
> > foo = tmpfile();
> > if(foo != NULL) {
> > fwrite(str, 1, sizeof str, foo);
> > rewind(foo);
> > fscanf(foo, "%x", &i);
> > fclose(foo);
> > }

>
> > return 0;
> >}

>
> the OP claimed not to be able to use scanf and sscanf as his board doesn't
> have this, so what makes you thing he can created files and/or use fscanf?

I only wrote a solution given the restrictions.
Ofcourse there is a "better" solution, and I suspect the one that OP
looks for.
However, it sounds like a homework assignment because the task is
quite trivial; furthermore I believe it would "harm" OP and others
more to give a working solution than to make him work it on his own.

 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      02-21-2008
MAx wrote:
) Hi,
) Im kinda stuck in a project at a point where i need an array to
) be converted to a
) integer using some kind of math.
) This board does not support functions like scanf, sscanf etc as
) it does not have enough memory to hold their stack.
)
) given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
) 'c' , 'a' };
) i need a function which can convert this array to an integer
) ( 0x0001fbca )

Since you're talking about a 'board' I assume that you have a single,
fixed platform, and that your character set is ASCII.

I also assume that char is 8 bits and unsigned long is at least 32 bits,
that you will never have malformed input, and that you want a lean and
mean solution.

This makes the following (quite evil) code possible:

unsigned long hex2num(unsigned char str[8])
{
unsigned long *res, dig;
res = (unsigned long *)str;
dig = ((*res & 0x10101010) >> 4) * 0x0f;
*res = (*res & dig) | ((*res + 0x09090909) & (dig ^ 0x0f0f0f0f));
res = (unsigned long *)(str + 4);
dig = ((*res & 0x10101010) >> 4) * 0x0f;
*res = (*res & dig) | ((*res + 0x09090909) & (dig ^ 0x0f0f0f0f));

return (str[7] ) + (str[6] << 4)
+ (str[5] << 8 ) + (str[4] << 12)
+ (str[3] << 16) + (str[2] << 20)
+ (str[1] << 24) + (str[0] << 2;
}

Which should translate nicely into assembly.
(The last bit with the return might be sped up a bit, still).


As an exercise to the reader: How many assumptions does this
code make that are outside the C standard ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      02-21-2008
MAx wrote:
> Hi,
> Im kinda stuck in a project at a point where i need an array to
> be converted to a
> integer using some kind of math.
> This board does not support functions like scanf, sscanf etc as
> it does not have enough memory to hold their stack.
>
> given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
> 'c' , 'a' };
> i need a function which can convert this array to an integer
> ( 0x0001fbca )


Then write one. It's not exactly difficult.

* Start with a running total, set initially to 0.
* Get a digit at a time, convert it into a numeric value,
multiply the running total by 16 and add the digit's value.
* When you run out of digits, you're done

Converting a character (e.g. '9' or 'a') to it's numeric value is a
little more tricky, but not very.

According to the standard, '0' - '9' are contiguous in the character
set, therefore the can always be converted to integers by subtracting
'0', so that's easy.

If you can guarantee that your character set has contiguous alphabetics
(ASCII does, but some representations don't), you can covert alphabetic
characters to their value in the hexadecimal range by subtracting 'a'
(or 'A' as appropriate) and adding 10.

If your character set isn't contiguous (or you're not sure that it will
be so everywhere that your code may need to run), you'll need to use a
lookup table or switch block.

> Expecting a quick response.


Nice to see an optimist.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char->integer, integer->char commands christophergraber@gmail.com Perl Misc 5 11-22-2005 07:13 AM



Advertisments