Advertisement

03.11.2008 at 12:44PM PDT, ID: 23233014
[x]
Attachment Details

How to read Big-Endian and Little-Endian in C?

Asked by akohan in C Programming Language

Tags: C, gcc

Hello group,

Can somebody tell me where I can learn how to read data when they are stored as Big-Endian and Little-Endian. I'm back to C after years and don't know how to do this. I'm dealing with wave files.

Regards,
ak
Start Free Trial
 
Keywords: How to read Big-Endian and Little-En…
 
Loading Advertisement...
 
[+][-]03.11.2008 at 12:48PM PDT, ID: 21099375

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 01:06PM PDT, ID: 21099577

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 01:12PM PDT, ID: 21099650

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 01:14PM PDT, ID: 21099686

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: C, gcc
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 1
Solution Grade: A
 
 
[+][-]03.11.2008 at 01:16PM PDT, ID: 21099699

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 02:00PM PDT, ID: 21100297

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.11.2008 at 12:48PM PDT, ID: 21099375
endianness refers to the order of bytes :

        http://en.wikipedia.org/wiki/Endianness

big endian means that the most significant byte comes first
little endian means that the least significant byte comes first


>> Can somebody tell me where I can learn how to read data when they are stored as Big-Endian and Little-Endian.

Depends on what data it is. But in general, you want to take into account the order of the bytes.
 
03.11.2008 at 01:06PM PDT, ID: 21099577

Hi again,

Thanks! I read the text and now I'm reading RIFF chunk. The 2nd section of this chunk is Chunk Data Size. Using a hex editor is see f0 59 0d 00.
Now, how can I find out what the size is?

Regards,
ak
 
03.11.2008 at 01:12PM PDT, ID: 21099650
>> Now, how can I find out what the size is?


1:
2:
3:
4:
5:
6:
unsigned char *sizestr = "\xf0\x59\x0d\x00";     /* <--- the size from the WAVE file */
unsigned int size = 0;                           /* <--- has to be 32 bits wide */
int i = 0;
for (i = 0; i < 4; ++i) {                        /* <--- loops over the 4 bytes */
  size |= sizestr[i] << (4 * i);                 /* <--- inserts the bytes in the correct position in the unsigned int */
}
Open in New Window
 
03.11.2008 at 01:14PM PDT, ID: 21099686
Note that if you're sure that the system you're working on is little endian, you can just do the following. Your code will not be portable to a non little-endian system any more though.
1:
2:
unsigned char *sizestr = "\xf0\x59\x0d\x00";     /* <--- the size from the WAVE file */
unsigned int size = *((unsigned int*) sizestr);  /* <--- just copy the data straight into the unsigned int */
Open in New Window
Accepted Solution
 
03.11.2008 at 01:16PM PDT, ID: 21099699
Oh, and just to test your code, here is the value for size you should get for your example f0 59 0d 00 :

        0x000D59F0 = 874992
 
03.11.2008 at 02:00PM PDT, ID: 21100297

Wow! thank you so much. Also, I'm looking at the wave format and it shows:

              chunk data size =  (file size) - 8

I guess it means we have to add 8 bytes to the value you generated. Right? and do you have any idea why they store it with 8 bytes less?

Regards,
ak
 
 
03.11.2008 at 02:03PM PDT, ID: 21100328
Those 8 bytes are the first 8 bytes of the chunk : 4 bytes for "RIFF" and 4 bytes for the length.
 
 
03.11.2008 at 03:54PM PDT, ID: 21101302

should I consider them anyway right?!

 
 
03.11.2008 at 03:58PM PDT, ID: 21101319
>> should I consider them anyway right?!

You already did, didn't you ? You start reading the first 4 bytes of the chunk to verify that they really are "RIFF". Then you get the next 4 bytes which gives you the length of the rest of the chunk. And then you can read the rest of the chunk
 
 
03.11.2008 at 05:16PM PDT, ID: 21101840

One thing I'm not sure is about Chunk Data Size. The file I'm working with is a file created from 3 files.
Let's says:
file1.wav
file2.wav
file3.wav

now we have a final.wav file which contains all of those. Of course each has its own chunk. I need to read some information about the first wave file then get some information about the 2nd one and ....
I guess the best way is reading the header of the first one checking what the size is. By knowing the size I get to starting of the 2nd wave file and so on.

However, one thing I'm confused is about the "Chunk Data Size" becuaes RIFF Chunk has this field, also FORMAT Chunk and DATA Chunk. Which one is the one that I need to know where the next wave file starts?

Regards,
Ramsin
 
 
03.11.2008 at 05:18PM PDT, ID: 21101857

One thing I'm not sure is about Chunk Data Size. The file I'm working with is a file created from 3 files.
Let's says:
file1.wav
file2.wav
file3.wav

now we have a final.wav file which contains all of those. Of course each has its own chunk. I need to read some information about the first wave file then get some information about the 2nd one and ....
I guess the best way is reading the header of the first one checking what the size is. By knowing the size I get to starting of the 2nd wave file and so on.

However, one thing I'm confused is about the "Chunk Data Size" becuaes RIFF Chunk has this field, also FORMAT Chunk and DATA Chunk. Which one is the one that I need to know where the next wave file starts?

Regards

 
 
03.12.2008 at 01:36AM PDT, ID: 21103929
>> I guess the best way is reading the header of the first one checking what the size is. By knowing the size I get to starting of the 2nd wave file and so on.

That sounds like the best way indeed :)


>> However, one thing I'm confused is about the "Chunk Data Size" becuaes RIFF Chunk has this field, also FORMAT Chunk and DATA Chunk. Which one is the one that I need to know where the next wave file starts?

Afaik, there will be multiple format and data chunks in the file. So, the next Wave file should start at the next format chunk ...

But you can easily test that out. Just list all the chunks in the file, and see how many of which there are. If you're unsure, you can post the WAV file in question here ...
 
 
03.12.2008 at 02:05AM PDT, ID: 21104048

I think I didn't ask my quesiton well. In every signle wav file there are multiple chunks. However, each chunk no matter what has the "Chunk Data Size". Which one should I consider to find out the size of data (music data) then?

regards.
 
 
03.12.2008 at 02:11AM PDT, ID: 21104071
>> However, each chunk no matter what has the "Chunk Data Size".

Each of the chunks has a size field indeed, because you need to know how big the chunk is in order to read exactly the correct amount of data.


>> Which one should I consider to find out the size of data (music data) then?

The data chunk will have its own size field. That field says how much audio data there is in the chunk.


You have to see each chunk as a separate entity.

The RIFF chunk is a container for several sub-chunks, and each sub-chunk contains a specific type of data with a specific length.
 
 
 
20080716-EE-VQP-32 / EE_QW_2_20070628