472,135 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,135 software developers and data experts.

how to convert a string of hex chars to ascii, including nulls

28
I want to convert a string of hex characters (2 hex chars = 1 byte), to ASCII. Hex chars include zeros (0x00) as well, which I want to include in ASCII string.

hex string: 5000005355....
ASCII: P<null><null>SU...

I can do it and the string length also includes nulls but when I concatenate other string, it doesn't show as its part.


Expand|Select|Wrap|Line Numbers
  1. string HexValue = "500000535500";
  2. string StrValue = "";
  3. while (HexValue.Length > 0)
  4. {
  5.     StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
  6.     HexValue = HexValue.Substring(2, HexValue.Length - 2);
  7. }
  8. StrValue += "hello";
  9. textASCII.Text = StrValue;
  10. MessageBox.Show(StrValue.Length.ToString());
  11.  

Or .. is there some problem with the text field in which I am displaying the result?
Actually, I need to send the string containing nulls through socket.
Mar 12 '09 #1
12 4699
tlhintoq
3,525 Expert 2GB
Put a breakpoint in after the concatenate and check the actual value of your variable. I wouldn't be surprises if it was complete but displaying wrong. Strings often don't deal well with having nulls in them. Null termination is fairly standard so when a control reaches the null it may just stop looking.
Mar 12 '09 #2
Plater
7,872 Expert 4TB
Why would you convert it to a string if you were going to send it on a socket?
Mar 12 '09 #3
aatif
28
@tlhintoq
In the tooltip, it shows the string with nulls and "hello" at the end, but in 'quick watch' window, it shows only characters before first '\0' and no "hello"...
Mar 12 '09 #4
Plater
7,872 Expert 4TB
Well tlhintoq really WAS right, \0 is not a valid character in an ASCII string and is used for termination. Its just part of the language specs.
If you really want to see the whole string, look at it in hex
Mar 12 '09 #5
aatif
28
@Plater
I have to convert because I want it to be read on the receiver end as it is in hex at my end, if I send it in hex form, it shows up as numerics on the other end.

If I send 5000005355, it becomes 35303030303035333535, i-e '5' converted to ASCII (0x35), and so on ...

But I want to send 5000005355 as 5000005355, which is only possible by converting them in ASCII form, 'P' would be taken as 0x50, <null> as 0x00 and so on ...
Mar 12 '09 #6
Plater
7,872 Expert 4TB
You could convert them to bytes and send them as bytes.
Sockets use bytes to send the data, you would have to convert the string to bytes to send it anyway
Mar 12 '09 #7
aatif
28
@Plater
Convert the hex string to bytes or the ASCII? How?
Mar 12 '09 #8
Plater
7,872 Expert 4TB
so you have a literal string such as
string s = "5000005355" ?
How do you get this string?
I mean byte b= 0x50 is the first byte and
byte[] ba = new byte[]{0x50, 0x00, 0x00, 0x53, 0x55}; would be the byte array of it
Mar 12 '09 #9
aatif
28
@Plater
The hex string is created at runtime so I cannot assign byte array like this.
Mar 12 '09 #10
Plater
7,872 Expert 4TB
So someone types into a box "5000005355" or something?
And even if created at runtime, you can still parse into byte[]
Mar 12 '09 #11
vekipeki
229 Expert 100+
Check this link: http://www.thinksharp.org/hex-string...ray-converter/
Mar 12 '09 #12
aatif
28
@vekipeki
Thanks! ... to all for their quick response.
Mar 12 '09 #13

Post your reply

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

Similar topics

4 posts views Thread by David Lawson | last post: by
3 posts views Thread by craig.wagner | last post: by
4 posts views Thread by Jonathan Burd | last post: by
7 posts views Thread by MilanB | last post: by
7 posts views Thread by Joris De Groote | last post: by
3 posts views Thread by Zach | last post: by
4 posts views Thread by meendar | last post: by

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.