472,784 Members | 824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

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,sizeof(rc));
memcpy(rc.msg, buffer, strlen(buf);
rc.len = strlen(buf);
return (const char *)&rc;
}

long convertCharToLong(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,100);

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

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

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

Thanks for all your help

jagmeena
ja*****@lycos.com
Jul 19 '05 #1
4 5347
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 convertCharToLong(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.com (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,sizeof(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 convertCharToLong(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,100);
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 = convertCharToLong(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 convertCharToLong 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.com


--
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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #4
On 19 Sep 2003 12:12:43 -0700, ja*****@lycos.com (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,sizeof(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 convertCharToLong(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,100);

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

v1 = convertCharToLong(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 convertCharToLong 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
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...
5
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 ...
7
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
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
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
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 ...
9
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...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
4
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. ...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.