simple casting question

  • Thread starter Thread starter DBC User
  • Start date Start date
D

DBC User

I have a value numberic 1 and I want to save it as 0x01 in the
character field instead of 1 in Ascii 31. How can I achive this?
 
DBC said:
I have a value numberic 1 and I want to save it as 0x01 in the
character field instead of 1 in Ascii 31. How can I achive this?

Can't you just say

char x = (char)1;

?
 
For some reason it is coming back as 31. May be I am looking at a wrong
location. I will try again.
 
DBC said:
For some reason it is coming back as 31. May be I am looking at a wrong
location. I will try again.

If you're trying to make the value of a char 0x01, then

char a = 1;

should do the trick.
 
DBC User said:
I have a value numberic 1 and I want to save it as 0x01 in the
character field instead of 1 in Ascii 31. How can I achive this?

If you want a string containing the character with code-point 1, then you
can simply cast your int to type char and assign that to the text box:

myTextBox.Text = ((char)myInt).ToString();

If what you want is the text box to contain the string "0x01", then you're
talking about a conversion, not casting.

myTextBox.Text = string.Format("0x{0:x}",myInt);

-cd
 
Carl,
I want to store the value in a char field that will be later used for
data transmission. I guess I can try the string format with (0:x) and
then convert it back to char.
 
DBC said:
Carl,
I want to store the value in a char field that will be later used for
data transmission. I guess I can try the string format with (0:x) and
then convert it back to char.

Well it didn't work either. It is bringing back 31. Any ideas?
 
I have no idea why. The following code prints "1" to the console:

namespace CastTest
{
class Program
{
static void Main(string[] args)
{
char c = (char)1;
System.Console.WriteLine((int)c);
}
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
 
DBC User said:
Well it didn't work either. It is bringing back 31. Any ideas?

It's difficult to understand exactly what you mean.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Write the program as best you can, and explain how the output differs
from what you want it to be.
 
Dustin said:
I have no idea why. The following code prints "1" to the console:

namespace CastTest
{
class Program
{
static void Main(string[] args)
{
char c = (char)1;
System.Console.WriteLine((int)c);
}
}
}

Of course it does.

char c = (char)1;

puts the _value_ 1 in the character variable. It is not the character
'1', but whatever character has ASCII value 1.

Then, when you cast it back to int, you of course get 1.

If you want to store the character '1' in the variable and then print
out its int (ASCII ordinal value), then you do this:

char c = '1';
System.Console.WriteLine((int)c);

This will print 31.

* For the purists, I know that char doesn't store ASCII, it stores
Unicode, but I'm an old dog and I figure that more people will
understand what ASCII is. :-)
 
Of course it does.
char c = (char)1;

puts the _value_ 1 in the character variable. It is not the character
'1', but whatever character has ASCII value 1.

Then, when you cast it back to int, you of course get 1.

If you want to store the character '1' in the variable and then print
out its int (ASCII ordinal value), then you do this:

char c = '1';
System.Console.WriteLine((int)c);
This will print 31.

* For the purists, I know that char doesn't store ASCII, it stores
Unicode, but I'm an old dog and I figure that more people will
understand what ASCII is. :-)

Sure, but that doesn't seem to be the original question:

"I have a value numberic 1 and I want to save it as 0x01 in the character
field instead of 1 in Ascii 31. How can I achive this?"

Best Regards,
Dustin Campbell
Developer Express Inc
 
Of course it does.
char c = (char)1;

puts the _value_ 1 in the character variable. It is not the character
'1', but whatever character has ASCII value 1.

Then, when you cast it back to int, you of course get 1.

If you want to store the character '1' in the variable and then print
out its int (ASCII ordinal value), then you do this:

char c = '1';
System.Console.WriteLine((int)c);
This will print 31.

You might want to test this before posting. It prints 49. 31 is *not* the
ASCII value of '1'.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
DBC User said:
For some reason it is coming back as 31. May be I am looking at a wrong
location. I will try again.

Surely you mean 0x31, the ASCII value that corresponds to the character for
the numeral "1". Decimal 31 is not a printable character.

In any case, the line of code posted by Bruce sets the numeric value of the
variable x to 1, which is what SEEMS to be what you're looking for. If you
believe it to be doing otherwise, you need to follow the steps in Jon's post
and give us some actual code that illustrates what you are doing and why you
think it's not doing what you want it to.

If I had to guess, I'd suspect that when you think you are simply checking
the value of the data in the "char" variable, you are actually somehow
converting it to a string, which of course causes the numeric value of 1 to
be translated into the ASCII value for the numeral "1" (0x31).

I will also point out that it may very well be that you don't understand the
"char" datatype, given that you wrote you intend to use this for "data
transmission". The "char" type represents a Unicode character, which is two
bytes. If you use the "char" type to store byte data you're sending, you
may well be doubling the amount of data being transmitted. It may be that
the C# "byte" or "sbyte" types actually make more sense for your use.

It's hard to say anything specific, because as Jon's pointed out, your
question isn't very clear.

Pete
 
You might want to test this before posting. It prints 49. 31 is *not*
the ASCII value of '1'.

0x31 is.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Dustin said:
Sure, but that doesn't seem to be the original question:

"I have a value numberic 1 and I want to save it as 0x01 in the character
field instead of 1 in Ascii 31. How can I achive this?"

Aargh. I'm sorry. I'm getting dislexic in my old age. I read your post
as, "I have no idea why this prints 1 to the console..." :-(
 
Aargh. I'm sorry. I'm getting dislexic in my old age. I read your post
as, "I have no idea why this prints 1 to the console..." :-(

lol

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Dustin Campbell said:

For what it's worth, that was one of the interpretations I had of your post
as well. Since you didn't quote anything, it was difficult to know what "I
have no idea why" referred to. At first glance, it seems as though to be
referring to the code you yourself posted.

I think the ensuing confusion was predictable. Not quite Abbott and
Costello, but close. :)
 
I think the ensuing confusion was predictable. Not quite Abbott and
Costello, but close. :)

Dude, I thought it was totally Abbott and Costello. My apologies for the
confusion.

Best Regards,
Dustin Campbell
Developer Express Inc
 

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

Back
Top