converting char array to integer

S

silentlights

Hi,
Can you help me conver a char array into an integer. I am trying something
like..

char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

I know this doesnt work. Does anyone of you have a suggestion for that
conversion. This is to be implemented in a 16bit µc. Is there a solution
without using anything like sprintf or multiplication.

Thanks in advance
Densil
 
N

Nils Petter Vaskinn

Hi,
Can you help me conver a char array into an integer. I am trying something
like..

char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

I know this doesnt work. Does anyone of you have a suggestion for that
conversion. This is to be implemented in a 16bit µc. Is there a solution
without using anything like sprintf or multiplication.

What would you want 'numb' to be after the conversion?

1515 ?
5151 ?
12 ?

Something else
 
S

Stephen Sprunk

silentlights said:
Can you help me conver a char array into an integer. I am trying something
like..

char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

I know this doesnt work. Does anyone of you have a suggestion for that
conversion. This is to be implemented in a 16bit c. Is there a solution
without using anything like sprintf or multiplication.

#include <stdlib.h>

char carray[5];
int numb;
....
carray[0] = '1'; // 1 != '1'
carray[1] = '5';
carray[2] = '1';
carray[3] = '5';
carray[4] = 0; // null termination of string

numb = atoi(carray);
....
 
A

Ahmed S. Badran

silentlights said:
char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

What you could also do (but this will only work on 32-bit micro-processors)
carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;
numb = (int *) carray; // carray is a char* already so cast it into an int
*

As for the case you're mentioning, if int's are 16 bits and char's are 8
bits this will only set num to either 15 or 51 depending on the endianity of
the machine you're using.

Ahmed
 
I

Irrwahn Grausewitz

Ahmed S. Badran said:
silentlights said:
char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

What you could also do (but this will only work on 32-bit micro-processors)
carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;
numb = (int *) carray; // carray is a char* already so cast it into an int
*

Besides it being a brain-dead idea in the first place, you could've
well put any garbage in the array, since you only try to assign the
pointer value carray decays to in value context (sic) to an integer
variable, after performing a useless type cast, BTW.

Regards
 
A

Ahmed S. Badran

Irrwahn said:
Ahmed S. Badran said:
silentlights said:
char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

What you could also do (but this will only work on 32-bit
micro-processors) carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;
numb = (int *) carray; // carray is a char* already so cast it into
an int *

Besides it being a brain-dead idea in the first place, you could've
well put any garbage in the array, since you only try to assign the
pointer value carray decays to in value context (sic) to an integer
variable, after performing a useless type cast, BTW.

Regards

Sorry guys, the correction is

numb = *((int *)carray);

Ahmed
 
S

silentlights

Hi Nils,

I need to have numb = 1515

by the way..

char str[5] = "5678";
int intvar = (int *)str;

doesnt work either.

I thank everyone here who is reading this messg and trying to post a
reply. Your help is highly appreciated.

Cheers
Densil
 
A

Ahmed S. Badran

silentlights said:
Hi Nils,

I need to have numb = 1515

by the way..

char str[5] = "5678";
int intvar = (int *)str;

doesnt work either.

I thank everyone here who is reading this messg and trying to post a
reply. Your help is highly appreciated.

Cheers
Densil

Densil,

char str[5] = "5678";

/* is NOT */

char str[5];
str[0] = 5;
str[1] = 6;
str[2] = 7;
str[3] = 8;
str[4] = 0; /* or null or whatever.. */

to convert

char str[5] = "5678"; /* to int */

use

numb = atoi(str);

to covert the earlier case (of str = 5 + i;) you can use

numb = * ((int *) str); /* in this case numb will be 5678 if you're on a
big endian system */

Ahmed

Ahmed
 
S

silentlights

Hi Guys,
I thank everyone who took time to write me an idea.

(1) I cant use the function "atoi" because I cant use any standard
libraries as headers.

(2) It is a 16bit env, neither can I use the *((int *) str) way.

The only method I can use.. without any multiplication is...

