473,387 Members | 1,702 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,387 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 5398
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...

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.