Decimal to IEEE 754 Single-precision IEEE 754 code using C










0















We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.



This is the assignment:
The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.



The ouput of the program must be equal to the following :
“Introduce a float point number in decimal (scientific notation):
The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754). “



==============================================================================



The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned "struct" or "union" or any of that yet, because I saw those in other examples and I didn't understand anything.



I would really appreciate it if someone can guide me through making this code!



Thanks in advanced :)










share|improve this question

















  • 2





    "I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.

    – user694733
    Nov 15 '18 at 9:28















0















We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.



This is the assignment:
The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.



The ouput of the program must be equal to the following :
“Introduce a float point number in decimal (scientific notation):
The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754). “



==============================================================================



The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned "struct" or "union" or any of that yet, because I saw those in other examples and I didn't understand anything.



I would really appreciate it if someone can guide me through making this code!



Thanks in advanced :)










share|improve this question

















  • 2





    "I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.

    – user694733
    Nov 15 '18 at 9:28













0












0








0








We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.



This is the assignment:
The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.



The ouput of the program must be equal to the following :
“Introduce a float point number in decimal (scientific notation):
The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754). “



==============================================================================



The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned "struct" or "union" or any of that yet, because I saw those in other examples and I didn't understand anything.



I would really appreciate it if someone can guide me through making this code!



Thanks in advanced :)










share|improve this question














We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.



This is the assignment:
The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.



The ouput of the program must be equal to the following :
“Introduce a float point number in decimal (scientific notation):
The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754). “



==============================================================================



The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned "struct" or "union" or any of that yet, because I saw those in other examples and I didn't understand anything.



I would really appreciate it if someone can guide me through making this code!



Thanks in advanced :)







c binary ieee-754 converters single-precision






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 9:12









Nor AlexanianNor Alexanian

13




13







  • 2





    "I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.

    – user694733
    Nov 15 '18 at 9:28












  • 2





    "I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.

    – user694733
    Nov 15 '18 at 9:28







2




2





"I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.

– user694733
Nov 15 '18 at 9:28





"I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.

– user694733
Nov 15 '18 at 9:28












2 Answers
2






active

oldest

votes


















1















guide me through making this code!




As OP only requested a guide:



  1. gets a scientific notation floating point number

Research scanf() and the "%f" specifier



  1. prints it´s IEE754 single precision value in ... hexadecimal

Research type punning a float into uint32_t. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?



Research printf() and the "%x" specifier.



  1. prints ... in ... binary

There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)






share|improve this answer

























  • I will take a look at the links and try them out, ill let you know if it works

    – Nor Alexanian
    Nov 15 '18 at 17:41











  • Got the code to work, thank youu

    – Nor Alexanian
    Nov 21 '18 at 22:23


















0














The assignment you describe has three parts:



  1. Examine the characters of the numeral and convert them into a floating-point number.


  2. Print the value of the floating-point number.


  3. Print the bytes of a floating-point number.


For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf (to read from input) or sscanf (to read from an array of char). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int, then multiply 1 by pow(10, -5), multiply 2 by pow(10, -6), and so on, and then add the products.



For 2, print using printf.



For 3, use memcpy to copy the bytes from the float into a uint32_t (look up the header <stdint.h>) and print its bytes with printf (look up the header <inttypes.h> for the correct format specifier to use). Alternatively, use memcpy to copy the bytes from the float into an array of char, then print the char using printf with a format specifier that prints hexadecimal.






share|improve this answer























  • Thanks for the reply! I will try out the code and let you know how it turns out

    – Nor Alexanian
    Nov 15 '18 at 17:40











  • Just letting u know, i got it to work :)

    – Nor Alexanian
    Nov 21 '18 at 22:23










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315908%2fdecimal-to-ieee-754-single-precision-ieee-754-code-using-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















guide me through making this code!




As OP only requested a guide:



  1. gets a scientific notation floating point number

Research scanf() and the "%f" specifier



  1. prints it´s IEE754 single precision value in ... hexadecimal

Research type punning a float into uint32_t. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?



Research printf() and the "%x" specifier.



  1. prints ... in ... binary

There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)






share|improve this answer

























  • I will take a look at the links and try them out, ill let you know if it works

    – Nor Alexanian
    Nov 15 '18 at 17:41











  • Got the code to work, thank youu

    – Nor Alexanian
    Nov 21 '18 at 22:23















1















guide me through making this code!




As OP only requested a guide:



  1. gets a scientific notation floating point number

Research scanf() and the "%f" specifier



  1. prints it´s IEE754 single precision value in ... hexadecimal

Research type punning a float into uint32_t. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?



Research printf() and the "%x" specifier.



  1. prints ... in ... binary

There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)






share|improve this answer

























  • I will take a look at the links and try them out, ill let you know if it works

    – Nor Alexanian
    Nov 15 '18 at 17:41











  • Got the code to work, thank youu

    – Nor Alexanian
    Nov 21 '18 at 22:23













1












1








1








guide me through making this code!




As OP only requested a guide:



  1. gets a scientific notation floating point number

Research scanf() and the "%f" specifier



  1. prints it´s IEE754 single precision value in ... hexadecimal

Research type punning a float into uint32_t. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?



Research printf() and the "%x" specifier.



  1. prints ... in ... binary

There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)






share|improve this answer
















guide me through making this code!




As OP only requested a guide:



  1. gets a scientific notation floating point number

Research scanf() and the "%f" specifier



  1. prints it´s IEE754 single precision value in ... hexadecimal

Research type punning a float into uint32_t. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?



