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

How to convert a structure name into a string and vice versa?

4
Hi,

I need to convert a tsructure name into a string and vice versa. I don't really know how to do that in c.
Could anyone help me for that?

Thanks in advance,
May 30 '13 #1
11 7512
Oralloy
985 Expert 512MB
Hi Carrow,

Structures, by their nature are binary objects, not strings.

Do you just want the raw bytes of the structure, or are you trying to do something else?

At a first blush, you can use something like this:
Expand|Select|Wrap|Line Numbers
  1. tstructure obj;
  2. void * pointer0 = (void *)&obj;
  3. char * pointer1 = (char *)pointer0;
Hope that helps a little,
Oralloy
May 30 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
First you need to convert each struct member to a string.

Next you can use strcpy and strcat to build your final string.

Now in order to decode the final string later, insert a marker between each of the members. A comma is frequently used:

data, data, data, etc...

A string like this is called a CSV string for comma-separated-values.

You will need this marker to break up the final string into the members that it was made from.

Expand|Select|Wrap|Line Numbers
  1. strcpy(dest, str1);
  2. strcat(dest, ",");
  3. strcat(dest, str2);
  4. strcat(dest, ",");
  5. etc...
Here dest is the final string. dest only needs to be large enough to hold str1:

Expand|Select|Wrap|Line Numbers
  1. dest = malloc(sizeof(strlen(str1) + 1));
Note that strlen only returns the number of characters in str1 so you need to add 1 to make room for the null terminator that will be placed into dest by that first strcpy.

After that, strcat will take care of adding memory as needed to dest as dest grows in size as the member strings are appended.
May 30 '13 #3
Carrow
4
Hi everyone,
Thanks a lot for all your answers.
Actually, I'm working on a function that receive as parameter a structure. Then, I need to compare the 4 first letters of the struct parsed name, that why I need to convert a struct name into string.

The structures I'm using, contain a lot of members (unsigned long, pointers...), Do you think that the only way is to convert each member?

Carrow
May 31 '13 #4
Nepomuk
3,112 Expert 2GB
Whether you have to convert all members depends on what the comparison is supposed to tell you. What do those first 4 letters mean? If you want to check, whether two struct instances are the same, you will need to create some kind of hash function (which is a function which creates an identifier which is hopefully unique for each instance). If you only want to compare whether it contains something specific you can probably do much less.
May 31 '13 #5
Carrow
4
The four first letters indicate which configuration should I point into, what come after indicate other specifications for my function.
The problem with my code is that I'm not free to use whatever I want, and juste to make it works...

I found a way to avoid the use of structure name conversion into a string...But, I'm still interesting to know how to do that...

Thanks again for your answers :-)
May 31 '13 #6
Oralloy
985 Expert 512MB
Carrow,

It sounds like you have a function that takes an arbitrary structure, perhaps as a void pointer, and then dispatches activity based on the type of the structure?

Is that the problem that you are trying to solve?

Oralloy
May 31 '13 #7
weaknessforcats
9,208 Expert Mod 8TB
You can use strncpy to compare just the first 4 letters.

Expand|Select|Wrap|Line Numbers
  1. struct x
  2. {
  3.    char name[80];
  4. };
  5.  
  6. x obj;
  7. etc...
  8.  
  9. strncpy(obj.name, "Mike", 4);
Jun 4 '13 #8
Carrow
4
Oralloy,
My function receive as parameter the name of the structure (with specific instantiation). I just wanted to compare the structure name to do specific tasks.
That why I needed to convert the structure name into a string, to make this comparison.

But actually, I found another way to do that. Instead of comparing and then doing specific tasks and configurations, I used, another structure that includes configurations I needed and I used a pointer in my structure that pointed into this one.

:-)

Thanks again,

Carrow
Jun 5 '13 #9
weaknessforcats
9,208 Expert Mod 8TB
There is no structure name at run time. All the code has been converted to binary machine instructions.

Why is the structure name important at run time?
Jun 5 '13 #10
If you were to switch to a c++ compiler (and you can do this without changing to an obect-oriented design) then using the GNU compilers, you can include
Expand|Select|Wrap|Line Numbers
  1. const char* classname(void) {return __PRETTY_FUNCTION__; }
in every structure of interest. Then call it (parameter.classname()) and parse out the stucture name (just before the ::).

Your mileage may vary, depending on the compiler you're using, and it's not just a matter of changing to a c++ compiler, there being language differeces that can bite the unwary.

And it's bound to be more complicated than just this. And you can't get the name of the variable, just the structure type.
Jun 16 '13 #11
weaknessforcats
9,208 Expert Mod 8TB
In a C++ program you should never need to know the type of something. If you do need to know, it indicates there may be problems with your C++ design. This is especially true of the names of structure types.

However, in the debugging phase of development, you may need to know this information so it is available at all times through the debugger.

Once you go to release, if there is code to provide a structure name, then you have frozen your application. On objective of C++ is to allow old programs to use new code without needing to be recompiled.
Jun 16 '13 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Byron | last post by:
Hello, I am a newbie and would like to know if it is possible to convert a string back to a dictionary? For example, I can convert a dictionary to a string by doing this: >>> names =...
2
by: Steve - DND | last post by:
Just wondering if anyone out there has any code to convert a plural word to it's singular form and vice versa. Most of our database tables are named in a plural fashion. When we go to create...
9
by: james | last post by:
I have a FileStream retrieved from FileOpenDialog. I have a Byte which I intend to store for later use. What is the most efficient way of getting the File into my Byte and tehn back out to a new...
2
by: Urs Vogel | last post by:
Hi What's sthe best approach to convert char or char* strings to a BSTR and vice versa? I would like to omit OLE2A macros and allocate the memory myself, or is the the only way? Any hints? ...
4
by: rz0 | last post by:
Hi all, This is a question about both C89 and C99 and is based on my partial reading of the standard drafts (one from before C89 but mainly N1124). If appropriate, please give a...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
6
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now,...
1
by: abintoms | last post by:
Hello everyone, Can you tell me how to convert MS word documents to PDF format and vice versa.. I'm working on a project which requires me to do this.. Is there any jar files specific for this kind...
0
by: nudrat | last post by:
Hi, My requirement are : 1) In one PC in LAN Linus is installed which is used for net surfing.... rest of the systems have xp including exchange server. 2) We want to convert all email...
1
by: Maric Michaud | last post by:
Le Tuesday 24 June 2008 07:08:46 swapna mudavath, vous avez écrit : This is not valid xml, there is no commas in attribute list in xml. You could try with minidom if your xml stream isn't too...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...

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.