Problems with Strlen


 
Thread Tools Search this Thread
Top Forums Programming Problems with Strlen
# 1  
Old 02-27-2002
Data Problems with Strlen

hello,

i have a problem with strlen. I have written this:

for(y=13,z=0; cInBuf[y]!=' ';y++)
{
cBuf[z]=cInBuf[y];
z++;
}

len = strlen(cBuf);
out=len/2;
fprintf(outfile,"F%i",out);

If strlen is e.g. 22, it write F22. I want to write F2F2.
How can i do this? Please help!!!
# 2  
Old 02-27-2002
I think you need to clarify a little more.
What kind of program is it?
What are cBuf[] and cInBuf[]?

In general, if you want to split an integer into its components for display, you must convert it to a string first
$ man sprintf

int n = 22;
int i;
char sbuf[6];
char tbuf[12];
/*
Especially when expanding a bufffer, make sure it holds 0's, otherwise strlen() will go amok
*/
memset(tbuf, 0, sizeof(tbuf));
/*
init should be done in general
*/
memset(sbuf, 0, sizeof(sbuf));

sprintf(sbuf,"%d", n);
/* sbuf[] now holds '2','2', 0 */

for (i = 0; sbuf[i]; i++)
{
tbuf[i*2] = sbuf[i]; /* for speed, you can shift here */
tbuf[i*2+1]='F';
}

/* Now, tbuf[] holds "2F2F", but somehow,
I don't think this is what you wanted?
*/


Atle
# 3  
Old 02-28-2002
I want to convert len, which is an integer, to an array. When it is an array, i can get F2F2.
I can convert it with "itoa". But how can i write this, to get F2F2? Please help!!!
# 4  
Old 02-28-2002
Then check the code above, this is exactly what it does.
It uses sprintf() to put the individual cifers into a char array, and then puts an 'F at every other place in the array.

If you want to use itoa() instead, that is OK, but I tried on both my Solaris, Linux box and FreeBSD box, and could not find a man itoa.

What system are you using?

I think you might want to use sprintf, unless itoa is part of your code?

Atle
# 5  
Old 02-28-2002
I thought that this was just a crazy thread. But I just typed:
echo 22 | dd conv=ebcdic | od -x
and sure enough a character 2 in ebcdic is a F2. But if this is supposed to be a conversion to ebcdic, this isn't really converging on a solution. Still, it might be an attempt at displaying what the bytes would be in ebcdic or something.

The lack of itoa always bothered me a little too. It seems like such an obvious companion to atoi. But it's not there, so I agree that sprintf is the general solution.

If you're absolutely sure that "i" contains an integer less than 100, this might be quick:

msd=i/10;
lsd=i%10;
printf("F%dF%d\n", msd, lsd);
# 6  
Old 02-28-2002
I have never used an EBCDIC machine, and had no idea that there was a correspondence ...
I sort of 'grew up' with itoa, it was defined like this:

char *itoa(int base, char *buf)

The reason is probably that it was coded in 8086 assembly under DOS, most low-level routines were.

About making a converter, though, I think it is much better with a table, conversion should boil down to an XLAT on Intel, and probably something to that effect on other processors.

Atle
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PHP: declared variables, strlen vs isset

greetings, pretty new to php and i think i might be missing some fundamental limitation of isset. i have two php scripts below that are executed by crond, one using --host X and one that does not. and below that are three different attempts at generating a command line that will be executed. the... (8 Replies)
Discussion started by: crimso
8 Replies

2. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies

3. Shell Programming and Scripting

If/then problems

#! /bin/bash # ask what the user would like to do CMD=$CMD MBA=$MB RS=$RS CT=$CT echo echo -n "What would you like to do?? REMEMBER WHEN PROGRAMMING ICP's TO SELECT CORRECT COMMAND ACCORDING TO NECCESSARY TYPE CODE! Please enter a command ct = program ctek ... (5 Replies)
Discussion started by: tdalyman
5 Replies

4. Programming

strlen for UTF-8

My OS (Debian) and gcc use the UTF-8 locale. This code says that the char size is 1 byte but the size of 'a' is really 4 bytes. int main(void) { setlocale(LC_ALL, "en_US.UTF-8"); printf("Char size: %i\nSize of char 'a': %i\nSize of Euro sign '€': %i\nLength of Euro sign: %i\n",... (8 Replies)
Discussion started by: cyler
8 Replies

5. UNIX for Dummies Questions & Answers

Problems with using less

Hello, I am having problems with using less on Linux version 2.6.18-92.1.17.el5 (brewbuilder@hs20-bc1-7.build.redhat.com) (gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)). I am using csh but have the same problems on bash. If I pipe something to less it works perfectly i.e. cat file | less... (9 Replies)
Discussion started by: z1dane
9 Replies

6. Programming

pointer arithmetic vs. strlen() & strnlen()?

I have been getting some flack recently for my use of strlen() and strnlen(). Honestly I have always just taken their functionality for granted as being the easiest way of getting the length of a string. Is it really so much better to do pointer arithmetic? What am I gaining besides more... (3 Replies)
Discussion started by: jjinno
3 Replies

7. Programming

'strlen' of a constant string

In a declaration, I have: const char comment_begin = "<!--"; const char comment_end = "-->"; const int comment_begin_len = strlen(comment_begin); const int comment_end_len = strlen(comment_end); When I compile, I get the warnings: emhttpc.c:64: warning: initializer element is not... (10 Replies)
Discussion started by: cleopard
10 Replies

8. UNIX for Dummies Questions & Answers

Few problems

Hi how can i do this? 1) shell script which writes data and time on to a file if filesystem exceeds 70% of space. 2) make entry to cron table to run a script every 15 mins. and can anyone expplain or demonstrate the difference between variables used in inside a function and outside a... (3 Replies)
Discussion started by: vivekshankar
3 Replies

9. Shell Programming and Scripting

Problem with the strlen function in ksh

Hello, Just a little problem with the ksh function : strlen I want to use this function in this little ksh program : while read line ; do TOTO=$line TOTONB=strlen($TOTO) echo $TOTONB (3 Replies)
Discussion started by: steiner
3 Replies

10. UNIX for Advanced & Expert Users

'make' problems (compliation problems?)

I'm trying to compile and install both most recent version of 'make' and the most recent version of 'openssh' on my Sparc20. I've run into the following problems... and I don't know what they mean. Can someone please help me resolve these issues? I'm using the 'make' version that was... (5 Replies)
Discussion started by: xyyz
5 Replies
Login or Register to Ask a Question