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> convertFloatToB its( <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 8 2919
In article <3f************ ***********@new s.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> convertFloatToB its( <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
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
Greetings.
In article <3f************ ***********@new s.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
"Rick" <as******@hotma il.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
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_ADDRESSE S long int
#define EEPROM_SIZE 8192
void write_ext_eepro m(EEPROM_ADDRES SES 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(EEPR OM_SELECT);
for(i=0;i<8;i++ )
{
output_bit(EEPR OM_DI, shift_left(&wre n,1,0));
output_high(EEP ROM_CLK);
output_low(EEPR OM_CLK);
}
output_high(EEP ROM_SELECT);
output_low(EEPR OM_SELECT);
for(i=0;i<32;i+ +)
{
output_bit(EEPR OM_DI, shift_left(cmd, 4,0));
output_high(EEP ROM_CLK);
output_low(EEPR OM_CLK);
}
output_high(EEP ROM_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
"Rick" <as******@hotma il.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_ADDRESSE S long int void write_ext_eepro m(EEPROM_ADDRES SES 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_eepro m(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.
Haven't tried it yet but many thanks!! It makes sense to me.
Greetings,
Rick
"Rick" <as******@hotma il.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 entered by the user. I know the
web script must convert the values into integers, store them as variables
and then output them, but I cannot seem to establish the calculation for
this. I'm attaching the html I've got, that works when the page is...
|
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 codecvt, in Josuttis' "The Standard C++
Library", did not help, and I'm still awaiting delivery of Langer's
"Standard C++ IOStreams and Locales".
Thanks,
|
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...
Whats the best way to read in each line and break it up into individual
ints?
|
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 bytes.
I've tried using bitwise operators, but they seem to convert the value
into an integer first, and i've tried using the toString() method to
convert it into a hex value so i can parse it, but that also seems to
first convert it into an...
|
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 problem I ran
into is a float with value INT_MAX assigned to an int results in the
value -2147483648 being assigned, but if the conversion takes place in
an expression INT_MAX is assigned as I would expect:
int...
| |
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;
http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe#4aadc71be8aeddbe
I copied this code and modified it slightly, however, you will notice
that for one of the examples, the conversion isn't...
|
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).
This comes up as too complex using nested IIf, then, else.
I tried a lookup table but here the problem I found was that unless the
average test score matches exactly then no grade is returned. For example a
score of 11.0 returns grade 4b but a...
|
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
|
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 and bArray is an array of bytes going into these
lines of code
**********
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |