473,473 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

checking of hextoascii code

26 New Member
Hi, i would like to know whether this code can be improved. because each time i convert extremely long strings, my exe hangs (does not respond).

Expand|Select|Wrap|Line Numbers
  1. private string HexString2Ascii(string HexString)
  2.         {
  3.             StringBuilder Sb = new StringBuilder();
  4.             for (int i = 0; i <= hexTextBox.Text.Length - 2; i += 2)
  5.             {
  6.                 Sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(HexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
  7.             }
  8.             return Sb.ToString();
  9.         }
  10.  
  11. private void hexToAsciiButton_Click_1(object sender, EventArgs e)
  12.         {
  13.             try
  14.             {
  15.                 asciiTextBox.Text = HexString2Ascii(hexTextBox.Text);
  16.             }
  17.  
  18.             catch
  19.             {
  20.                 MessageBox.Show("Invalid Input", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  21.             }
  22.         }
TIA
Nov 26 '08 #1
10 1595
anijos
52 New Member
Are you getting index out of bound error?
Nov 26 '08 #2
trixxy
26 New Member
no. i am able to convert long strings but not extremely long. the string that i want to convert contains about 2millions characters
Nov 26 '08 #3
anijos
52 New Member
what is the error message u r getting?
Nov 26 '08 #4
trixxy
26 New Member
there is no error message. the program just hangs. you need to open up the task manager and end task it to close it.
Nov 26 '08 #5
anijos
52 New Member
Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates.

The capacity of a StringBuilder is the maximum number of characters the instance can store at any given time, and is greater than or equal to the length of the string representation of the value of the instance. The capacity can be increased or decreased with the Capacity property or EnsureCapacity method

A StringBuilder can allocate more memory as needed to store characters when the value of an instance is enlarged, and the capacity is adjusted accordingly. The amount of memory allocated is implementation-specific, and ArgumentOutOfRangeException is thrown if the amount of memory required is greater than the maximum capacity.


Try StringBuilder Sb = new StringBuilder(2000000);

AniJos
Nov 26 '08 #6
trixxy
26 New Member
it still hangs. i tried lowering the value to 200,000 but it doesn't show the "ArgumentOutOfRangeException" error message
Nov 26 '08 #7
anijos
52 New Member
The maximum size of all reference type (like a string) instances is limited
by the CLR to 2GB, that means that a string can hold a maximum of ~1G
characters.

While it's possible to reach that limit when running on a 64 bit OS, you
will never be able to create such large strings (or arrays) on a 32 bit OS.
The reason is that you won't have that amount of "contiguous" address space
available to create the backing store (a char array) for the string.
The size of the largest contiguous memory space highly depends on how
modules are mapped (see: Win32 and framework DLL's base addresses) into the process address space. Some modules are laid-out in such a way that the
largest chunk becomes something like 950.000Kb, this before you even have
created a single object.


So try with smaller values till you find out what is the max capacity your system can afford.

AniJos
Nov 26 '08 #8
trixxy
26 New Member
i found out something. it actually doesn't hang. it just takes a long time to process.
any way to shorten the process?
Nov 27 '08 #9
balabaster
797 Recognized Expert Contributor
Would it be possible to parse the string as if it were a stream? Dump it into some form of streamwriter and pass it into a streamreader and parse it on the fly?

Just a thought - it would mean you wouldn't need to read the string all in one go. I'm not sure if it's a valid way of doing this or if there's a more efficient way of doing it, just a thought...
Nov 27 '08 #10
trixxy
26 New Member
i have no idea on how to use a streamwriter
Nov 27 '08 #11

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

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
0
by: Lucifer | last post by:
Hi I have some code for checking for cookies, that sets a cookie on page1 and checks for it on page2. and its based on the code by MS: ...
22
by: Qopit | last post by:
Hi there, I'm pretty new to Python and am trying to figure out how to get "will this code compile?"-like code checking. To me this is a pretty basic language/environment requirement, especially...
7
by: HIK | last post by:
Click once can be set up to pole the setup url if there is a newer version or not. I have a project which can only be deployed using a setup project. How can I create the same feature in my setup...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
16
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
7
by: polas | last post by:
Afternoon everyone. I have a quick question about standard C. Generally speaking, in my experience, whenever one accesses an array there is never any bounds checking done (either statically...
0
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,...
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
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
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...
1
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...
0
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...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.