473,785 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string / char[] question

Hello,

This might be a rather basic question, but I've tried a few things and
I can't really find a solution as elegant as what I'd like for this
problem. The situation is this - I have a file that's written to disk
in a binary format. Basically it's a bunch of records, one after the
other, where each record has the following format : 18 bytes, 6 bytes
which are all zero, 4 bytes which represent a UInt32. The first 18
bytes of each record are actually a non-unicode string with maximum
length 18. If my string is "hello", the bytes are 'h', 'e', 'l', 'l',
'o', followed by 13 zeros. If my string is abcdefghijklmno pqr, then
all 18 bytes of this field are used.

So now that I've explained the structure, the problem I'm encountering
is when I want to read this string field. I'm using a
System.IO.Binar yReader to read the file. I created it with
Encoding.ASCII and then called getChars(18), which returns me a char[]
that is 18 characters long. But then how to get this into a
System.String? I tried the string constructor that takes a char[] as a
constructor parameter, but it doesn't seem acknowledge the nulls that
are padded into the end of the array, and even if it did it wouldn't
matter since I need to handle the case where all 18 chars contain a
value. The code I'm using now is as follows:

char[] NameChars = Reader.ReadChar s(18);
int i = 0;
for (i = 0; i < 18; ++i)
if (NameChars[i] == '\0') break;
string Name = new string(NameChar s, 0, i);

Reader.ReadByte s(6); //Skip the padding.

System.UInt32 Code = Reader.ReadUInt 32();

That loop I coded in there just seems "bad", and I'd much prefer if
there was a way to accomplish the same thing using only standard
framework calls.

On a related but slightly different topic, is it correct behavior of
the string::string( char[]) constructor that it simply ignores any nulls
embedded in the array, and creates a string with embedded nulls? I
wasn't aware that .NET strings even supported embedded nulls at all,
although this could be due to my lack of experience with .NET

Thanks

Jun 2 '06 #1
3 1931
I think it's normal behaviour.

Because in C/C++, strings are usually null terminated, it's understandable
that the string constrctor see "null" character as the end of string marker.

"Zach" <di***********@ gmail.com>
???????:11***** *************** **@h76g2000cwa. googlegroups.co m...
Hello,

This might be a rather basic question, but I've tried a few things and
I can't really find a solution as elegant as what I'd like for this
problem. The situation is this - I have a file that's written to disk
in a binary format. Basically it's a bunch of records, one after the
other, where each record has the following format : 18 bytes, 6 bytes
which are all zero, 4 bytes which represent a UInt32. The first 18
bytes of each record are actually a non-unicode string with maximum
length 18. If my string is "hello", the bytes are 'h', 'e', 'l', 'l',
'o', followed by 13 zeros. If my string is abcdefghijklmno pqr, then
all 18 bytes of this field are used.

So now that I've explained the structure, the problem I'm encountering
is when I want to read this string field. I'm using a
System.IO.Binar yReader to read the file. I created it with
Encoding.ASCII and then called getChars(18), which returns me a char[]
that is 18 characters long. But then how to get this into a
System.String? I tried the string constructor that takes a char[] as a
constructor parameter, but it doesn't seem acknowledge the nulls that
are padded into the end of the array, and even if it did it wouldn't
matter since I need to handle the case where all 18 chars contain a
value. The code I'm using now is as follows:

char[] NameChars = Reader.ReadChar s(18);
int i = 0;
for (i = 0; i < 18; ++i)
if (NameChars[i] == '\0') break;
string Name = new string(NameChar s, 0, i);

Reader.ReadByte s(6); //Skip the padding.

System.UInt32 Code = Reader.ReadUInt 32();

That loop I coded in there just seems "bad", and I'd much prefer if
there was a way to accomplish the same thing using only standard
framework calls.

On a related but slightly different topic, is it correct behavior of
the string::string( char[]) constructor that it simply ignores any nulls
embedded in the array, and creates a string with embedded nulls? I
wasn't aware that .NET strings even supported embedded nulls at all,
although this could be due to my lack of experience with .NET

Thanks

Jun 2 '06 #2
Actually, it seems to me that string constructor does NOT see null
character as end of string marker. Example:

char[] c = new char {'a', 'a', 'a', 'a', 'a', '\0', '\0'}
string s = new string(c);

This code creates a string of length 7. Shouldn't it create a string
of length 5?

