473,387 Members | 1,863 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,387 software developers and data experts.

encoding matter

hello

i have a program which manipulates ints which have their value in a
limited range, for instance, [-100..+100].
in a hash table i store these values. as this hash table can grow very
large, i whish to store this ints in a minimal number of characters
(1 for the previous range). so for each range of the program i create
an appropriate structure. for instance,
typedef struct
{
char c1;
char c2;
} __2chars;
if the int can have between 256 and 65536 values.

my problem is that i don't know hot to pass from an int to a __2chars
without making a big function.

any idea?

Sami Evangelista
Nov 14 '05 #1
8 1392
"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
hello

i have a program which manipulates ints which have their value in a
limited range, for instance, [-100..+100].
in a hash table i store these values. as this hash table can grow very
large, i whish to store this ints in a minimal number of characters
(1 for the previous range). so for each range of the program i create
an appropriate structure. for instance,
typedef struct
{
char c1;
char c2;
} __2chars;
if the int can have between 256 and 65536 values.

my problem is that i don't know hot to pass from an int to a __2chars
without making a big function.

any idea?

Sami Evangelista


Try making a union:

typedef struct

{

char c1;

char c2;

} __2chars;

typedef union

{

__2chars x;

int y;

} __2charsu;

Or try something like:

int x = c1 << 8 | c2; (or swap c1 w/ c2 for endian issues)

--

Elias
Nov 14 '05 #2

"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
hello

i have a program which manipulates ints which have their value in a
limited range, for instance, [-100..+100].
in a hash table i store these values. as this hash table can grow very
large, i whish to store this ints in a minimal number of characters
(1 for the previous range). so for each range of the program i create
an appropriate structure. for instance,
typedef struct
{
char c1;
char c2;
} __2chars;
if the int can have between 256 and 65536 values.

my problem is that i don't know hot to pass from an int to a __2chars
without making a big function.

any idea?


What about casting and letting the compiler do it?

E.g.

#include <stdio.h>

typedef struct{
char a,b;
} __2chars;

typedef struct {
char a;
} __1char;

main(){
__2chars a;
short int b;
__1char c;

short int * p;

b=257; // a two byte integer value to test with
p=(short int *)&a; // Make p point to the structure (but cast as an int
*)
*p=b; // Now assign the struct elements to the value of b
printf("a=%d b=%d\n",(int) a.a, (int) a.b); // this outputs 1 and 1
for 257

b=99; // Smaller value for single byte ints
p=(int *) &c; // make p point to the 1 byte struct
*p= (char) b; // truncate b by casting to a char..
printf("a=%d\n",(int) c.a); // c.a is set to 99
}

Sean
Nov 14 '05 #3
"lallous" <la*****@lgwm.org> wrote in message news:<bv************@ID-161723.news.uni-berlin.de>...
"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
hello

i have a program which manipulates ints which have their value in a
limited range, for instance, [-100..+100].
in a hash table i store these values. as this hash table can grow very
large, i whish to store this ints in a minimal number of characters
(1 for the previous range). so for each range of the program i create
an appropriate structure. for instance,
typedef struct
{
char c1;
char c2;
} __2chars;
if the int can have between 256 and 65536 values.

my problem is that i don't know hot to pass from an int to a __2chars
without making a big function.

any idea?

Sami Evangelista


Try making a union:

typedef struct

{

char c1;

char c2;

} __2chars;

typedef union

{

__2chars x;

int y;

} __2charsu;

Or try something like:

int x = c1 << 8 | c2; (or swap c1 w/ c2 for endian issues)


the problem is that u assume (in this case) that only the 16 last bits
of the int are used which is not necessarely the case.
Nov 14 '05 #4
"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
"lallous" <la*****@lgwm.org> wrote in message

news:<bv************@ID-161723.news.uni-berlin.de>...
"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
hello

