converting to bits

R

Richard Bos

Rick said:
Hmmm, I never did this before so maybe I'm talking a bit strange. What
happens is as follow. A microcontroller with a webserver gets a string with
all kind of data that needs to be stored in the Eeprom. So I read the string
and at some point I'll get for example this : x=3,24. I know the storage
location for x so now I need to write "3,24" into the eeprom, af a float so
there are 4 bytes reserved for that. I can't just say, eeprom_write(
somedata ); so I need to convert that float, at least, that's what I think,
as I said, never did it before.

Ah, you want to convert _text_ to a floating point value! That's a
different problem from what you described in the first post.

Normally, you'd use strtod() for this. Before we had strtod(), you'd use
atof(). The problem here is that both strtod() and atof() are in
<stdlib.h>, and a freestanding implementation (which I suspect is what
you may be using) is not required to supply <stdlib.h>.
If I were you, I'd first check that, maybe, it does provide <stdlib.h>,
in particular strtod(), after all; you may be in luck. If not, I'm
afraid you'll just have to parse the text by hand. It's not _that_
difficult.

Richard
 
R

Rick

Hi,

Does C have some handy functions to convert chars, ints and floats to bit
arrays? I need to store that stuff binary so a few functions would be great.
Converting chars and ints isn't difficult but floats are giving me a
headache. And how about converting them back?
I need a function like :
<bitarray> convertFloatToBits( <number>, <signed or not>, <length
default at 32 bits> )
By the way, the C program has to be suitable for a microcontroller so I only
have acces to the basic libraries.

Greetings,
Rick
 
A

Andreas Kahari

Hi,

Does C have some handy functions to convert chars, ints and floats to bit
arrays? I need to store that stuff binary so a few functions would be great.
Converting chars and ints isn't difficult but floats are giving me a
headache. And how about converting them back?
I need a function like :
<bitarray> convertFloatToBits( <number>, <signed or not>, <length
default at 32 bits> )
By the way, the C program has to be suitable for a microcontroller so I only
have acces to the basic libraries.


If I'm not terribly wrong, most machines already store all
datatypes as binary.

If you calculate all the ones and zeroes of an integer or
floating point value, what type of array would you want to store
them in? An int array? That kind'a defeats the purpose of the
execise, don't you think?

Are you not happy with writing the floats and whatnot to a
binary file, or do you want to write character ones and charater
zeroes to a text file?
 
R

Rick

Hmmm, I never did this before so maybe I'm talking a bit strange. What
happens is as follow. A microcontroller with a webserver gets a string with
all kind of data that needs to be stored in the Eeprom. So I read the string
and at some point I'll get for example this : x=3,24. I know the storage
location for x so now I need to write "3,24" into the eeprom, af a float so
there are 4 bytes reserved for that. I can't just say, eeprom_write(
somedata ); so I need to convert that float, at least, that's what I think,
as I said, never did it before.

Greetings,
Rick
 
T

Tristan Miller

Greetings.

Does C have some handy functions to convert chars, ints and floats to
bit arrays?

No built-in functions, but you might be able to get away with using a
union. As with structs, I don't believe compilers are required to
tightly pack unions, so you may end up with padding problems. I'm sure
someone will be along to correct me soon if I'm wrong, though.

Regards,
Tristan
 
R

Rick

StrtoD... have to remember that one. But we're not finished yet( I think).
Now that value needs te be stored as 1's and zeros in the eeprom. Somebody
else has written some code to store a byte into the eeprom before, it looked
as follow :

#ifndef EEPROM_SELECT

#define EEPROM_SELECT PIN_B4
#define EEPROM_CLK PIN_C3
#define EEPROM_DI PIN_C5
#define EEPROM_DO PIN_C4

#endif

#define EEPROM_ADDRESSES long int
#define EEPROM_SIZE 8192
void write_ext_eeprom(EEPROM_ADDRESSES address, byte data)
{
byte cmd[4];
byte i;
byte wren;
byte rdsr;
wren=0x06;
rdsr=0x05;
cmd[0]=data;
cmd[1]=address;
cmd[2]=(address>>8);
cmd[3]=0x02;

output_low(EEPROM_SELECT);
for(i=0;i<8;i++)
{
output_bit(EEPROM_DI, shift_left(&wren,1,0));
output_high(EEPROM_CLK);
output_low(EEPROM_CLK);
}
output_high(EEPROM_SELECT);
output_low(EEPROM_SELECT);
for(i=0;i<32;i++)
{
output_bit(EEPROM_DI, shift_left(cmd,4,0));
output_high(EEPROM_CLK);
output_low(EEPROM_CLK);
}
output_high(EEPROM_SELECT);
delay_ms(6);
}

I haven't written this myself and I don't really understand it (yet). But as
you can see this function can write a byte into the eeprom. Now there needs
to be a function for writing a float type as well. Maybe it can be done with
this function somehow... Anyway, I had quit different code in my head. Let
some fancy function convert that float to a array like this BIT
bitArray[32]; and then set a zero or 1 at every single bit. But maybe I'm
completely wrong or doing it the stupid way. But shoot, there's a first time
for everything :)

Thanks for helping!
Rick
 
S

Simon Biber

Rick said:
StrtoD... have to remember that one. But we're not finished yet( I
think). Now that value needs te be stored as 1's and zeros in the
eeprom. Somebody else has written some code to store a byte into
the eeprom before, it looked as follow :
[snip]
#define EEPROM_ADDRESSES long int
void write_ext_eeprom(EEPROM_ADDRESSES address, byte data)
[snip]

From this function signature you can see that it writes a single byte of
data to the given address. Therefore, you want code like:

char input[] = "3.27";
float foo = strtod(input, 0); /* Convert text to float value */
size_t i;
for(i = 0; i < sizeof foo; i++) /* For each byte of foo */
{
/* Write the corresponding byte to the EEPROM at the address */
write_ext_eeprom(address + i, ((unsigned char *)&foo));
}

If your embedded system does not include a strtod function, you will
have to supply your own equivalent function (perhaps grab the code
from a free one, if the licensing permits it).
I haven't written this myself and I don't really understand it (yet).
But as you can see this function can write a byte into the eeprom.
Now there needs to be a function for writing a float type as well.
Maybe it can be done with this function somehow... Anyway, I had quit
different code in my head. Let some fancy function convert that float
to a array like this BIT bitArray[32]; and then set a zero or 1
at every single bit. But maybe I'm completely wrong or doing it the
stupid way.

Yes, I think you had the wrong idea.
 
R

Richard Bos

Rick said:
StrtoD... have to remember that one.

No, _not_ StrtoD()! strtod(). C is case-sensitive.
But we're not finished yet( I think).
Now that value needs te be stored as 1's and zeros in the eeprom. Somebody
else has written some code to store a byte into the eeprom before, it looked
as follow :

What it looks like is immaterial, as long as we know that it correctly
writes a byte to EEPROM. Now all you need to know is what the ROM
expects the float to look like, and you can start trying to decode a
float from the text (not necessarily hard) and write it to your own
memory, munge that memory into the shape the ROM expects, and then send
those bytes.

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top