473,395 Members | 1,377 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,395 software developers and data experts.

help needed in converting hex to float......

Heyy Guys...
Im very new to vb.
Im using vb6 and i want to convert a 32 bit ieee 754 number into a floating point number.
for example.
'BE C3 F2 DF' (in hexa representation) should be converted to -0.382712....

i have worked this c++ code over which seems to be running fine.
Need to convert the code in VB.
please help me guys.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4.  
  5. void main()
  6. {
  7. //clrscr();
  8. unsigned long int number,value;
  9. int mantissa,exponent;
  10. long int sign;
  11. cout<<"ENTER THE NUMBER.APPEND 0X BEFORE A HEX NUMBER :"<<endl;
  12. cin>>number;
  13. sign=number & 0x80000000;
  14. if(sign==0x80000000) //most significant bit is 1
  15. {
  16. sign=-1;
  17. }
  18. else sign=1;
  19. //cout<<number<<endl;
  20. //mantissa=number & 0x7F800000;
  21. mantissa=number>>23;
  22. mantissa=mantissa & 0xFF;
  23. //cout<<mantissa<<endl;
  24. exponent=mantissa-127;
  25. //cout<<exponent;
  26. value=number & 0x007FFFFF;//extract the characteristic
  27. //cout<<value;
  28. long int mask=0x00800000;
  29. long int masked;
  30. double charac,flot;
  31. for(int i=1;i<24;i++)
  32. {
  33. masked=value & mask;
  34. //cout<<mask<<endl;
  35. if (masked==mask)//value at that bit is 1
  36. {
  37. flot=1.0;
  38. for(int k=0;k<i-1;k++)
  39. {
  40. flot=flot*0.5;
  41. //cout<<flot<<endl;
  42. }
  43. charac=charac+flot;
  44. //cout<<(1/pow(2,i))<<endl;
  45. //cout<<charac<<endl;
  46. }
  47. mask=mask>>1;
  48.  
  49. }
  50. charac=charac+1.0;
  51. //cout<<"charac"<<charac<<endl;
  52. //cout<<"exponent"<<exponent;
  53. float multiplier;
  54. multiplier=1.0;
  55. if(exponent>0)
  56. {
  57. for(i=0;i<exponent;i++)
  58. {
  59. multiplier=multiplier*2;
  60. }
  61. }
  62. if(exponent<0)
  63. {
  64. exponent=exponent*-1;
  65. for(i=0;i<exponent;i++)
  66. {
  67. multiplier=multiplier*2;
  68. }
  69. multiplier=1/multiplier;
  70. }
  71. charac=charac*multiplier*sign;
  72. cout<<charac;
  73. getch();
  74. }
  75.  
  76.  

The funny thing is, the same code when executed in VC++, gives different output. Any views on that...
Mar 2 '07 #1
4 5536
willakawill
1,646 1GB
Hi. Looks like your question is too involved. Try the solution yourself and come back if you are stuck.
Mar 4 '07 #2
Killer42
8,435 Expert 8TB
Hi. Looks like your question is too involved. Try the solution yourself and come back if you are stuck.
A good first step would be to work out (and write down) what the C++ program does, step by step. Once you do that, it should be quite simple to convert the individual steps to VB. And it will ensure that you understand the process.

Of course, given the different capabilities of different languages, the process may end up somewhat different. But the better you understand it, the easier it will be.
Mar 5 '07 #3
thanks to everyone.
i finally found d solution using the powerful copymemory( ) function.
thank u again for your efforts.
Mar 6 '07 #4
willakawill
1,646 1GB
thanks to everyone.
i finally found d solution using the powerful copymemory( ) function.
thank u again for your efforts.
Congratulations. I thought you would never find an answer to this.
Mar 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Giovanni Azua | last post by:
Hello all, I need to return back to TSQL two numeric values from an Extended Stored Procedure developed in C. As result my C dll program produces two float values, the TSQL side expects to have...
6
by: Robert P. Stearns | last post by:
I have been trying to convert the following simple table into CSS. <table> <tr><th colspan=2>Header Area</th></tr> <tr><th>Navigation Area</th><th>Information Area</th></tr> <tr><th...
5
by: el prinCipante | last post by:
I'm getting tired of the following error message. Compiler Error message : Error: Need explicit cast to convert from: float to: float * I am trying to use a routine from the Numerical Recipes...
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
4
by: Yasin Cepeci | last post by:
I ve get float data from serial port. I ve taken it in the form of hex by modbus protocol. I know it is float but I couldnt convert it there is a few sample data below; B3 33 43 34 = 180.699997...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
2
by: IdlePhaedrus | last post by:
Hi, I have a FFT routine that I converted from C++ to VB in a module as follows: Const M_PI = 3.1415926535897931 ' Fast Fourier Transform Public Sub FFT(ByRef rex() As Single, ByRef imx() As...
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
tracyyun
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...

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.