unsigned int intvar = 0;
char str[4] = {'4','3','2','1'};
unsigned int tau[11] = {0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
9000};
unsigned int hun[11] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900};
unsigned int ten[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};

intvar = tau[str[0] - 48] + hun[str[1] - 48] + ten[str[2] - 48] + str[3] -
48;


If anyone of you find there is something fishy.. pls tell me.. if there
could be any other way.. without using functions like "atoi" or any "str*"
functions.. wihtout using multiplication.. pls post me here.

Thank you all once again
Densil
 
M

Martin Dickopp

silentlights said:
Hi Guys,
I thank everyone who took time to write me an idea.

(1) I cant use the function "atoi" because I cant use any standard
libraries as headers.

(2) It is a 16bit env, neither can I use the *((int *) str) way.

The only method I can use.. without any multiplication is...

unsigned int intvar = 0;
char str[4] = {'4','3','2','1'};
unsigned int tau[11] = {0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
9000};
unsigned int hun[11] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900};
unsigned int ten[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};

intvar = tau[str[0] - 48] + hun[str[1] - 48] + ten[str[2] - 48] + str[3] -
48;

If anyone of you find there is something fishy.. pls tell me.. if there
could be any other way.. without using functions like "atoi" or any "str*"
functions.. wihtout using multiplication.. pls post me here.

I don't understand /why/ you cannot use multiplication. However, you
can emulate it using shifts and additions. For example, the macro

#define MUL10(x) (((x) << 3) + ((x) << 1))

emulates multiplication by 10.

Martin
 
J

Jack Klein

silentlights said:
Hi Nils,

I need to have numb = 1515

by the way..

char str[5] = "5678";
int intvar = (int *)str;

doesnt work either.

I thank everyone here who is reading this messg and trying to post a
reply. Your help is highly appreciated.

Cheers
Densil

Densil,

char str[5] = "5678";

/* is NOT */

char str[5];
str[0] = 5;
str[1] = 6;
str[2] = 7;
str[3] = 8;
str[4] = 0; /* or null or whatever.. */

to convert

char str[5] = "5678"; /* to int */

use

numb = atoi(str);

Don't use atoi(), or any of the ato... functions, unless you can
guarantee the converted result will not be out of range for the return
type. Undefined behavior results.

The strto... functions, on the other hand, have defined behavior no
matter what the input.
to covert the earlier case (of str = 5 + i;) you can use

numb = * ((int *) str); /* in this case numb will be 5678 if you're on a
big endian system */


1. No, the result will not be 5678 on any conforming C
implementation. Not a chance. C requires a pure binary notation for
the integer types.

2. In the original post, the OP stated he was using a 16 bit uC. At
best, accessing an array of four bytes will result in only the first
two of them being accessed.

3. str might not meet the alignment requirements for the address of
an int on many 16-bit platforms. Results can range from accessing the
wrong bytes of memory to hardware traps. Undefined behavior is like
that.
 
S

silentlights

Hi Martin,
I don't understand /why/ you cannot use multiplication.

it is expensive for the µC.
However, you can emulate it using shifts and additions.
For example, the macro

#define MUL10(x) (((x) << 3) + ((x) << 1))
emulates multiplication by 10.

Fast multiplication is a better solution. It saves a lot of opcodes.

Thanks
Densil
 
C

CBFalconer

Martin said:
.... snip ...

I don't understand /why/ you cannot use multiplication. However,
you can emulate it using shifts and additions. For example, the
macro

#define MUL10(x) (((x) << 3) + ((x) << 1))

emulates multiplication by 10.

Pseudo assembly, 10 * R1 -> R2. R2, flags disturbed. Fast.

mov r2,r1; operands dest,src
add r2,r2; 2*
add r2,r2; 4*
add r2,r1; 5*
add r2,r2; 10*

and the equivalent macro:

#define MUL10(x) (((x) + ((x) << 2)) << 1)
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