i have a program which manipulates ints which have their value in a
limited range, for instance, [-100..+100].
in a hash table i store these values. as this hash table can grow very
large, i whish to store this ints in a minimal number of characters
(1 for the previous range). so for each range of the program i create
an appropriate structure. for instance,
typedef struct
{
char c1;
char c2;
} __2chars;
if the int can have between 256 and 65536 values.

my problem is that i don't know hot to pass from an int to a __2chars
without making a big function.

any idea?

Sami Evangelista


Try making a union:

typedef struct

{

char c1;

char c2;

} __2chars;

typedef union

{

__2chars x;

int y;

} __2charsu;

Or try something like:

int x = c1 << 8 | c2; (or swap c1 w/ c2 for endian issues)


the problem is that u assume (in this case) that only the 16 last bits
of the int are used which is not necessarely the case.


And you seem to assume that only the last 8 bits of the char are used ;-)

If you want portability, a funtion is your only option. Everything else,
including unions and pointers, relies on an implementation-defined
behaviour at best, an undefined behaviour at worst.

If you know the target platform well enough, and if you know that you
will never port your program anywhere else, by all means use some kind
of optimization such as unions, but only after you have made sure that
your structure contains no padding bytes. In that case (if you know your
target platform well, that is) you presumably also know a suitable type
with sizeof(type)==2. On many platforms, short fits the bill.
Nov 14 '05 #5
"Sean Kenwrick" <sk*******@hotmail.com> wrote in message
news:bv**********@sparta.btinternet.com...
"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
hello

i have a program which manipulates ints which have their value in a
limited range, for instance, [-100..+100].
in a hash table i store these values. as this hash table can grow very
large, i whish to store this ints in a minimal number of characters
(1 for the previous range). so for each range of the program i create
an appropriate structure. for instance,
typedef struct
{
char c1;
char c2;
} __2chars;
if the int can have between 256 and 65536 values.
What I forgot to mention in my other post is that it is not entirely clear
how you represent negative numbers in this representation.
my problem is that i don't know hot to pass from an int to a __2chars
without making a big function.

any idea?
What about casting and letting the compiler do it?

E.g.

#include <stdio.h>

typedef struct{
char a,b;
} __2chars;

typedef struct {
char a;
} __1char;

main(){
__2chars a;
short int b;
__1char c;

short int * p;

b=257; // a two byte integer value to test with
p=(short int *)&a; // Make p point to the structure (but cast as an

int *)
What makes you think that sizeof(short int)==2?
*p=b; // Now assign the struct elements to the value of b
printf("a=%d b=%d\n",(int) a.a, (int) a.b); // this outputs 1 and 1
for 257
What makes you think that char has 8 bits?
Even if it had, can you predict what would get printed for, let's say,
b==19500? A free hint: try it on a) PC and b) Macintosh.
And finally, what would get printed for b==-42?
b=99; // Smaller value for single byte ints
p=(int *) &c; // make p point to the 1 byte struct
*p= (char) b; // truncate b by casting to a char..
printf("a=%d\n",(int) c.a); // c.a is set to 99
}


Peter
Nov 14 '05 #6
On 30 Jan 2004 02:37:12 -0800, ev******@cnam.fr (Evangelista Sami)
wrote in comp.lang.c:
hello

i have a program which manipulates ints which have their value in a
limited range, for instance, [-100..+100].
in a hash table i store these values. as this hash table can grow very
large, i whish to store this ints in a minimal number of characters
(1 for the previous range). so for each range of the program i create
an appropriate structure. for instance,
typedef struct
{
char c1;
char c2;
} __2chars;


This is an illegal name in your C program. All symbols beginning with
either two underscores or an underscore followed by an upper case
letter are reserved for the implementation in all contexts.

--
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 #7
"Peter Pichler" <pi*****@pobox.sk> wrote in message news:<40********@mk-nntp-2.news.uk.tiscali.com>...
"Sean Kenwrick" <sk*******@hotmail.com> wrote in message
news:bv**********@sparta.btinternet.com...
"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
hello

