By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

using '\0' in char string

P: n/a
Dear all,

Our bitmap has some x00 values ( '\0' ) and i am storing it in char*
array. the problem is that the '\0' is treated as eos character in c
and it is truncating it so the characters after it are not pass to the
function.

here is the code snippet
/*
DATA8583[0].data[0] = ( char ) ( unsigned int ) 0xF0;
DATA8583[0].data[1] = ( char ) ( unsigned int ) 0x30;
DATA8583[0].data[2] = ( char ) ( unsigned int ) 0x01;
DATA8583[0].data[3] = ( char ) ( unsigned int ) 0x41;
DATA8583[0].data[4] = ( char ) ( unsigned int ) 0x08;
DATA8583[0].data[5] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[6] = ( char ) ( unsigned int ) 0x80;
DATA8583[0].data[7] = ( char ) ( unsigned int ) 0x20;
DATA8583[0].data[8] = ( char ) ( unsigned int ) 0x03;
DATA8583[0].data[9] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[10] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[11] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[12] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[13] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[14] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[15] = ( char ) ( unsigned int ) 0x00;

*/

here DATA8583[0].data is a char* variable. Please suggest some work
around or is there other/better way of doing this.
Best regards,

Salman Makhani

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
techno <sa***********@gmail.com> wrote:
Our bitmap has some x00 values ( '\0' ) and i am storing it in char*
array. the problem is that the '\0' is treated as eos character in c
and it is truncating it so the characters after it are not pass to the
function.
If you pass that array to a function everything gets passed to the
function and nothing is truncated (only a pointer to the first
character of the array gets passed to the function anyway). What
you can't do, of course, is using the string handling functions
(the ones with names starting with "str") with this array because
it's not meant to be a string but just an array of chars.
here is the code snippet
DATA8583[0].data[0] = ( char ) ( unsigned int ) 0xF0;
DATA8583[0].data[1] = ( char ) ( unsigned int ) 0x30; here DATA8583[0].data is a char* variable.


Since these data rather obviously aren't signed it would probably
better to make DATA8583[0].data an unsigned char pointer. Then you
can initialize the elements just as

DATA8583[0].data[0] = 0xF0;

without all that ugly casts.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2

P: n/a
To summarise, your want to manipulate character arrays that contain one
or more '\0's. You are experiencing difficulties because many standard
C functions use the character '\0' to denote the end-of-string.

Clearly, the solution is to write / use counterparts of the
zero-denotes-EOS functions that either 1) take the length of the array
as an additional argument; or 2) use a different character or sequence
of characters to denote EOS.

--
-trent
WITHOUT ULTRA PORT GUARD 2000, HACKERS CAN STEAL YOUR CHILDREN!
Nov 14 '05 #3

P: n/a
techno wrote:
Dear all,

Our bitmap has some x00 values ( '\0' ) and i am storing it in char*
array. the problem is that the '\0' is treated as eos character in c
and it is truncating it so the characters after it are not pass to the
function.

here is the code snippet [Snip] here DATA8583[0].data is a char* variable. Please suggest some work
around or is there other/better way of doing this.
Best regards,

Salman Makhani

Hi, I'm not a C expert but I'm messing about with bitmaps at the mo so
I'll have a go at helping you.

You can store the value 0 ('\0') in an array of char (char being a small
integer type that just happens to be used for strings of text), but
functions written to deal with strings of text ("Hello world") work with
arrays of char terminated by '\0'.

Now, is it meaningful or useful to use string-handling functions on
bitmap data? I can't really think why you would want to, except perhaps
in reading or writing to/from files.

If you are using fgets, fprintf, something like that,
fprintf(filepointer, "%s", DATA8583[0].data);
- you should probably be using binary functions instead of text ones:
fwrite(DATA8583[0].data, sizeof(unsigned char),
SIZE_TO_WRITE, filepointer);
(By the way, sizeof(char) and sizeof(unsigned char) is guarantees to be 1.)

If you need some functionality analogous to string functions but
ignoring '\0' and using some length defined by you, I think you'll have
to write your own.

Best of luck,
Rob M
Nov 14 '05 #4

P: n/a


techno wrote:
Dear all,

