help needed on coversin of an char array to an integer

M

MAx

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
 
M

MAx

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 :)
 
N

Nick Keighley

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?
 
V

vippstar

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?
 
M

MisterE

MAx said:
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 >= '0')&&(str<='9')) r |= str - '0';
else if ((str >= 'A')&&(str<='F')) r |= str - 'A' + 10;
else if ((str >= 'a') && (str<='f')) r |= str - 'a' + 10;
if (i != 7) r <<=4;
}
 
V

vippstar

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 >= '0')&&(str<='9')) r |= str - '0';
else if ((str >= 'A')&&(str<='F')) r |= str - 'A' + 10;
else if ((str >= 'a') && (str<='f')) r |= str - '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;
}
 
J

Joachim Schmitz

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 >= '0')&&(str<='9')) r |= str - '0';
else if ((str >= 'A')&&(str<='F')) r |= str - 'A' + 10;
else if ((str >= 'a') && (str<='f')) r |= str - '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
 
V

vippstar

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 >= '0')&&(str<='9')) r |= str - '0';
else if ((str >= 'A')&&(str<='F')) r |= str - 'A' + 10;
else if ((str >= 'a') && (str<='f')) r |= str - '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.
 
W

Willem

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] << 28);
}

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
 
M

Mark Bluemel

MAx said:
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.
 
B

Bartc

MAx said:
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 )

I assume sometimes the string will be something else? Otherwise it's rather
trivial.

Where are the hex digits coming from? If from a hex keyboard, then each
digit may already be 0 to 15 then combining to an integer value is very easy
(each digit corresponds to a 4-bit chunk).

Complete solutions have already been posted, the only tricky bit is
converting each character, 0 1 2 3 4 5 6 7 8 9 a b c d e f (and maybe A B C
D E F?) to a value 0 to 15.
 
M

MAx

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 )

I assume sometimes the string will be something else? Otherwise it's rather
trivial.

Where are the hex digits coming from? If from a hex keyboard, then each
digit may already be 0 to 15 then combining to an integer value is very easy
(each digit corresponds to a 4-bit chunk).

Complete solutions have already been posted, the only tricky bit is
converting each character, 0 1 2 3 4 5 6 7 8 9 a b c d e f (and maybe A B C
D E F?) to a value 0 to 15.




Hey,
Thanks guys, got the code to work. MisterE's solution did the
trick. Thanks man.
 
R

Richard Heathfield

MAx said:

Hey,
Thanks guys, got the code to work. MisterE's solution did the
trick. Thanks man.


Unfortunately, there are circumstances in which that code *won't* do the
trick. Presumably you have investigated these circumstances and determined
that they will never apply to your code's user base?
 
M

MisterE

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.

I don't bother assuming he is one of 0.0000000000000000001% of people who
don't use ASCII character set for raw chars in c.
 
R

Richard Heathfield

MisterE said:
I don't bother assuming he is one of 0.0000000000000000001% of people who
don't use ASCII character set for raw chars in c.

If we assume everyone on the planet - around 6600000000 people - uses a
computer, then 0.0000000000000000001% of this would be 0.0000000000066
people. Effectively, you're claiming that at most one person on the planet
uses a non-ASCII system.

You are mistaken.
 
B

Bartc

Richard Heathfield said:
MAx said:




Unfortunately, there are circumstances in which that code *won't* do the
trick. Presumably you have investigated these circumstances and determined
that they will never apply to your code's user base?

MisterE has already indicated what he thinks the probability of that is:
something less than one person in the world (was it one trillionth of a
person?) having such a system. Well I wouldn't go that far myself..

MAx was talking about a board, which either gets distributed with the code,
or the customers have the same model.

But, you might be right, maybe in Europe they like keying in hex numbers
with accents (0001fbcá). That wouldn't work.
 
W

Willem

Richard wrote:
) Unfortunately, there are circumstances in which that code *won't* do the
) trick. Presumably you have investigated these circumstances and determined
) that they will never apply to your code's user base?

For the record, EBCDIC has A..F in sequential order, (and a..f as well).


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
 
V

vippstar

Because they were/are sequential on every single
system that ever had a C compiler?
Let us assume you are correct here, what does that guarantee about the
future?
The standard only mentions '0'..'9' to be sequential.
 
R

Richard Tobin

Because they were/are sequential on every single
system that ever had a C compiler?
[/QUOTE]
Let us assume you are correct here, what does that guarantee about the
future?

That the chance of your program being used on a computer with a
character set with non-sequential A-F is less than, say, the chance of
the computer being eaten by beetles?

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top