473,799 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

4-byte char from hex to float

2 New Member
he there,
I am trying to read out a .md3 file.
i am stuck on a point where i have to convert a 8-bit hexedecimal char to a float.
and change it from little endian to big endian

this is how far i got

Expand|Select|Wrap|Line Numbers
  1.     unsigned char tmp[4];
  2.     f32 test;
  3.  
  4.     fread(&tmp, 1, 4, f);
  5.     test = bitshiftf32(tmp);
  6.  
  7. f32 bitshiftf32(unsigned char val[4]) {
  8.     return val[0]|(val[1]<<8)|(val[2]<<16)|(val[3]<<24);
  9.     }
  10.  
  11.  
f being the file it readss out
test being the end result
Jan 23 '10 #1
4 9019
newb16
687 Contributor
As c/c++ standart doesn't mandate use of IEEE-754 32-bit floating-point for 'float' you probably have to convert it by yourself - get mantissa, add missing '1' bit, multiply 2 to the power of exponent.
Or, assuming that float is IEEE-754 32 bit, just
unsigned char val[4];
float x = *(float*)val;
Jan 23 '10 #2
donbock
2,426 Recognized Expert Top Contributor
Does the MD3 specification tell you how to interpret the 4-byte floating-point value? If so, then you should write code that explicitly decodes it accordingly; rather than casting the byte array into a native type. The casting trick might work for you, but it is brittle -- it might stop working when you upgrade to the next version of your compiler.
Jan 24 '10 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. f32 bitshiftf32(unsigned char val[4]) { 
  2.     return val[0]|(val[1]<<8)|(val[2]<<16)|(val[3]<<24); 
  3.     } 
val is an unsigned char. Probably 8 bits. So you can shift it left up to 7 places. What does shifting it left 16 or 24 do?
Jan 24 '10 #4
MrL0co
2 New Member
tnx newb16 that works
Jan 26 '10 #5

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

Similar topics

5
4041
by: George | last post by:
This program need to draw the some triangles into a 512 × 512 buffer (in memory). Or save it to a file. #include "project3.h" Image::Image(int xres, int yres): xres(xres), yres(yres) { image =new Color*; for (int i=0;i<yres;i++)
5
2539
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now when my function returns, the values stored in the char* are some garbage values (perhaps because I didn't allocate any memory for them).. but even if I allocate memory in the function, on the return of this function I see garbage.. here is my...
12
10092
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
6
7614
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
9
30524
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. How can I convert the sets of 4 bytes to floats? Thanks, Greg Book
8
8438
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer = "aabbccdd"; where aa is the LSB and dd is the MSB.
34
2694
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
4
2909
by: ohmmega | last post by:
hello, i've a 4 character string stored in a 4byte value (lets say an 32 bit integer). the integer holds the value: char*255^3+char*255^2+char*255^1+char*255^0. how can i convert this integer back to string? thx
3
2718
by: docmur | last post by:
Okay heres my problem I have a problem called Calc which the user enters 2 numbers and a sign to be done with what is entered EX: ./calc 200 + 200 or ./calc 200.20 + 200 Okay thats easy where I can't seem to get, my prof wants the input checked to see if the char *argv or char *argv contains a float or int. so I have to check for a decimal point. I know I can search the number like a string because it's in an array of char but...
0
9687
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, 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...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10231
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,...
0
10027
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 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...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4141
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
3
2938
bsmnconsultancy
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...

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.