473,695 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting char * in struct to a long value

Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is

typedef struct Rec {
unsigned char msg[10];
unsigned long len;
} rec;

static rec rc;
const unsigned char * tmp;

const char* getRec(const char* buf)
{
memset(&rc,0,si zeof(rc));
memcpy(rc.msg, buffer, strlen(buf);
rc.len = strlen(buf);
return (const char *)&rc;
}

long convertCharToLo ng(const unsigned char *lVal)
{
......
}

void main()
{
long v1;
char stg[100];
char buffer[255];
memset(buffer,0 ,255);
printf("Enter a string: ");
fgets(buffer);

tmp = getRec(buffer);

memset(stg,0,10 0);

memcpy(stg, tmp, 10);
printf("1. STG VALUE --> (%s)\n", stg);

v1 = convertCharToLo ng(tmp+10);
printf("2. LEN VALUE ---> (%ld)\n", v1);
}

The problem I have is writing the convertCharToLo ng function:
How do I convert the string I get from the offset, to a long value.

Thanks for all your help

jagmeena
ja*****@lycos.c om
Jul 19 '05 #1
4 5424
jagmeena wrote:
Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.


Your code appears to be C. If you want a C answer, you won't get it in
comp.lang.c++. Please don't cross-post between comp.lang.c and
comp.lang.c++. Questions are rarely topical in both groups, and a good
solution in one is rarely a good solution in the other (or even a legal
solution).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
jagmeena wrote:

Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is
[snip] long convertCharToLo ng(const unsigned char *lVal)
{
......
}


strtol where lVal points to a string representation of a number (e.g.,
"12345").

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #3
On 19 Sep 2003 12:12:43 -0700, ja*****@lycos.c om (jagmeena) wrote in
comp.lang.c:
Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is
The code you have is littered with problems.
typedef struct Rec {
unsigned char msg[10];
unsigned long len;
} rec;

static rec rc;
const unsigned char * tmp;

const char* getRec(const char* buf)
{
memset(&rc,0,si zeof(rc));
memcpy(rc.msg, buffer, strlen(buf);
By looking ahead, I see that buffer is an array of 255 characters. If
you had correctly written code to get input from stdin into buffer,
which you didn't, there is a very good chance that strlen(buf) can be
anything up to 255. If it happens to be greater than 10, you will
overwrite the msg array and cause undefined behavior. If it is
greater than sizeof(struct Rec), which is implementation defined but
probably between 12 and 16, then you will really have heck to pay.

But if you don't overwrite your destination array, you still have
problems. You copy all the characters but not the terminating '\0',
so even if you fit everything in msg, it does not contain a string.

Well, actually as written it might contain a string the first time
this code is run, but only because rc is defined at file scope and so
initialized to all 0 bytes. If you ever more this structure inside a
function, where it belongs, you won't get a string. And if the user
types more than 9 characters, you still have the overflow.
rc.len = strlen(buf);
return (const char *)&rc;
Why would you do this? If you want an array of chars, use an array of
chars. If you use a structure, return a pointer to the structure.
From the rest of your code you don't know enough to be playing around
with casting types like this. Those who do know enough know to do it
only very rarely, and only for very good reasons.
}

long convertCharToLo ng(const unsigned char *lVal)
{
......
}

void main()
There is no "void main()", it's undefined behavior. In C, main()
returns an int.
{
long v1;
char stg[100];
char buffer[255];
memset(buffer,0 ,255);
Why the unnecessary call to memset(), which accomplishes nothing
useful?
printf("Enter a string: ");
On many platforms, this prompt will not appear until the user presses
the "enter" or "return" key at the end of his input. You need:

fflush(stdout);

....here to make sure it appears first.
fgets(buffer);
Obviously this is not code you compiled, because fgets() takes three
arguments, not one. Did you really use gets() but try to hide it
because you thought we would scold you?
tmp = getRec(buffer);

memset(stg,0,10 0);
What is with you and memset()? Is your computer too fast, so you
waste clock cycles to slow it down?
memcpy(stg, tmp, 10);
printf("1. STG VALUE --> (%s)\n", stg);
Of course, you might well not have a valid string here, so say hello
to undefined behavior.
v1 = convertCharToLo ng(tmp+10);
Exactly what do you think is at tmp+10? If your code did not blow up
with the undefined behavior of overflowing the array, the next thing
it did was to store a size_t value in rc.len. Now since tmp is
actually a pointer to the first byte of rc, tmp+10 points to the first
byte in rc after rc.msg. That is either a padding byte or the
location of rc.len. It certainly does not point to any characters, if
points to either a padding byte or an unsigned long.
printf("2. LEN VALUE ---> (%ld)\n", v1);
}

The problem I have is writing the convertCharToLo ng function:
How do I convert the string I get from the offset, to a long value.
If there is a string in rc at all, tmp+10 does not point to it.
Thanks for all your help

jagmeena
ja*****@lycos.c om


--
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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #4
On 19 Sep 2003 12:12:43 -0700, ja*****@lycos.c om (jagmeena) wrote:
Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is
Actually, this is probably not your code. (If it were, you would not
be able to compile it due to the syntax errors.) In the future, you
will get better responses if you use cut and paste instead of
retyping.

typedef struct Rec {
unsigned char msg[10];
unsigned long len;
} rec;

static rec rc;
const unsigned char * tmp;

const char* getRec(const char* buf)
{
memset(&rc,0,si zeof(rc));
memcpy(rc.msg, buffer, strlen(buf);
There is no buffer. Did you mean buf?

Is there some reason you didn't want to use strcpy?

Are you aware that this will copy all the characters including the
'\n' that fgets normally places in the buffer?

rc.msg is only 10 characters. Your code does nothing to insure the
input will fit. If it doesn't, you invoke undefined behavior.
rc.len = strlen(buf);
return (const char *)&rc;
}

long convertCharToLo ng(const unsigned char *lVal)
{
Look up the strtol function in your reference.
......
}

void main()
main returns int period
{
long v1;
char stg[100];
char buffer[255];
memset(buffer,0 ,255);
Why bother since you will read into this array immediately.
printf("Enter a string: ");
Without a fflush(stdout), there is no guarantee that this will be
visible when the next statement executes.
fgets(buffer);
Did you forget a couple of parameters?

tmp = getRec(buffer);
tmp is declared as const unsigned char *. getRec returns a const
char*. These are not compatible without a cast.
memset(stg,0,10 0);

memcpy(stg, tmp, 10);
printf("1. STG VALUE --> (%s)\n", stg);

v1 = convertCharToLo ng(tmp+10);
tmp points to rc.msg. You have no idea what tmp+10 points to since
there could be padding in the struct after msg.
printf("2. LEN VALUE ---> (%ld)\n", v1);
}

The problem I have is writing the convertCharToLo ng function:
How do I convert the string I get from the offset, to a long value.


You are not getting a string from the offset. If you want to convert
the input to long, you should be using tmp (after ensuring it points
to an area long enough to hold the entire string).

<<Remove the del for email>>
Jul 19 '05 #5

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

Similar topics

4
16461
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope someone can help Thanks
5
1283
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is typedef struct Rec { unsigned char msg; unsigned long len;
7
5290
by: Angus Comber | last post by:
Hello Here is my code so far. Is this correct/incorrect/along the right lines/other? #include <stdio.h> #include <string.h> #include <search.h> struct mystruct {
31
7735
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
18
4053
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
12
2112
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef unsigned char byte; // 'byte' is an 8-bit unsigned value
9
5162
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte level parsing what ever may be the type of data.The data would be coming from an external environment.When I parse int,char at byte level,I get right values,where as floating point just prints 0.000000 to the screen.Given below is a sample program...
29
9976
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
4
3609
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. int WINAPI ImgSave(_T_Scan_Param *ScanP, unsigned char *szMicr, int *num_chars, int y0, int y1, int cOcrCode_option) struct _T_Scan_Param { char file1;
0
8630
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8568
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
9115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8982
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
8845
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
8825
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
4340
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
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.