473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

converting to bits

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
Nov 13 '05 #1
8 2889
In article <3f***********************@news.xs4all.nl>, Rick wrote:
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?
--
Andreas Kähäri
Nov 13 '05 #2
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
Nov 13 '05 #3
Greetings.

In article <3f***********************@news.xs4all.nl>, Rick wrote:
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

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #4
"Rick" <as******@hotmail.com> wrote:
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
Nov 13 '05 #5
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
Nov 13 '05 #6
"Rick" <as******@hotmail.com> wrote:
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)[i]);
}

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.

--
Simon.
Nov 13 '05 #7
Haven't tried it yet but many thanks!! It makes sense to me.

Greetings,
Rick
Nov 13 '05 #8
"Rick" <as******@hotmail.com> wrote:
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
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Krista Bailie | last post by:
I'm sure this is so easy that it will hurt for anyone to read it, but I really need some direction. I'm trying to create a color chart (RGB) that shows steps between two different colors as...
22
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
7
by: Jus! | last post by:
Hi. I am reading bits(1's & 0's) from a file and i wa wondering what is the most efficient method of converting these strings to individual int's? eg. File contains: 110001 010011 etc......
25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
5
by: Code4u | last post by:
In the course of writing numerical code I needed to convert a float to an int with a defined behavior: if the float is great than INT_MAX, set the int to INT_MAX, otherwise assign directly. The...
2
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats;...
11
by: Penfold | last post by:
I'd appreciate help converting student average test scores into grades. My problem is that I need to allocate one of about 20 grades (3a,3b,3c,4a,4b,4c etc through to 8c plus a couple of others)....
6
by: iwdu15 | last post by:
hi, how can i convert type Short to Unicode strings? the System.Text.UnicodeEncoding cant convert it if the length is too shrot....so i need to do this manually. how can i do this -- -iwdu15
3
by: news.microsoft.com | last post by:
Hello, I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue. Steping through both the C# and VB.NET apps i get identical results but for one section. Notes: bit_buffer = 360...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.