473,406 Members | 2,220 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,406 software developers and data experts.

using '\0' in char string

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
7 5555
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
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
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


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas Müller | last post by:
hi @all, I have a char* string array and i want to fill it up in a while loop: while(i<3){ length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer); ... } after this the array should...
3
by: Vishal Ladha | last post by:
Hi ! I have been experimenting with char * for a while now. I have two pieces of code : Code1 : ===== char *ptr = "hello";
6
by: Sona | last post by:
Hi, What's the advantage/disadvantage of using a "const char*" over a "char*" ? I read some place that char* are string literals that on some machines are stored in a read only memory and cannot...
3
by: Jim Lewis | last post by:
I have read several things that state accessing a Web Service through a Query String should work. However, when I try to execute http://localhost/webservice1/service1.asmx/HelloWorld I get the...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
14
by: gustavo | last post by:
I was looking at the Sendmail's source code, and i've got confused about this kind of initialization: ------------------------ struct prival PrivacyValues = { { "public", PRIV_PUBLIC }, {...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
1
by: niteshpanchal | last post by:
Hi, How to assign dynamic array using pointers for string.
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.