Casting Union as hexadecimal and back again

J

James DeClerk

Hi everyone.

I'm new to C++ and I've got a seemingly tough problem to tackle.

I have a union. This union needs to be converted into hexadecimal
format. The hexadecimal number is then inserted into a Binary (255)
column in a SQL Server DB. The program will be complied on the unix
platform using gcc.

union account
{
struct
{
float f_balance;
int i_daysopened;
};
};

Question 1) How would I convert this union to a hexadecimal?

Question 2) Would the hexadecimal best be stored as a data type of
char with a length of 255?

Question 3) Once converted to a hexadecimal, how would I recast it
back in to a union? Would it simply be something like:

//x is the char[255] of the hex number
account new_acct;
new_acct = (account) x;

Or is there a more elaborate way of doing this. Any help would be much
appreciated.

Thanks
 
U

upashu2

How would I convert this union to a hexadecimal?
???????Only single member in union?and that is unnamed member. what is use of
union? how will i access that struct in union? I am in confusion??????

Do you want this/
union account
{
struct
{
float f_balance; ///4 byte
int i_daysopened; ///4 byte
} a;
char b[8]; ///8 byte , hopes 4 byte aligenment or 1 byte
alignment packing
};
account new_acct;
new_acct .a.f_balance =....
new_acct.a.i_daysopened=...
store & retrieve new_acct.b in database
..... = new_acct.a.f_balance;

OR if u want the string represention,
struct A
{
float f_balance;
int i_daysopened;
};
A a;
char b[255];
sprintf(b,"%f %d",a.f_balance,a.i_idaysopened);
store b in database as a string.
___________________________________________________
If it is top posting , avoid it.
If it is for posting , read it.
 
J

James DeClerk

Hi

It would be more like the first example. Only the char field would be
255. I excluded it because I thought it was more simplistic, not
realizing it was an integral part.

union account
{
struct
{
float f_balance; ///4 byte
int i_daysopened; ///4 byte
} a;
char b[255];
alignment packing
};
account new_acct;
new_acct .a.f_balance =....
new_acct.a.i_daysopened=...
store & retrieve new_acct.b in database
.... = new_acct.a.f_balance;

Now from here...how would I convert this to a char(255) hex number? Or
is it already stored for me in the char b[255] field?

Thanks
 
V

velthuijsen

James said:
Hi

It would be more like the first example. Only the char field would be
255. I excluded it because I thought it was more simplistic, not
realizing it was an integral part.

union account
{
struct
{
float f_balance; ///4 byte
int i_daysopened; ///4 byte
} a;
char b[255];
alignment packing
};
account new_acct;
new_acct .a.f_balance =....
new_acct.a.i_daysopened=...
store & retrieve new_acct.b in database
.... = new_acct.a.f_balance;

Now from here...how would I convert this to a char(255) hex number? Or
is it already stored for me in the char b[255] field?

Thanks

Please, do not top post (That is put your reply before the section of
text you are replying to), it makes for difficult reading.

Now to your question:
At this point you could care less what type the data is. This is
because the computer allows you to store data in any way as long as it
is binary (to paraphrase Ford).
This is the reason that the conversion trick with a union works (even
though technically speaking it is a hack).
All you have to do is copy b to the database. Then when you later need
it you can copy it back (into b).

I do have a question though. Why not 2 entries in the database, one for
balance one for age of the account? That way you could use SQL directly
to search on the data instead of having to copy out the entire
column/database, convert it back and then do the search.
 
J

James DeClerk

This would make life definately simpler for me. I'm dealing with
someone else's design. I suspect he made the data this way because for
space contraints. We're dealing with large data sets, and the person
also wanted some level of portability with it. I.E. the ability to
burn the database on a CD for backups or shipping it off to another
dept for what not. Whether this solves the problem is
another question. Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top