i have a program which manipulates ints which have their value in a
limited range, for instance, [-100..+100].
in a hash table i store these values. as this hash table can grow very
large, i whish to store this ints in a minimal number of characters
(1 for the previous range). so for each range of the program i create
an appropriate structure. for instance,
typedef struct
{
char c1;
char c2;
} __2chars;
if the int can have between 256 and 65536 values.


What I forgot to mention in my other post is that it is not entirely clear
how you represent negative numbers in this representation.


that's the problem in fact. in other words, i only know that my ints
have their value in a specific range so i would like to encode them in
a minimum number of characters.
i may have a solution. its not very pretty, surely not portable, but
it could perhaps work. here it is :
if my integer's range is [-2500 .. 635] i first convert it into a long
int i then add -2500 to i and then put i into c1, i >> 8 into c2,
etc...
what do you think of it ? i know this is ugly, but could it work?
Nov 14 '05 #8
"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
"Peter Pichler" <pi*****@pobox.sk> wrote in message

news:<40********@mk-nntp-2.news.uk.tiscali.com>...
What I forgot to mention in my other post is that it is not entirely clear how you represent negative numbers in this representation.


that's the problem in fact. in other words, i only know that my ints
have their value in a specific range so i would like to encode them in
a minimum number of characters.
i may have a solution. its not very pretty, surely not portable, but
it could perhaps work. here it is :
if my integer's range is [-2500 .. 635] i first convert it into a long
int i then add -2500 to i and then put i into c1, i >> 8 into c2,
etc...
what do you think of it ? i know this is ugly, but could it work?


Ugly it might be, but it would work and it would even be portable. The only
problem is that you need to store the offset somewhere as well. How else
would you know that you need to subtract 2500 when you convert it back?

On the other hand, your range [-2500..635] fits comfortably in a short int.
You could use chars for the smallest range, shorts for larger etc, up to
long longs for the largest...

You could also use chars as per your original suggestions, converting your
number to unsigned first, before splitting it into several chars. When
converting it back, combine it to unsigned and convert to signed afterwards.

Peter
Nov 14 '05 #9

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

Similar topics

18
by: Klaus Alexander Seistrup | last post by:
Hi, After upgrading my Python interpreter to 2.3.1 I constantly get warnings like this: DeprecationWarning: Non-ASCII character '\xe6' in file mumble.py on line 2, but no encoding declared;...
6
by: Golawala, Moiz M (GE Infrastructure) | last post by:
Hi all, I have a some data is encoded into something thing. I want to find out the encoding of that piece of data. For example s = u"somedata" I want to do something like ...
3
by: xmlguy | last post by:
XmlTextReader myXmlReader = new XmlTextReader(args); string en = myXmlReader.Encoding.EncodingName; //Console.WriteLine(x); Error: Unhandled Exception: System.NullReferenceException: Object...
4
by: Stephen | last post by:
Using the code below I am trying, in VB .Net 2003, to serialise classes defined in a couple of XSD documents. The encoding for both is Unicode(UTF-8). However the resulting XML is encoded as...
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...
3
by: z f | last post by:
Hi, i run a web application in a commercial hosting. my web app need encoding of hebrew, but the default encoding on the hosting (win2003) is english - westren-european, and my pages are being...
2
by: jmhmaine | last post by:
During the course of development cycle I receive HTML files from designers that use Macs and PCs, but use tools other then Visual Studio. So these files sometimes are not UTF-8 Encoded. I see...
3
by: Dale Strickland-Clark | last post by:
A colleague has asked me this and I don't know the answer. Can anyone here help with this? Thanks in advance. Here is his email: I am trying to parse an HTML document using the xml.dom.minidom...
4
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
I do the following: StringBuilder xml = new StringBuilder(); XmlWriterSettings xws = new XmlWriterSettings(); xws.Encoding = Encoding.GetEncoding("iso-8859-1"); xws.Indent = true; XmlWriter...
8
by: Erwin Moller | last post by:
Hi group, I could use a bit of guidance on the following matter. I am starting a new project now and must make some decisions regarding encoding. Environment: PHP4.3, Postgres7.4.3 I must...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.