By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,129 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,129 IT Pros & Developers. It's quick & easy.

Casting Union as hexadecimal and back again

P: n/a
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

Aug 3 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
>> How would I convert this union to a hexadecimal?
???????
union account
{
struct
{
float f_balance;
int i_daysopened;
};
};

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.

Aug 3 '05 #2

P: n/a
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
upashu2 wrote:
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;


Aug 3 '05 #3

P: n/a

James DeClerk wrote:
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.

Aug 3 '05 #4

P: n/a

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.

Aug 3 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.