473,385 Members | 1,907 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,385 software developers and data experts.

working with a hex byte array (which represents a string)

HaLo2FrEeEk
404 256MB
Hi, I'm working on a project wherein I need to pull out 256 bytes from a specified position in a file. The 256 bytes represent the title of the file. The thing is, it's null-padded, so there are 2 null hex bytes before the string starts, a single null byte between each letter in the title, and a variable number of nulls after the title ends. For example, if my title was "some random title", I would have 256 bytes with the first 2 being "00 00", then the title with a single "00" byte between each letter, and 221 "00" bytes after.

I need to know how to get rid of those null bytes. I've tried using:

ASCIIEncoding.ASCII.GetString(byte[] bytes);

And that would return a string "some random title" but when I do string.Length I get 256, because null bytes are non-printable when converted to ASCII, but they're still there.

Is there anything I can do? I can't really do any more work on my project until this gets resolved.
Jan 29 '10 #1
4 3768
RedSon
5,000 Expert 4TB
Your string is still 256 bytes in size. It's just a null terminated string. The text is valid but the size is the same. This is normal.

You could try trimming the string if you have a method that will do that.
Jan 29 '10 #2
tlhintoq
3,525 Expert 2GB
A null between each word? Not a 32 (space character)? That seems odd.
Are you sure the file contains just bytes and not was not written as text? Unicode characters are two-bytes per character, so a space would be two bytes 0x0020 (hex) 00-32 (decimal)

I think I would replace all the nulls with spaces.
Then do a trim.start and trim.end to clear all the leading and trailing spaces, leaving you with just the middle.
Jan 29 '10 #3
HaLo2FrEeEk
404 256MB
RedSon, I tried using the trim() method, but it didn't do anything at all. I don't want the string to be 256 bytes, I want it to be the length of the actual filename, without the null bytes. I need to use string.Length to get the string length so I can truncate it down to fit the area it's being printed in or add an ellipsis (...) to the end.

And tlhintoq, since the actual filename has the null bytes between each character, using trim would be useless anyway since I would still have a string that is (string.Length + (string.Length - 1)) long. A 15 character string will have 14 null bytes if there is 1 between each character. I need to remove those null bytes. It's not a space, it's a "00" hex byte. Here is an example, the title in this example is "Prepare to Drop Premium Theme", the Hex for that is:

Expand|Select|Wrap|Line Numbers
  1. 00 00 50 00 72 00 65 00 70 00 61 00 72 00 65 00 20 00 74 00
  2. 6F 00 20 00 44 00 72 00 6F 00 70 00 20 00 50 00 72 00 65 00
  3. 6D 00 69 00 75 00 6D 00 20 00 54 00 68 00 65 00 6D 00 65 00
  4. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  5. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  6. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  7. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  8. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  9. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  10. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  11. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  12. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  13. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
and the ASCII is:

Expand|Select|Wrap|Line Numbers
  1. ..P.r.e.p.a.r.e. .t.o. .D.r.o.p. .P.r.e.
  2. m.i.u.m. .T.h.e.m.e.....................
  3. ........................................
  4. ........................................
  5. ........................................
  6. ........................................
  7. ................
The dots represent non-printable ASCII characters, the 00 bytes.

I did manage to throw something together, a method that works, but I feel like there's got to be a better way. Here's my method:

Expand|Select|Wrap|Line Numbers
  1. private string getThemeName()
  2.         {
  3.             string themeName;
  4.             br.BaseStream.Position = 0x410;
  5.             byte[] nameBytesNull = br.ReadBytes(256);
  6.             List<byte> nameBytesList = new List<byte>();
  7.             foreach (byte single in nameBytesNull)
  8.             {
  9.                 if (single.ToString() != "0")
  10.                 {
  11.                     nameBytesList.Add(single);
  12.                 }
  13.             }
  14.             byte[] nameBytes = new byte[nameBytesList.Count];
  15.             nameBytesList.CopyTo(nameBytes);
  16.             themeName = ASCIIEncoding.ASCII.GetString(nameBytes);
  17.             return themeName;
  18.         }
Basically I'm reading the 256 bytes that I need into a byte array, then I iterate through each byte in that array and ask if it's ToString() value is 0, if it isn't then I add it to the byte List. Then I create another byte array based on the length of the byte List, and copy that byte List to that byte array. THEN I use the ASCIIEncoding GetString() method to convert the resulting byte array to it's ASCII string value and return it.

This works, but like I said, I feel like there must be a simpler way. Any ideas?
Jan 29 '10 #4
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. 00 00 50 00 72 00 65 00 70 00 61 00 72 00 65 00 20 00 74 00
This is exactly as I mentioned: unicode where two bytes represent a single character.

This is 1 null at the beginning
0050 is the two byte unicode of 'P'
0072 is the two byte unicode of 'r'
0065 is ... e
0070 is p
0061 is a
0072 is r
0065 is e
0020 is a space
0074 is a t

Expand|Select|Wrap|Line Numbers
  1.  byte[] nameBytesNull = br.ReadBytes(256);
Instead of reading bytes into a byte[] you should try reading lines into a string[]
Jan 29 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jarrod Sharp | last post by:
Using the VB6 Winsock control in VB.NET the GetData(byRef Data as Object) method of the control returns an Object. I have used this code that works but am not familiar with .NET...
1
by: Paul W | last post by:
How do I do this with: Option Strict On? I know of the function System.Text.ASCIIEncoding.GetChars, but that takes a Byte array not a DataRow. So with Option Strict On, how do I take the byte...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
1
by: ZorpiedoMan | last post by:
There MUST be an easier way... I have a byte array I want to convert to a string. I do NOT want to have characters above hex7h converted to a question mark (that is what...
2
by: Jaime Stuardo | last post by:
Hi all... I'm trying to retrieve a SQLXML query using VB.NET. When I programmed in VB 6.0, I used Stream object to accomplish this which was trivial. I cannot do the same thing in VB.NET. Here...
2
by: O.B. | last post by:
I have a structure named EntityState with an explicit layout. The following two operations exist within the class to return a byte array representing the current object. Upon executing them each a...
10
by: Sayudh27 | last post by:
Hello all, I used multicasting and received compressed data packets over the network.I decompressed the packets at a specified memory block 'in'(declared as lzo_byte *in)....Now,i...
1
by: Jo | last post by:
TIA for the help!
2
by: TaeMike | last post by:
Hello, I have some varchar(2000) fields in my tables, and they have a lot of "weird" characters in them including line break and carriage returns, etc. When I do a select, I see the entire string of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...

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.