473,804 Members | 3,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting Union as hexadecimal and back again

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
4 2701
>> 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_da ysopened=...
store & retrieve new_acct.b in database
..... = new_acct.a.f_ba lance;

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_idaysopene d);
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
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_da ysopened=...
store & retrieve new_acct.b in database
.... = new_acct.a.f_ba lance;

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_da ysopened=...
store & retrieve new_acct.b in database
.... = new_acct.a.f_ba lance;


Aug 3 '05 #3

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_da ysopened=...
store & retrieve new_acct.b in database
.... = new_acct.a.f_ba lance;

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3484
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
2
4360
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes undefined behavior because my reference to the union is via a
1
1886
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for queueing ip fragments as struct ipq { struct ipq *next; /* linked list pointers */ struct list_head lru_list; /* lru list member */ u32 user; u32 saddr;
12
1962
by: candy_init | last post by:
I recently came across the following CAST macro which can cast anything to any other thing: #define CAST(new_type,old_object) (*((new_type *)&old_object)) union { char ch; int i; } my_union;
8
5665
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to reinterpret_cast for the code
31
3200
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
7
13717
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
30
3290
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
3
3531
by: anon.asdf | last post by:
Hi! I am not succeeding in (statically) initializing a variable of the following type: union myunion { short arr2; int arr4; };
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.