Our bitmap has some x00 values ( '\0' ) and i am storing it in char*
array. the problem is that the '\0' is treated as eos character in c
and it is truncating it so the characters after it are not pass to the
function.

here is the code snippet
/*
DATA8583[0].data[0] = ( char ) ( unsigned int ) 0xF0;
DATA8583[0].data[1] = ( char ) ( unsigned int ) 0x30;
DATA8583[0].data[2] = ( char ) ( unsigned int ) 0x01;
DATA8583[0].data[3] = ( char ) ( unsigned int ) 0x41;
DATA8583[0].data[4] = ( char ) ( unsigned int ) 0x08;
DATA8583[0].data[5] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[6] = ( char ) ( unsigned int ) 0x80;
DATA8583[0].data[7] = ( char ) ( unsigned int ) 0x20;
DATA8583[0].data[8] = ( char ) ( unsigned int ) 0x03;
DATA8583[0].data[9] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[10] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[11] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[12] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[13] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[14] = ( char ) ( unsigned int ) 0x00;
DATA8583[0].data[15] = ( char ) ( unsigned int ) 0x00;

*/

here DATA8583[0].data is a char* variable. Please suggest some work
around or is there other/better way of doing this.


Make the 'data' member of the struct/union an array of
unsigned char.
You pass a pointer to the first element in the array. Using
this value, you can access all elements in the defined array
even elements after a so called eos.

Look at and run this example. You will see that function PrintTEST
will print the value of element 5 even though the value of
element 4 is '\0'.

#include <stdio.h>

struct TEST
{
unsigned char data[400];
unsigned size;
};

void PrintTEST( const unsigned char *p);
void TESTF(struct TEST *p);

int main(void)
{
struct TEST DATA8583 = {{0xF0,0x30,0x01,'\0',0xFF},400};

TESTF(&DATA8583);
return 0;
}

void PrintTEST( const unsigned char *p)
{
int i;

puts("START Function PrintTEST");
for(i = 0; i < 5; i++)
printf("data[%d] = 0x%02X\n",i,p[i]);
puts("ENDOF Function PrintTEST");
return;
}

void TESTF(struct TEST *p)
{
puts("START Function TESTF");
PrintTEST(p->data);
printf("size = %u\n",p->size);
puts("ENDOF Function TESTF");
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5

P: n/a
techno wrote:
...
Our bitmap has some x00 values ( '\0' ) and i am storing it in char*
array. the problem is that the '\0' is treated as eos character in c
and it is truncating it so the characters after it are not pass to the
function.
...


Presence of '\0' values inside string array doesn't affect the way such
arrays are passed to functions in C language. Apparently, you
misinterpreted the problem. Provide more details about what's happening.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #6

P: n/a
Andrey Tarasevich wrote:
...
Our bitmap has some x00 values ( '\0' ) and i am storing it in char*
array. the problem is that the '\0' is treated as eos character in c
and it is truncating it so the characters after it are not pass to the
function.
...
Presence of '\0' values inside string array doesn't affect the way such

^^^^^^

I meant "... inside character (char) array ... "
arrays are passed to functions in C language. Apparently, you
misinterpreted the problem. Provide more details about what's happening.


--
Best regards,
Andrey Tarasevich
Nov 14 '05 #7

P: n/a
A character array in C, by definition, is terminated by '\0' when
interpreted as a *STRING*. However, it otherwise just holds the '\0'
value like any other so long as you interpret your array as just that,
a character array. That is to say, rather than using strcpy(),
strcat(), strlen(), strcmp() or other such functions, you should use
memcmp(), memset() and memcmp() functions. (This is despite the
irony that all these functions are declared in string.h.) The size of
any array in C is implicit (usually by declaration, or some kind of
direct semantic intention) so there is no analogy to strlen() -- you
just have to somehow know the length of the array in your code.

But if you are intent on wanting to be able to treat an array of bytes
as either a string or a block of memory (because you want to insert,
scan and compare like a string, but still carry non-ASCII payloads
which have other semantic meaning), then of course you can use or
create a library of routines that performs this using some kind of
implicitely length delimited scheme. I have written up such a library
that is suitable for exactly this purpose here:

http://bstring.sf.net/

It has the bonus of outperfoming all the standard C library string
functionality, while remaining semantically compatible with either
string usage or memory block usage as you desire.
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.