473,657 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5579
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***********@p hysik.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(filepoi nter, "%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,0x0 1,'\0',0xFF},40 0};

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******@myrapi dsys.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
3104
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 look like this:
3
2877
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
4749
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 be modified... does that apply on const char*? thanks Sona
3
7662
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 error below. Can I access a Web Service through a Query Sting. I need to send XML to a Flash movie using a Web Service. Thank You, Jim Lewis Server Error in '/WebService1' Application.
33
3663
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 has prepared for those who love him" 1 Cor 2:9
14
2401
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 }, { "needmailhelo", PRIV_NEEDMAILHELO }, { "needexpnhelo", PRIV_NEEDEXPNHELO }, { "needvrfyhelo", PRIV_NEEDVRFYHELO },
6
8559
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 circumstances(XML file source) and with exactly the same value it gets it perfectly correct all the time. These are the results I got, XML is always correct, CSV are only incorrect for some of the values (above about 0.01) but always gives the...
33
15534
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 fact, they may not even represent complete characters. At this point I think of text as simple binary blobs. What charset, character encoding and termination they use should not be exposed in the interface used to operate on them. But now I have...
1
1642
by: niteshpanchal | last post by:
Hi, How to assign dynamic array using pointers for string.
13
3731
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 like to remove the weekday information that is "Wed" here, and I also would like to replace the spaces char by "_" and also remove the "\n" char. I didn't know how to truncate the string from beginning or replace some chars in a string with another...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.