Research printf() and the "%x" specifier.



  1. prints ... in ... binary

There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 17:52

























answered Nov 15 '18 at 13:56









chuxchux

84k872154




84k872154












  • I will take a look at the links and try them out, ill let you know if it works

    – Nor Alexanian
    Nov 15 '18 at 17:41











  • Got the code to work, thank youu

    – Nor Alexanian
    Nov 21 '18 at 22:23

















  • I will take a look at the links and try them out, ill let you know if it works

    – Nor Alexanian
    Nov 15 '18 at 17:41











  • Got the code to work, thank youu

    – Nor Alexanian
    Nov 21 '18 at 22:23
















I will take a look at the links and try them out, ill let you know if it works

– Nor Alexanian
Nov 15 '18 at 17:41





I will take a look at the links and try them out, ill let you know if it works

– Nor Alexanian
Nov 15 '18 at 17:41













Got the code to work, thank youu

– Nor Alexanian
Nov 21 '18 at 22:23





Got the code to work, thank youu

– Nor Alexanian
Nov 21 '18 at 22:23













0














The assignment you describe has three parts:



  1. Examine the characters of the numeral and convert them into a floating-point number.


  2. Print the value of the floating-point number.


  3. Print the bytes of a floating-point number.


For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf (to read from input) or sscanf (to read from an array of char). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int, then multiply 1 by pow(10, -5), multiply 2 by pow(10, -6), and so on, and then add the products.



For 2, print using printf.



For 3, use memcpy to copy the bytes from the float into a uint32_t (look up the header <stdint.h>) and print its bytes with printf (look up the header <inttypes.h> for the correct format specifier to use). Alternatively, use memcpy to copy the bytes from the float into an array of char, then print the char using printf with a format specifier that prints hexadecimal.






share|improve this answer























  • Thanks for the reply! I will try out the code and let you know how it turns out

    – Nor Alexanian
    Nov 15 '18 at 17:40











  • Just letting u know, i got it to work :)

    – Nor Alexanian
    Nov 21 '18 at 22:23















0














The assignment you describe has three parts:



  1. Examine the characters of the numeral and convert them into a floating-point number.


  2. Print the value of the floating-point number.


  3. Print the bytes of a floating-point number.


For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf (to read from input) or sscanf (to read from an array of char). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int, then multiply 1 by pow(10, -5), multiply 2 by pow(10, -6), and so on, and then add the products.



For 2, print using printf.



For 3, use memcpy to copy the bytes from the float into a uint32_t (look up the header <stdint.h>) and print its bytes with printf (look up the header <inttypes.h> for the correct format specifier to use). Alternatively, use memcpy to copy the bytes from the float into an array of char, then print the char using printf with a format specifier that prints hexadecimal.






share|improve this answer























  • Thanks for the reply! I will try out the code and let you know how it turns out

    – Nor Alexanian
    Nov 15 '18 at 17:40











  • Just letting u know, i got it to work :)

    – Nor Alexanian
    Nov 21 '18 at 22:23













0












0








0







The assignment you describe has three parts:



  1. Examine the characters of the numeral and convert them into a floating-point number.


  2. Print the value of the floating-point number.


  3. Print the bytes of a floating-point number.


For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf (to read from input) or sscanf (to read from an array of char). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int, then multiply 1 by pow(10, -5), multiply 2 by pow(10, -6), and so on, and then add the products.



For 2, print using printf.



For 3, use memcpy to copy the bytes from the float into a uint32_t (look up the header <stdint.h>) and print its bytes with printf (look up the header <inttypes.h> for the correct format specifier to use). Alternatively, use memcpy to copy the bytes from the float into an array of char, then print the char using printf with a format specifier that prints hexadecimal.






share|improve this answer













The assignment you describe has three parts:



  1. Examine the characters of the numeral and convert them into a floating-point number.


  2. Print the value of the floating-point number.


  3. Print the bytes of a floating-point number.


For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf (to read from input) or sscanf (to read from an array of char). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int, then multiply 1 by pow(10, -5), multiply 2 by pow(10, -6), and so on, and then add the products.



For 2, print using printf.



For 3, use memcpy to copy the bytes from the float into a uint32_t (look up the header <stdint.h>) and print its bytes with printf (look up the header <inttypes.h> for the correct format specifier to use). Alternatively, use memcpy to copy the bytes from the float into an array of char, then print the char using printf with a format specifier that prints hexadecimal.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 14:00









Eric PostpischilEric Postpischil

78.5k883166




78.5k883166












  • Thanks for the reply! I will try out the code and let you know how it turns out

    – Nor Alexanian
    Nov 15 '18 at 17:40











  • Just letting u know, i got it to work :)

    – Nor Alexanian
    Nov 21 '18 at 22:23

















  • Thanks for the reply! I will try out the code and let you know how it turns out

    – Nor Alexanian
    Nov 15 '18 at 17:40











  • Just letting u know, i got it to work :)

    – Nor Alexanian
    Nov 21 '18 at 22:23
















Thanks for the reply! I will try out the code and let you know how it turns out

– Nor Alexanian
Nov 15 '18 at 17:40





Thanks for the reply! I will try out the code and let you know how it turns out

– Nor Alexanian
Nov 15 '18 at 17:40













Just letting u know, i got it to work :)

– Nor Alexanian
Nov 21 '18 at 22:23





Just letting u know, i got it to work :)

– Nor Alexanian
Nov 21 '18 at 22:23

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315908%2fdecimal-to-ieee-754-single-precision-ieee-754-code-using-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3