Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-14-2008, 11:52 AM
Walle Walle is offline
Newbie
 
Join Date: Mar 2008
Posts: 27
Credits: 0
Rep Power: 3
Walle is on a distinguished road
Default Converting float to char[4]

I have a float that I need converted to a 4-byte char-array.

using sprintf does not work.

What do you suggest?
__________________
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-14-2008, 04:31 PM
davHunter davHunter is offline
Newbie
 
Join Date: Oct 2007
Posts: 8
Credits: 0
Rep Power: 0
davHunter is on a distinguished road
Default Re: Converting float to char[4]

This should work.

Code:
float value;
char array[4];

...

memcpy(array, &value, sizeof (value));
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-14-2008, 08:42 PM
Walle Walle is offline
Newbie
 
Join Date: Mar 2008
Posts: 27
Credits: 0
Rep Power: 3
Walle is on a distinguished road
Default Re: Converting float to char[4]

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'm not young enough to know everything." - Oscar Wilde
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-20-2008, 04:21 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Credits: 30
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default Re: Converting float to char[4]

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.
__________________
God is real... unless declared an integer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-20-2008, 09:14 AM
Walle Walle is offline
Newbie
 
Join Date: Mar 2008
Posts: 27
Credits: 0
Rep Power: 3
Walle is on a distinguished road
Default Re: Converting float to char[4]

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?
__________________
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 03-20-2008, 11:11 AM
dcs dcs is offline
Programmer
 
Join Date: Mar 2008
Posts: 164
Credits: 216
Rep Power: 5
dcs will become famous soon enoughdcs will become famous soon enough
Default Re: Converting float to char[4]

Quote:
Originally Posted by Walle View Post
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.
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".
Code:
   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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-20-2008, 04:32 PM
broncoslb broncoslb is offline
Learning Programmer
 
Join Date: Feb 2008
Posts: 34
Credits: 0
Rep Power: 3
broncoslb is on a distinguished road
Default Re: Converting float to char[4]

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.
Code:
int y = 4;
int x;
x = y << 2;
This will ***ign 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-21-2008, 01:45 AM
Walle Walle is offline
Newbie
 
Join Date: Mar 2008
Posts: 27
Credits: 0
Rep Power: 3
Walle is on a distinguished road
Default Re: Converting float to char[4]

Quote:
Originally Posted by dcs View Post
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".
Code:
   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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-21-2008, 01:46 AM
Walle Walle is offline
Newbie
 
Join Date: Mar 2008
Posts: 27
Credits: 0
Rep Power: 3
Walle is on a distinguished road
Default Re: Converting float to char[4]

Quote:
Originally Posted by broncoslb View Post
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.
Code:
int y = 4;
int x;
x = y << 2;
This will ***ign 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 ***embler, but didn't know how to do it using c/c++!
__________________
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-21-2008, 01:35 PM
dcs dcs is offline
Programmer
 
Join Date: Mar 2008
Posts: 164
Credits: 216
Rep Power: 5
dcs will become famous soon enoughdcs will become famous soon enough
Default Re: Converting float to char[4]

Quote:
Originally Posted by Walle View Post
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.
There is no standard representation for binary literals. There may be implementation-specific offerings such as a leading 0b (similar to 0x).

Quote:
Originally Posted by Walle View Post
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?
The usual way is to become comfortable with reading hexadecimal literal notation. You may have your druthers, but this is the most widely-accepted and portable practice.

Bit shifting and bitwise operations

XOR:
Code:
PORTB ^= 0x90; /* flip bits 7 and 4 (bits 8 and 5 to the new) */

Last edited by dcs; 03-21-2008 at 01:48 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help please! zizimetalique C and C++ 5 11-17-2007 09:39 PM
Converting [willing to pay] DarkWalk General Programming 1 09-28-2007 03:25 PM
Missing "0"s when converting hex to byte lwings C# Programming 0 05-02-2007 08:43 PM
Converting DB values into strings ashleysmithd Pascal/Delphi 5 04-24-2007 11:21 AM
Converting between ebcdic and ACSII dirkfirst General Programming 3 12-30-2006 01:03 PM


All times are GMT -5. The time now is 10:38 AM.

Contest Stats

Xav ........ 1323.18
MeTh0Dz|Reb0rn ........ 1053.7
morefood2001 ........ 879.43
John ........ 877.37
marwex89 ........ 869.98
WingedPanther ........ 834.24
Brandon W ........ 749.07
chili5 ........ 310.39
Steve.L ........ 241.84
dcs ........ 216.02

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads