how to cast byte to char

D

Daniel

How do I change a byte variable so that I can add it's character value to
the end of a variable of type string, such that:

String^ s = "1234";
byte b;
b= 53;

s = s + b;

thus s = "12345".

I wasn't able to use a cast.

s = s + (char)b;

did not work.

Daniel
 
D

David Wilkinson

Daniel said:
How do I change a byte variable so that I can add it's character value to
the end of a variable of type string, such that:

String^ s = "1234";
byte b;
b= 53;

s = s + b;

thus s = "12345".

I wasn't able to use a cast.

s = s + (char)b;

did not work.

Daniel:

1. It would be good if you were to consider whether you are using standard C++
or C++/CLI (two *different* languages), and post accordingly.

2. It would also be good if you described what "did not work". What did you
expect, and what happened? Did the code fail to compile? Or link? Or run? Or to
give the result you expected?
 
J

Jim Langston

Daniel said:
How do I change a byte variable so that I can add it's character value to
the end of a variable of type string, such that:

String^ s = "1234";

c++ has no type String. Without a definition, type, struct or class that is
a syntax error. ^ is a syntax error. That is not C++

c++ has no type byte. Without a typedef or structure or class definition
that is a syntax error. That is not C++
b= 53;

s = s + b;

thus s = "12345".

I wasn't able to use a cast.

s = s + (char)b;

did not work.

Of course it didn't work, that's not legal C++ code. If you mean to ask
about some other language, please post in a newsgroup about that other
language.

Thank you.
 
D

Daniel

In the example that I gave, s=s+b resulted in s. The b did not get
concatenated. As for standard C++ or C++/CLI, I can't tell the difference.
I'm not that well versed in the technology to be able to tell you.

Daniel
 
D

David Wilkinson

Daniel said:
In the example that I gave, s=s+b resulted in s. The b did not get
concatenated. As for standard C++ or C++/CLI, I can't tell the difference.
I'm not that well versed in the technology to be able to tell you.

Daniel:

If you do not know whether you are using standard C++ or C++/CLI, and why, you
need to stop what you are doing and find out.

From the limited information that you present here, I would say there there is
a very high probability that you should be learning C#.
 
B

Ben Voigt [C++ MVP]

Jim said:
c++ has no type String. Without a definition, type, struct or class
that is a syntax error. ^ is a syntax error. That is not C++


c++ has no type byte. Without a typedef or structure or class
definition that is a syntax error. That is not C++


Of course it didn't work, that's not legal C++ code. If you mean to
ask about some other language, please post in a newsgroup about that
other language.

He did. microsoft.public.dotnet.languages.vc is the correct ng for C++/CLI.
 
B

Ben Voigt [C++ MVP]

Daniel said:
How do I change a byte variable so that I can add it's character
value to the end of a variable of type string, such that:

String^ s = "1234";
byte b;
b= 53;

s = s + b;

thus s = "12345".

I wasn't able to use a cast.

s = s + (char)b;

Try wchar_t instead, or System::Char.
 
D

David Wilkinson

Ben said:
He did. microsoft.public.dotnet.languages.vc is the correct ng for C++/CLI.

Ben:

Yes, but microsoft.public.vc.language is not.

In fact the OP appears unaware that C++ and C++/CLI are different languages. But
the solution is to learn the difference, not to cross-post.
 
D

David Wilkinson

Daniel said:
In the example that I gave, s=s+b resulted in s. The b did not get
concatenated. As for standard C++ or C++/CLI, I can't tell the difference.
I'm not that well versed in the technology to be able to tell you.

Daniel:

To answer your question, if I do

String^ s = L"1234";
byte b = 53;
s = s + b; // or s +(char)b

then I get "123453" (isn't this what happened for you?).

But if I do

String^ s = L"1234";
byte b = 53;
s = s + (wchar_t)b;

then I get "12345", which I think is what you wanted.

This is because System::String contains wide characters (wchar_t), not 8-bit
characters (char), and the + operator is overloaded for wchar_t.

Note that, although you can initialize System::String with "" string (8-bit
string), you should always use L"" (wide string) in C++/CLI.

C++/CLI is not an easy language, and you might well be better off learning C#.
C++/CLI is essentially standard C++ and C# combined together in one (often
confusing) language.
 
D

Daniel

That explanation on C++/CLI was helpful. I found example code that used the
^ notation after the type name in the MSDN help that came with Visual
Studio. I had never seen that notation before. I thought it was part of
..NET and not necessarily C#. Thanks for the explanation.

Daniel
 
D

David Wilkinson

Daniel said:
That explanation on C++/CLI was helpful. I found example code that used the
^ notation after the type name in the MSDN help that came with Visual
Studio. I had never seen that notation before. I thought it was part of
.NET and not necessarily C#. Thanks for the explanation.

Daniel:

I would really advise you to get a firm grip on what all these things mean. The
^ notation is not part of .NET (or of C#); it is a feature of the C++/CLI
language, which (unlike standard C++) can target the .NET framework. Not the
same thing.

There are three *different* languages:

C# (managed)
C++ (native/unmanaged)
C++/CLI (mixed)

Both C# and C++/CLI can target the .NET framework. However, Microsoft is no
longer promoting C++/CLI as a first class .NET language for GUI applications.
They only promote it for interfacing between managed and native code.

All new GUI development should be done in C# (or VB.NET). This is why you should
think carefully before investing a lot of time in C++/CLI.
 
J

Joe.M

How do I change a byte variable so that I can add it's character value to
the end of a variable of type string, such that:

String^ s = "1234";
byte b;
b= 53;

s = s + b;

thus s = "12345".

I wasn't able to use a cast.

s = s + (char)b;

did not work.

Daniel

I think you programming in C++/CLI, so u can try this:
String^ s = L"1234";
Byte b = 53;

s += gcnew String((wchar_t)b, 1);
 

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