Lau Lei Cheong wrote:
I think it's normal behaviour.

Because in C/C++, strings are usually null terminated, it's understandable
that the string constrctor see "null" character as the end of string marker.

"Zach" <di***********@ gmail.com>
???????:11***** *************** **@h76g2000cwa. googlegroups.co m...
Hello,

This might be a rather basic question, but I've tried a few things and
I can't really find a solution as elegant as what I'd like for this
problem. The situation is this - I have a file that's written to disk
in a binary format. Basically it's a bunch of records, one after the
other, where each record has the following format : 18 bytes, 6 bytes
which are all zero, 4 bytes which represent a UInt32. The first 18
bytes of each record are actually a non-unicode string with maximum
length 18. If my string is "hello", the bytes are 'h', 'e', 'l', 'l',
'o', followed by 13 zeros. If my string is abcdefghijklmno pqr, then
all 18 bytes of this field are used.

So now that I've explained the structure, the problem I'm encountering
is when I want to read this string field. I'm using a
System.IO.Binar yReader to read the file. I created it with
Encoding.ASCII and then called getChars(18), which returns me a char[]
that is 18 characters long. But then how to get this into a
System.String? I tried the string constructor that takes a char[] as a
constructor parameter, but it doesn't seem acknowledge the nulls that
are padded into the end of the array, and even if it did it wouldn't
matter since I need to handle the case where all 18 chars contain a
value. The code I'm using now is as follows:

char[] NameChars = Reader.ReadChar s(18);
int i = 0;
for (i = 0; i < 18; ++i)
if (NameChars[i] == '\0') break;
string Name = new string(NameChar s, 0, i);

Reader.ReadByte s(6); //Skip the padding.

System.UInt32 Code = Reader.ReadUInt 32();

That loop I coded in there just seems "bad", and I'd much prefer if
there was a way to accomplish the same thing using only standard
framework calls.

On a related but slightly different topic, is it correct behavior of
the string::string( char[]) constructor that it simply ignores any nulls
embedded in the array, and creates a string with embedded nulls? I
wasn't aware that .NET strings even supported embedded nulls at all,
although this could be due to my lack of experience with .NET

Thanks


Jun 2 '06 #3
So now that I've explained the structure, the problem I'm encountering
is when I want to read this string field. I'm using a
System.IO.Bina ryReader to read the file. I created it with
Encoding.ASC II and then called getChars(18), which returns me a char[]
that is 18 characters long. But then how to get this into a
System.Strin g?
The easist way would be to call Encoding.GetStr ing direcly and skip
the GetChars call.

On a related but slightly different topic, is it correct behavior of
the string::string( char[]) constructor that it simply ignores any nulls
embedded in the array, and creates a string with embedded nulls?


Yes, embedded nulls are allowed in managed strings since the length is
stored separately. If you don't want them, you can remove trailing
nulls with String.TrimEnd( '\0').
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

74
5090
by: Michael | last post by:
As if we needed another string reversal question. I have a problem with the following code, that I believe should work. int StringReverse(char* psz) { char *p = psz; char *q = psz + strlen(psz) - 1; while (p < q) {
7
10629
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using namespace std; : : vector<string>* getTyphoon()
22
17204
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
5
2925
by: Alan Silver | last post by:
Hello, <dumb question> I am trying to do something similar to the following... string str = "Hello"; char c = (char)str.Substring(2,1); but the compiler complains that it cannot convert a string to a char.
7
7603
by: Eric | last post by:
Hi All, I need to XOR two same-length Strings against each other. I'm assuming that, in order to do so, I'll need to convert each String to a BitArray. Thus, my question is this: is there an easy way to convert a String to a BitArray (and back again)? I explained the ultimate goal (XORing two Strings) so that, if anyone has a better idea of how to go about this they may (hopefully) bring that up...?
11
17654
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
7
373
by: junw2000 | last post by:
In the following code: std::string s1; s1 = "ABCDE"; std::cout<<s1<<std::endl; s1 = '\0'; //LINE1 std::cout<<s1<<std::endl; //LINE2 At LINE1, I add a NULL terminator at s1, so s1 should end at s1, right?
17
4709
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
5
2899
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to memory holding the string and the string might very well be held in read only memory. However, I am sure that I read somewhere that the declaration char a = "string literal", even though a is an array (and I understand the differences between...
0
10341
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
9954
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...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
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...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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.