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

how to cast byte to char

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
Jul 17 '08 #1
12 2110
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
Jul 17 '08 #2
"Daniel" <ne******@cableone.netwrote in message
news:OC**************@TK2MSFTNGP02.phx.gbl...
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
Jul 17 '08 #3
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******@effisols.comwrote in message
news:eL*************@TK2MSFTNGP06.phx.gbl...
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

Jul 17 '08 #4
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
Jul 17 '08 #5
Jim Langston wrote:
"Daniel" <ne******@cableone.netwrote in message
news:OC**************@TK2MSFTNGP02.phx.gbl...
>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.

Jul 17 '08 #6
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

Jul 17 '08 #7
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
Jul 17 '08 #8
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
Jul 17 '08 #9
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******@effisols.comwrote in message
news:e2*************@TK2MSFTNGP02.phx.gbl...
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

Jul 17 '08 #10
Daniel wrote:
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.

--
David Wilkinson
Visual C++ MVP
Jul 17 '08 #11
ok. Thanks for the advice. I'll have to consider that carefully.

Daniel

"David Wilkinson" <no******@effisols.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Daniel wrote:
>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.

--
David Wilkinson
Visual C++ MVP

Jul 17 '08 #12
On Jul 17, 7:58*am, "Daniel" <newso...@cableone.netwrote:
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);

Jul 18 '08 #13

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

Similar topics

0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
5
by: Dopey | last post by:
This is not homework. I'm trying to understand why this code doesn't output "$$$$$$$\0\n" -- all I get is "$)\n". (I admit I'm an idiot, OK?) #include <stdio.h> #include <stdlib.h> struct...
12
by: Martin | last post by:
Two questions relating to FAQ answer 12.42. (1) In the statement s.i16 |= (unsigned)(getc(fp) << 8); i16 is declared int. The reason for casting to (unsigned) is explained as guarding...
6
by: Tim | last post by:
I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to pass to a native function call in a bit of managed c++ code like this: Byte field __gc = dynamic_cast<Byte...
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
10
by: Allen | last post by:
I want to package an address to byte buffer. So I need to cast it to integer value. How to cast it? const char * ptr = 0x0013e328; int value = <cast(ptr); // use reinterpret_cast?
1
by: idlyatall | last post by:
Can we assume member variables reside in a class as they would in a structure? It is legitimate to assume the memory layout of the field of a structure. Can we assume the same for a class? e.g....
3
by: ist | last post by:
Hi, I am trying to get (and transfer over ASP.NET) some encrypted data from some MySQL fields. Since the data contains many unicode characters, I tried to get the data as a series of ASCII...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.