473,405 Members | 2,373 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,405 software developers and data experts.

encoding / decoding questions

hello

i have to write a program in which i encode / decode datas into bits
vector.
and i do not really know how to do it.
my solution is to represent my bits as a char *.
so extracting the 32 first bits of a vector is done by :
bits[0] << 24 | bits[1] << 16 | bits[2] << 8 | bits[3]
or
bits[0] >> 24 | bits[1] >> 16 | bits[2] >> 8 | bits[3]
depending on how i encode my datas. is this correct?

so the program makes the hypothesis that a char takes 8 bits.
i dont know if this is correct. is this defined in C99?

any help would be very useful. in particuliar is there a more
efficient way to do this?
Nov 14 '05 #1
2 1034
Evangelista Sami <ev******@cnam.fr> spoke thus:
so the program makes the hypothesis that a char takes 8 bits.
i dont know if this is correct. is this defined in C99?


The macro CHAR_BIT is what you're looking for, although it's usually
8.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
On 12 Apr 2004 07:25:55 -0700, ev******@cnam.fr (Evangelista Sami)
wrote in comp.lang.c:
hello

i have to write a program in which i encode / decode datas into bits
vector.
and i do not really know how to do it.
my solution is to represent my bits as a char *.
so extracting the 32 first bits of a vector is done by :
bits[0] << 24 | bits[1] << 16 | bits[2] << 8 | bits[3]
or
bits[0] >> 24 | bits[1] >> 16 | bits[2] >> 8 | bits[3]
depending on how i encode my datas. is this correct?

so the program makes the hypothesis that a char takes 8 bits.
i dont know if this is correct. is this defined in C99?

any help would be very useful. in particuliar is there a more
efficient way to do this?


As already pointed out, the macro CHAR_BIT defined in <limits.h>
contains the number of bits in objects of the character types. It is
often best to just use 8, since CHAR_BIT must be at least 8, and if
you actually port this code to an architecture where character types
have more than 8 bits, it will still work.

There are a few other things you should do, however. First you should
used "unsigned char", not "signed char" or just plain "char", for the
elements of your array.

Second, you should cast the values to unsigned long when assembling
them. On most platforms, the unsigned chars of your array will
promote to signed ints. If ints have only 16 bits, shifting by 24 and
16 produce undefined behavior. Even if your ints have 32 bits,
shifting signed values can create a trap representation and cause
undefined behavior.

When dealing with bits, always use all unsigned types.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dot | last post by:
Hi all, I have written a Python huffman Encoding Module, for my own amusement. I thought it might be educational/entertaining for other people, so I've put it on my website and wrote about it a...
1
by: Slade | last post by:
Hi, I'm trying to use POST an image to a web page with WebRequest/WebResponse. Only problem is that I must be making an error somewhere in the encoding/decoding process. I've pasted below a bit...
9
by: Mark | last post by:
I've run a few simple tests looking at how query string encoding/decoding gets handled in asp.net, and it seems like the situation is even messier than it was in asp... Can't say I think much of the...
37
by: Zhiv Kurilka | last post by:
Hi, I have a text file with following content: "((^)|(.* +))§§§§§§§§" if I read it with: k=System.IO.StreamReader( "file.txt",System.Text.Encoding.ASCII); k.readtotheend()
0
by: henk-jan ebbers | last post by:
Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.9 Precedence: list List-Id: General discussion...
14
by: Zoro | last post by:
My task is to read html files from disk and save them onto SQL Server database field. I have created an nvarchar(max) field to hold them. The problem is that some characters, particularly html...
11
by: KWSW | last post by:
The other thread got too long so decided to start a new one. Got my huffman tree working and am now moving onto encoding and decoding my given text file. My idea now is to print out all the leaf...
9
by: KWSW | last post by:
Having settled the huffman encoding/decoding and channel modeling(thanks to the previous part on bitwise operation), the last part would be hamming encoding/decoding. Did some research as usual on...
0
by: Michele | last post by:
Hi there, I'm using a python script in conjunction with a JPype, to run java classes. So, here's the code: from jpype import * import os import random import math import sys
3
by: mviuk | last post by:
Hi, I'm looking for a system which detaches the decoding process from the encoding process. That is, I would like a system for encoding data, but even if both the encoded data and the encoding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.