I have a float that I need converted to a 4-byte char-array.
using sprintf does not work.
What do you suggest?
Converting float to char[4]
Started by Walle, Mar 14 2008 08:52 AM
9 replies to this topic
#1
Posted 14 March 2008 - 08:52 AM
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
"I'm not young enough to know everything." - Oscar Wilde
|
|
|
#2
Posted 14 March 2008 - 01:31 PM
This should work.
float value; char array[4]; ... memcpy(array, &value, sizeof (value));
#3
Posted 14 March 2008 - 05:42 PM
Thank you, memcpy will do it, although I have to change the float into a data type that can be unsigned (actually, in Visual Studio 2008, you can declare a float as unsigned, but that's wierd I think).
I have another question though, I think I'll just ask it in this thread for now and just hope that someone sees it :)
How do you do bitwise operations in C/C++?
If I for example want to increase a variable by a binary 1?
Or if I want to turn a variable into it's 2's complement (You probably already figured this has something to do with my previus question ;) )
I have another question though, I think I'll just ask it in this thread for now and just hope that someone sees it :)
How do you do bitwise operations in C/C++?
If I for example want to increase a variable by a binary 1?
Or if I want to turn a variable into it's 2's complement (You probably already figured this has something to do with my previus question ;) )
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
"I'm not young enough to know everything." - Oscar Wilde
#4
Posted 20 March 2008 - 01:21 AM
if you want to increase a binary to a nummber add 1 to it!(2^0=1 which will be appended at the end.)
bitwise operators operate starting from the least significant bit. The rest is implementation of truth tables.
bitwise operators operate starting from the least significant bit. The rest is implementation of truth tables.
God is real... unless declared an integer
my blog :: http://techarraz.com/
#5
Posted 20 March 2008 - 06:14 AM
ofc I know that a binary number will increase if you add 1 to it...I know the binary system as well as I know the decimal, hexadecimal and octal. But that is not really the answer to my question I'm afraid.
The question here wasn't how to add 1 to it, but _how_ to do binary operations (I.e. what is the _syntax_). Increasing a value was just an example.
Ok, so another example. (I'm still looking for the syntax..)
Say I have an 8-bit variable that I want to binary add with 01010101? Or I want to NOT (invert, 1's complement) it?
The question here wasn't how to add 1 to it, but _how_ to do binary operations (I.e. what is the _syntax_). Increasing a value was just an example.
Ok, so another example. (I'm still looking for the syntax..)
Say I have an 8-bit variable that I want to binary add with 01010101? Or I want to NOT (invert, 1's complement) it?
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
"I'm not young enough to know everything." - Oscar Wilde
#6
Posted 20 March 2008 - 08:11 AM
Walle said:
ofc I know that a binary number will increase if you add 1 to it...I know the binary system as well as I know the decimal, hexadecimal and octal. But that is not really the answer to my question I'm afraid.
c = a + b; /* "binary" addition */ c = a + b; /* "decimal" addition */ c = a + b; /* "octal" addition */ c = a + b; /* "hexadecimal" addition */Does this help clarify you inquiry a little?
#7
Posted 20 March 2008 - 01:32 PM
for c/c++ you can use >> or << for right and left bit shifts respectively. To do a certain number of bits give it as an argument after the operators. e.g.
int y = 4; int x; x = y << 2;This will assign x to y after it was shifted 2 bits left. Which would be 16. This is because 4 is binary as 100 and shifting the bits 2 to the left will give you a binary pattern of 10000, or 16.
-Dustin
www.theCprogrammer.com
www.theCprogrammer.com
#8
Posted 20 March 2008 - 10:45 PM
dcs said:
Binary, decimal, hexadecimal, octal, and what not are representations. The underlying value is a value; when you add a value, you add a value. The representation is just how you choose to see it. So there is no such thing as a difference in adding a "binary", "octal", "decimal", or "hexadecimal" "number".
c = a + b; /* "binary" addition */ c = a + b; /* "decimal" addition */ c = a + b; /* "octal" addition */ c = a + b; /* "hexadecimal" addition */Does this help clarify you inquiry a little?
I feel that I'm still not getting thrugh what I want. I do of course know that they merely are different representations of the same value. Even if 4d + 4d is exactly the same as 100b + 100b value-wise, the way it is written is not. I want to know the syntax for doing binary operations, using binary representation. I do not want to write 4 + 4, when i really want to write 100 + 100. It's all about clarity in the code.
I'm programming MCU's, mostly 8-bit MCU's from Microchip. Many times I want to do the operations using the binary representation to get a clear and neat code.
An example, maybe this will make you understand...
On a specific MCU (the PIC16F88) i have a total of 16 I/O pins, divided into two 8-bit ports. In a specific application ihave several sensors connected to one port, so each pin status depends on the status of the sensor connected to that pin. To the other port some output devices are connected, like LED's. I want to compare the pins of one port to a predetermined state, and based on the result do some changes to the other port (I.e: set of 8 pins.) Ie i don't want to compare each pin to a predetermined state, I wan't to compare all 8 pins (the port) at the same time. If ALL of them matches my predetermined state, then I do something..
For clarity purposes, I want to do that comparision using binary representation. Say that unless the status of the 8 pins are (binary) 10101010, the program will do nothing. Using decimal representation just isn't practical. It will make the code unclear. Using binary representation would make it very easy to know what pin should be what state, since each bit corresponds to a physical pin (and in the extent, a physical sensor). Using decimal, octal, hexadecimal, or any other imaginable nummerical system will just not be clear to anyone looking at the harware and firmware code.
"If (PORTA == 10101010)" Is a lot more clear than writing "If (PORTA == 170)" or "IF (PORTA == AA)", or "IF (PORTA == 252)"
Then, based on the results from the comparision, I might want to change state of pin 5 and 8 (IE bit 5 and 8) on PORTB. Changing the state of a pin is usually done by XOR'ing it with 1 (at least when using ASM, since that lets me change the state of it using only one instruction). So, to alter the state of pin 6 and 8, I would XOR the port with 10010000. Using the decimal representation, that would be 144...very un-intuive and unclear!
Do you understand what I mean now?
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
"I'm not young enough to know everything." - Oscar Wilde
#9
Posted 20 March 2008 - 10:46 PM
broncoslb said:
for c/c++ you can use >> or << for right and left bit shifts respectively. To do a certain number of bits give it as an argument after the operators. e.g.
int y = 4; int x; x = y << 2;This will assign x to y after it was shifted 2 bits left. Which would be 16. This is because 4 is binary as 100 and shifting the bits 2 to the left will give you a binary pattern of 10000, or 16.
Thank you! I often have to shift bits, and I know how to do it in assembler, but didn't know how to do it using c/c++!
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
"I'm not young enough to know everything." - Oscar Wilde
#10
Posted 21 March 2008 - 10:35 AM
Walle said:
I feel that I'm still not getting thrugh what I want. I do of course know that they merely are different representations of the same value. Even if 4d + 4d is exactly the same as 100b + 100b value-wise, the way it is written is not. I want to know the syntax for doing binary operations, using binary representation. I do not want to write 4 + 4, when i really want to write 100 + 100. It's all about clarity in the code.
Walle said:
I'm programming MCU's, mostly 8-bit MCU's from Microchip. Many times I want to do the operations using the binary representation to get a clear and neat code.
An example, maybe this will make you understand...
On a specific MCU (the PIC16F88) i have a total of 16 I/O pins, divided into two 8-bit ports. In a specific application ihave several sensors connected to one port, so each pin status depends on the status of the sensor connected to that pin. To the other port some output devices are connected, like LED's. I want to compare the pins of one port to a predetermined state, and based on the result do some changes to the other port (I.e: set of 8 pins.) Ie i don't want to compare each pin to a predetermined state, I wan't to compare all 8 pins (the port) at the same time. If ALL of them matches my predetermined state, then I do something..
For clarity purposes, I want to do that comparision using binary representation. Say that unless the status of the 8 pins are (binary) 10101010, the program will do nothing. Using decimal representation just isn't practical. It will make the code unclear. Using binary representation would make it very easy to know what pin should be what state, since each bit corresponds to a physical pin (and in the extent, a physical sensor). Using decimal, octal, hexadecimal, or any other imaginable nummerical system will just not be clear to anyone looking at the harware and firmware code.
"If (PORTA == 10101010)" Is a lot more clear than writing "If (PORTA == 170)" or "IF (PORTA == AA)", or "IF (PORTA == 252)"
Then, based on the results from the comparision, I might want to change state of pin 5 and 8 (IE bit 5 and 8) on PORTB. Changing the state of a pin is usually done by XOR'ing it with 1 (at least when using ASM, since that lets me change the state of it using only one instruction). So, to alter the state of pin 6 and 8, I would XOR the port with 10010000. Using the decimal representation, that would be 144...very un-intuive and unclear!
Do you understand what I mean now?
An example, maybe this will make you understand...
On a specific MCU (the PIC16F88) i have a total of 16 I/O pins, divided into two 8-bit ports. In a specific application ihave several sensors connected to one port, so each pin status depends on the status of the sensor connected to that pin. To the other port some output devices are connected, like LED's. I want to compare the pins of one port to a predetermined state, and based on the result do some changes to the other port (I.e: set of 8 pins.) Ie i don't want to compare each pin to a predetermined state, I wan't to compare all 8 pins (the port) at the same time. If ALL of them matches my predetermined state, then I do something..
For clarity purposes, I want to do that comparision using binary representation. Say that unless the status of the 8 pins are (binary) 10101010, the program will do nothing. Using decimal representation just isn't practical. It will make the code unclear. Using binary representation would make it very easy to know what pin should be what state, since each bit corresponds to a physical pin (and in the extent, a physical sensor). Using decimal, octal, hexadecimal, or any other imaginable nummerical system will just not be clear to anyone looking at the harware and firmware code.
"If (PORTA == 10101010)" Is a lot more clear than writing "If (PORTA == 170)" or "IF (PORTA == AA)", or "IF (PORTA == 252)"
Then, based on the results from the comparision, I might want to change state of pin 5 and 8 (IE bit 5 and 8) on PORTB. Changing the state of a pin is usually done by XOR'ing it with 1 (at least when using ASM, since that lets me change the state of it using only one instruction). So, to alter the state of pin 6 and 8, I would XOR the port with 10010000. Using the decimal representation, that would be 144...very un-intuive and unclear!
Do you understand what I mean now?
Bit shifting and bitwise operations
XOR:
[FONT="Fixedsys"]PORTB ^= 0x90; /* flip bits 7 and 4 (bits 8 and 5 to the new) */[/FONT]
Edited by dcs, 21 March 2008 - 10:48 AM.


Sign In
Create Account


Back to top









