473,404 Members | 2,137 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,404 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 4897
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

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

Similar topics

4
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
3
by: craig.wagner | last post by:
I've got an application that is calling a command-line executable. The command-line tool uses stdin and stdout as its interface, and it expects a binary stream in and sends a binary stream out. ...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
7
by: Joris De Groote | last post by:
Hi, I have a 1 dimensional table byte with a number af characters in ASCII code. How do I convert those ASCII codes to real letters? Thanks
3
by: Zach | last post by:
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...
4
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. ...
0
by: Michael Postmann | last post by:
Hello! I have Oracle 10g using AL32UTF8 as internal charset. I have a documents table where uploaded files (I'm using modplsql web-gateway (release 8i)) are stored in a blob-column. I upload a...
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
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: 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:
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,...
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
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,...

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.