PC Review


Reply
Thread Tools Rate Thread

how to cast byte to char

 
 
Daniel
Guest
Posts: n/a
 
      17th Jul 2008
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


 
Reply With Quote
 
 
 
 
David Wilkinson
Guest
Posts: n/a
 
      17th Jul 2008
Daniel wrote:
> 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?

--
David Wilkinson
Visual C++ MVP
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      17th Jul 2008
"Daniel" <(E-Mail Removed)> wrote in message
news:OCwug$(E-Mail Removed)...
> 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++

> byte b;


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.
--
Jim Langston


 
Reply With Quote
 
Daniel
Guest
Posts: n/a
 
      17th Jul 2008
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

"David Wilkinson" <no-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Daniel wrote:
>> 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?
>
> --
> David Wilkinson
> Visual C++ MVP



 
Reply With Quote
 
David Wilkinson
Guest
Posts: n/a
 
      17th Jul 2008
Daniel wrote:
> 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#.

--
David Wilkinson
Visual C++ MVP
 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      17th Jul 2008
Jim Langston wrote:
> "Daniel" <(E-Mail Removed)> wrote in message
> news:OCwug$(E-Mail Removed)...
>> 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++
>
>> byte b;

>
> 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.


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

>
> Thank you.



 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      17th Jul 2008
Daniel wrote:
> 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.

>
> did not work.
>
> Daniel



 
Reply With Quote
 
David Wilkinson
Guest
Posts: n/a
 
      17th Jul 2008
Ben Voigt [C++ MVP] wrote:
> 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.

--
David Wilkinson
Visual C++ MVP
 
Reply With Quote
 
David Wilkinson
Guest
Posts: n/a
 
      17th Jul 2008
Daniel wrote:
> 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.

--
David Wilkinson
Visual C++ MVP
 
Reply With Quote
 
Daniel
Guest
Posts: n/a
 
      17th Jul 2008
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

"David Wilkinson" <no-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Daniel wrote:
>> 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.
>
> --
> David Wilkinson
> Visual C++ MVP



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Char Char Char1 Char Char Char,Char Char Char1 Char borkena Microsoft Word Document Management 2 8th May 2008 04:36 PM
Ever seen "Char Char Char Char Char Char1 Char Char Ch" style? =?Utf-8?B?TWFyaWx5bg==?= Microsoft Word Document Management 2 4th Oct 2006 04:47 PM
Char Char Char Char =?Utf-8?B?UmFjaGVs?= Microsoft Word Document Management 2 8th Jun 2006 09:52 AM
Char Char Char Char =?Utf-8?B?UmFjaGVs?= Microsoft Word Document Management 0 7th Jun 2006 02:51 PM
Formatting changed to Char Char Char Char... =?Utf-8?B?TWVsaXNzYQ==?= Microsoft Word Document Management 10 14th Apr 2006 07:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:47 AM.