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 6664675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:38:14+00:00 2026-05-26T02:38:14+00:00

I’m building some code to read a RIFF wav file and I’ve bumped into

  • 0

I’m building some code to read a RIFF wav file and I’ve bumped into something odd.

The first 4 bytes of the file header are the word RIFF in big-endian ascii coding:

   0x5249 0x4646

I read this first element using:

char *fileID = new char[4];
filestream.read(fileID,4);

When I write this to screen the results are as expected:

std::cout << fileID << std::endl;
>>  RIFF

Now, the next 4 bytes give the size of the file, but crucially they’re little-endian.

So, I write a little function to flip the bytes, based on a union:

int flip4bytes(char* input){

   union flip {int flip_int; char flip_char[4];};

   flip.flip_char[0] = input[3];
   flip.flip_char[1] = input[2];
   flip.flip_char[2] = input[1];
   flip.flip_char[3] = input[0];

   return flip.flip_int;

 }

This looks good to me, except when I call it, the value returned is totally wrong. Interestingly, the following code (where the bytes are not reversed!) works correctly:

int flip4bytes(char* input){

   union flip {int flip_int; char flip_char[4];};

   flip.flip_char[0] = input[0];
   flip.flip_char[1] = input[1];
   flip.flip_char[2] = input[2];
   flip.flip_char[3] = input[3];

   return flip.flip_int;

 }

This has thoroughly confused me. Is the union somehow reversing the bytes for me?! If not, how are the bytes being converted to int correctly without being reversed?

I think there’s some facet of endian-ness here that I’m ignorant to..

  • 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-05-26T02:38:15+00:00Added an answer on May 26, 2026 at 2:38 am

    You are simply on a little-endian machine, and the “RIFF” string is just a string and thus neither little- nor big-endian, but just a sequence of chars. You don’t need to reverse the bytes on a little-endian machine, but you need to when operating on a big-endian.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.