Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7825971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:13:32+00:00 2026-06-02T09:13:32+00:00

I’m trying to translate the following Python code into C++: import struct import binascii

  • 0

I’m trying to translate the following Python code into C++:

import struct
import binascii


inputstring = ("0000003F" "0000803F" "AD10753F" "00000080")
num_vals = 4

for i in range(num_vals):
    rawhex = inputstring[i*8:(i*8)+8]

    # <f for little endian float
    val = struct.unpack("<f", binascii.unhexlify(rawhex))[0]
    print val

    # Output:
    # 0.5
    # 1.0
    # 0.957285702229
    # -0.0

So it reads 32-bit worth of the hex-encoded string, turns it into a byte-array with the unhexlify method, and interprets it as a little-endian float value.

The following almost works, but the code is kind of crappy (and the last 00000080 parses incorrectly):

#include <sstream>
#include <iostream>


int main()
{
    // The hex-encoded string, and number of values are loaded from a file.
    // The num_vals might be wrong, so some basic error checking is needed.
    std::string inputstring = "0000003F" "0000803F" "AD10753F" "00000080";
    int num_vals = 4;


    std::istringstream ss(inputstring);

    for(unsigned int i = 0; i < num_vals; ++i)
    {
        char rawhex[8];

// The ifdef is wrong. It is not the way to detect endianness (it's
// always defined)
#ifdef BIG_ENDIAN
        rawhex[6] = ss.get();
        rawhex[7] = ss.get();

        rawhex[4] = ss.get();
        rawhex[5] = ss.get();

        rawhex[2] = ss.get();
        rawhex[3] = ss.get();

        rawhex[0] = ss.get();
        rawhex[1] = ss.get();
#else
        rawhex[0] = ss.get();
        rawhex[1] = ss.get();

        rawhex[2] = ss.get();
        rawhex[3] = ss.get();

        rawhex[4] = ss.get();
        rawhex[5] = ss.get();

        rawhex[6] = ss.get();
        rawhex[7] = ss.get();
#endif

        if(ss.good())
        {
            std::stringstream convert;
            convert << std::hex << rawhex;
            int32_t val;
            convert >> val;

            std::cerr << (*(float*)(&val)) << "\n";
        }
        else
        {
            std::ostringstream os;
            os << "Not enough values in LUT data. Found " << i;
            os << ". Expected " << num_vals;
            std::cerr << os.str() << std::endl;
            throw std::exception();
        }
    }
}

(compiles on OS X 10.7/gcc-4.2.1, with a simple g++ blah.cpp)

Particularly, I’d like to get rid of the BIG_ENDIAN macro stuff, as I’m sure there is a nicer way to do this, as this post discusses.

Few other random details – I can’t use Boost (too large a dependency for the project). The string will usually contain between 1536 (83*3) and 98304 float values (323*3), at most 786432 (643*3)

(edit2: added another value, 00000080 == -0.0)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:13:36+00:00Added an answer on June 2, 2026 at 9:13 am

    This is what we ended up with, OpenColorIO/src/core/FileFormatIridasLook.cpp

    (Amardeep’s answer with the unsigned uint32_t fix would likely work also)

        // convert hex ascii to int
        // return true on success, false on failure
        bool hexasciitoint(char& ival, char character)
        {
            if(character>=48 && character<=57) // [0-9]
            {
                ival = static_cast<char>(character-48);
                return true;
            }
            else if(character>=65 && character<=70) // [A-F]
            {
                ival = static_cast<char>(10+character-65);
                return true;
            }
            else if(character>=97 && character<=102) // [a-f]
            {
                ival = static_cast<char>(10+character-97);
                return true;
            }
    
            ival = 0;
            return false;
        }
    
        // convert array of 8 hex ascii to f32
        // The input hexascii is required to be a little-endian representation
        // as used in the iridas file format
        // "AD10753F" -> 0.9572857022285461f on ALL architectures
    
        bool hexasciitofloat(float& fval, const char * ascii)
        {
            // Convert all ASCII numbers to their numerical representations
            char asciinums[8];
            for(unsigned int i=0; i<8; ++i)
            {
                if(!hexasciitoint(asciinums[i], ascii[i]))
                {
                    return false;
                }
            }
    
            unsigned char * fvalbytes = reinterpret_cast<unsigned char *>(&fval);
    
    #if OCIO_LITTLE_ENDIAN
            // Since incoming values are little endian, and we're on little endian
            // preserve the byte order
            fvalbytes[0] = (unsigned char) (asciinums[1] | (asciinums[0] << 4));
            fvalbytes[1] = (unsigned char) (asciinums[3] | (asciinums[2] << 4));
            fvalbytes[2] = (unsigned char) (asciinums[5] | (asciinums[4] << 4));
            fvalbytes[3] = (unsigned char) (asciinums[7] | (asciinums[6] << 4));
    #else
            // Since incoming values are little endian, and we're on big endian
            // flip the byte order
            fvalbytes[3] = (unsigned char) (asciinums[1] | (asciinums[0] << 4));
            fvalbytes[2] = (unsigned char) (asciinums[3] | (asciinums[2] << 4));
            fvalbytes[1] = (unsigned char) (asciinums[5] | (asciinums[4] << 4));
            fvalbytes[0] = (unsigned char) (asciinums[7] | (asciinums[6] << 4));
    #endif
            return true;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.