473,386 Members | 1,821 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,386 software developers and data experts.

simple casting 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?

Oct 18 '06 #1
22 1479

DBC User wrote:
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;

?

Oct 18 '06 #2
For some reason it is coming back as 31. May be I am looking at a wrong
location. I will try again.
Bruce Wood wrote:
DBC User wrote:
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;

?
Oct 18 '06 #3

DBC User wrote:
For some reason it is coming back as 31. May be I am looking at a wrong
location. I will try again.
Bruce Wood wrote:
DBC User wrote:
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;

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

char a = 1;

should do the trick.

Oct 18 '06 #4

bwray...@gmail.com wrote:
DBC User wrote:
For some reason it is coming back as 31. May be I am looking at a wrong
location. I will try again.
Bruce Wood wrote:
DBC User wrote:
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;
>
?

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

char a = 1;

should do the trick.
Forget that, I was thinking in C.

Think, then type...

Oct 18 '06 #5
"DBC User" <db*****@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
>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

Oct 18 '06 #6
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.

Carl Daniel [VC++ MVP] wrote:
"DBC User" <db*****@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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
Oct 18 '06 #7

DBC User wrote:
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.

Carl Daniel [VC++ MVP] wrote:
"DBC User" <db*****@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
>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
Well it didn't work either. It is bringing back 31. Any ideas?

Oct 18 '06 #8
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.
Oct 18 '06 #9
DBC User <db*****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 18 '06 #10

Dustin Campbell wrote:
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. :-)

Oct 18 '06 #11
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.
Oct 18 '06 #12
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.
Oct 18 '06 #13
"DBC User" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Bruce Wood wrote:
>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.
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
Oct 18 '06 #14
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.
Oct 18 '06 #15

Dustin Campbell wrote:
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?"
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..." :-(

Oct 18 '06 #16
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.
Oct 18 '06 #17
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>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
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. :)
Oct 18 '06 #18
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.
Oct 18 '06 #19
Then you actually want to store it in a byte.
On Oct 18, 1:56 pm, "DBC User" <dbcu...@gmail.comwrote:
I want to store the value in a char field that will be later used for
data transmission.
Oct 18 '06 #20

ja**********@gmail.com wrote:
Then you actually want to store it in a byte.
On Oct 18, 1:56 pm, "DBC User" <dbcu...@gmail.comwrote:
I want to store the value in a char field that will be later used for
data transmission.
Well I missed out the conversation here. It looks like I can not store
the non printable value of 1 (not the character 1) in a character
field?

I can store the value 1 as binay 1 in byte field. Now is it possible to
move the value as it is to a character field??

Thanks.

Oct 18 '06 #21
"DBC User" <db*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Well I missed out the conversation here. It looks like I can not store
the non printable value of 1 (not the character 1) in a character
field?
Of course you can.

// character with value 1
char c = (char)1;

// string containing a single character with value 1
string s = ((char)1).ToString();

-cd
Oct 19 '06 #22

DBC User wrote:
ja**********@gmail.com wrote:
Then you actually want to store it in a byte.
On Oct 18, 1:56 pm, "DBC User" <dbcu...@gmail.comwrote:
I want to store the value in a char field that will be later used for
data transmission.

Well I missed out the conversation here. It looks like I can not store
the non printable value of 1 (not the character 1) in a character
field?

I can store the value 1 as binay 1 in byte field. Now is it possible to
move the value as it is to a character field??
Yes it is possible. Several posters have illustrated how to do it.

Evidently, there is something wrong with your program. Unless you post
the code in question, we can't help you fix it.

Oct 19 '06 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
3
by: Dariusz Tomon | last post by:
Hi I'm trying to retrieve data from database (MS Sql serv) from field of BIT type. I'm trying like this: while (myReader.Read()) { Int16 cos = (Int16)myReader.GetSqlInt16(1);
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
15
by: Rob | last post by:
In order to create a date in CCYY-MM-DD fashion, I used to be able to use the Format(date,"yyyy")&"-"&Format(month,"mm")... syntax... This no longer appears to work in vb.net. I know how I...
5
by: aaragon | last post by:
Hi everyone, I wrote a very simple function to try to understand the casting of variables in C++. The function is function foo() { std::vector<inttest(100); randomize(test); unsigned long...
8
by: scottc | last post by:
i'm stuck and i need a little direction. i'm only getting 2 error messages. i have 2 files: import java.util.ArrayList; import java.util.*; //author public class StudentTester { ...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.