CBFalconer wrote:
| Martin Dickopp wrote:
|
| ... snip ...
|
|>I don't understand /why/ you cannot use multiplication. However,
|>you can emulate it using shifts and additions. For example, the
|>macro
|>
|> #define MUL10(x) (((x) << 3) + ((x) << 1))
|>
|>emulates multiplication by 10.
[snip]
|
| #define MUL10(x) (((x) + ((x) << 2)) << 1)
|

Sweet!


- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAf8wqagVFX4UWr64RAiwWAJ0TLwPUSAY6i5RxKmoyQVje+Ey4eACfdTte
wqIYy3T7DgKJMFw73IM6y7s=
=LAKV
-----END PGP SIGNATURE-----
 
S

silentlights

Could you please explain this shift.. I didnt get you.

How can I emulate multiplication for 100 and 1000 ?
Can I do something similair for division of 10, 100 and 1000 too ?..
Is there an alternative for modulo of 10, 100 and 1000 ?


Thanks
Densil
 
C

CBFalconer

silentlights said:
Could you please explain this shift.. I didnt get you.

How can I emulate multiplication for 100 and 1000 ?
Can I do something similair for division of 10, 100 and 1000 too ?..
Is there an alternative for modulo of 10, 100 and 1000 ?

You failed to quote anything, so I have no idea what you are
talking about. I have better things to do than search through
discarded newsgroup entries.
 
K

kal

Could you please explain this shift.. I didnt get you.
How can I emulate multiplication for 100 and 1000 ?
Can I do something similair for division of 10, 100 and 1000 too ?..
Is there an alternative for modulo of 10, 100 and 1000 ?

Multiplication is, by definition, repeated addition.

To multiply by 10 add the number to itselt 9 times.

result = x + x + x + x + x + x + x + x + x + x

Sincethe shift operations are fast and left shifting by 1 bit is
the same as multiplying by 2 one can rework this scheme.

result = (x << 1) + (x << 1) + (x << 1) + (x << 1) + (x << 1)

On second thoughts,

result = (x << 2) + (x << 2) + (x << 1)

Come to think of it,

result = (x << 3) + (x << 1)

Note that: 10 = 8 + 2
100 = 64 + 32 + 4
1000 = 512 + 256 + 128 + 64 + 32 + 8

It might be worthwhile to get hold of one of those hand cranked
adding machines and practice multiplication/division on it.
 
C

Christian Bau

Multiplication is, by definition, repeated addition.

To multiply by 10 add the number to itselt 9 times.

result = x + x + x + x + x + x + x + x + x + x

Sincethe shift operations are fast and left shifting by 1 bit is
the same as multiplying by 2 one can rework this scheme.

result = (x << 1) + (x << 1) + (x << 1) + (x << 1) + (x << 1)

On second thoughts,

result = (x << 2) + (x << 2) + (x << 1)

Come to think of it,

result = (x << 3) + (x << 1)

Note that: 10 = 8 + 2
100 = 64 + 32 + 4
1000 = 512 + 256 + 128 + 64 + 32 + 8

Maybe more useful: 1000 = 1024 - 16 - 8.
 
J

Joe Wright

silentlights said:
Could you please explain this shift.. I didnt get you.

How can I emulate multiplication for 100 and 1000 ?
Can I do something similair for division of 10, 100 and 1000 too ?..
Is there an alternative for modulo of 10, 100 and 1000 ?


Thanks
Densil
Any book on C explains the << and >> shift operators. Do you have a
book? I recommend K&R2. Given two examples of MUL10 in recent posts,
why can't you imagine MUL100 and MUL1000? Division is another
animal. I'll have to get back to you on that.
 
C

CBFalconer

Joe said:
Any book on C explains the << and >> shift operators. Do you have a
book? I recommend K&R2. Given two examples of MUL10 in recent posts,
why can't you imagine MUL100 and MUL1000? Division is another
animal. I'll have to get back to you on that.

I have mounted a slightly revised version of my recent posting on
the double-dabble algorithm, which provides a divide by 10
facility for machines without hardware division, as dubldabl.txt
at:

<http://cbfalconer.home.att.net/download/>
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top