Converting ascii to string

J

Jed Fletcher

up = textBox1.Text;
string ep = "";

foreach (char c in up)
{
ep += (System.Convert.ToInt16(c)).ToString();
}
textBox2.Text = ep;
label1.Text = ep.Length.ToString();

This gives me 575056706569485365504848574749484750 from the string
2009/10/2928FAE05A
Now is there a way to convert back to the 2009/10/2928FAE05A string.

Thanks in advance
 
F

Family Tree Mike

Jed Fletcher said:
up = textBox1.Text;
string ep = "";

foreach (char c in up)
{
ep += (System.Convert.ToInt16(c)).ToString();
}
textBox2.Text = ep;
label1.Text = ep.Length.ToString();

This gives me 575056706569485365504848574749484750 from the string
2009/10/2928FAE05A
Now is there a way to convert back to the 2009/10/2928FAE05A string.

Thanks in advance
.

In general, the answer would be no, since the string you get out would not
have an N-to-1 relationship to the characters in the input string. You are
lucky in this case though, as the characters all got sent to two digits each.
Split the output string into pairs of digits and reverse the process with
Convert.ToChar().
 
P

Peter Duniho

Jed said:
up = textBox1.Text;
string ep = "";

foreach (char c in up)
{
ep += (System.Convert.ToInt16(c)).ToString();
}
textBox2.Text = ep;
label1.Text = ep.Length.ToString();

This gives me 575056706569485365504848574749484750 from the string
2009/10/2928FAE05A
Now is there a way to convert back to the 2009/10/2928FAE05A string.

In addition to Mike's response, I think it's important to understand
some things:

-- The code you posted does not convert the original string to
ASCII in the first place. It doesn't even convert the original string
to a text representation of ASCII.

-- It's unclear from your post what value there even would be in
representing a string as hexadecimal values of the character codes of
the string itself. If you can store a string in the first place, you
might as well just store the string you want. If you have to store
bytes, then your code example isn't relevant anyway.

If you can explain exactly what it is you're trying to accomplish, you
might get a better, more useful answer than what you've received so far.

Pete
 
C

Cdudej

In addition to Mike's response, I think it's important to understand
some things:

     -- The code you posted does not convert the original string to
ASCII in the first place.  It doesn't even convert the original string
to a text representation of ASCII.

     -- It's unclear from your post what value there even would be in
representing a string as hexadecimal values of the character codes of
the string itself.  If you can store a string in the first place, you
might as well just store the string you want.  If you have to store
bytes, then your code example isn't relevant anyway.

If you can explain exactly what it is you're trying to accomplish, you
might get a better, more useful answer than what you've received so far.

Pete

Sorry i dont know if ascii is the right word. What i really need is to
convert 2009/10/2928FAE05A to numerics then back to alphanumeric.
Thanks in advance
 
P

Peter Duniho

Cdudej said:
Sorry i dont know if ascii is the right word. What i really need is to
convert 2009/10/2928FAE05A to numerics then back to alphanumeric.

That still doesn't make sense. "2009/10/2928FAE05A" isn't
"alphanumeric" in the first place; it contains the '/' character, which
is neither an alphabetical character nor a numeric character. There'd
be no way to "convert...back to alphanumeric".

Also, you haven't defined "numerics". That is, do you need the string
"2009/10/2928FAE05A" represented a different way as another string? Or
do you really need it in a numeric format (e.g. byte)?

If you need an actual numeric format, then something like the
Encoding.GetBytes() method is appropriate. If you want a new string
representation, you need to provide more detail regarding the purpose of
that string representation and what requirements it has to meet.

Pete
 
R

Rick Lones

rossum said:
I assume that 'up' is a string.

Better to use StringBuilder here:

StringBuilder ep = new StringBuilder(up.Length * 2);

I get 504848574749484750575056706569485365 from "2009/10/2928FAE05A".
Your version appears to be split into two parts and the parts swapped:

575056706569485365 504848574749484750
9 2 8 F A E 0 5 A 2 0 0 9 / 1 0 / 2

In general no, you cannot tell if "57" is two characters: U+0005,
U+0007 or one character: U+0039. In the particular example you gave
the process is reversible provided you assume that all characters are
two digits.

It might help to know why you are doing this.

rossum

Rossum:
I also tried the posted code (before noticing this post) and agree that the
output is not as shown by the OP. I got your result. And frankly I don't
believe the part about text boxes either - this input is not a string anyone
would type into a text box.

OP:
To me this looks maybe like input records you are reading from a text file or
possibly some ASCII bytes coming throug a serial port. The first part of
"2009/10/2928FAE05A" surely looks like a date (Oct 29, 2009) formatted to a
string. The remaining 8 bytes look like the hex representation of some number
or numbers - but that could be anything from an ANSI float to a single Int32 to
a pair of UInt16 values to a mix of signed and unsigned bytes, etc. The
interpretation would have to be known by the parsing code - it cannot in general
simply be inferred.

So anyhow I am not quite sure why you are parsing these bytes as individual
numbers in the first place. Are you sure you wouldn't prefer, e.g., to convert
those first 10 bytes to a TimeDate type rather than to a string of concatenated
numbers?

In any case, the mismatch between your posted code and the result you claim to
get from it indicates that there is some context being left out here . . .

-rick-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top