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

Type conversion question

Hi, I have two NewBie questions:

1. Can a string in C# contain any byte values in between (including 0
Bytes)?
2. How to convert a byte array (byte[]) into a string and vic versa?

Thanks for Your help
Regards, Harald
Nov 17 '05 #1
2 1106
I forgot to mention, that I need the conversion to include Unicode <--->
ANSI translation
- Harald
"Harald" <Di*********@zdf.de> schrieb im Newsbeitrag
news:u5*************@TK2MSFTNGP15.phx.gbl...
Hi, I have two NewBie questions:

1. Can a string in C# contain any byte values in between (including 0
Bytes)?
2. How to convert a byte array (byte[]) into a string and vic versa?

Thanks for Your help
Regards, Harald

Nov 17 '05 #2
Hi Harald,

Take a look at the Encoding class.

You can translate a String to a byte[] and back to String like this:

String s = "Hello World";
byte[] bytes = MyEncoding.GetBytes(s);
String t = MyEncoding.GetString(bytes);

To translate to/from bytes you need to use the correct Encoding or
characters will get translated wrong as well as decoding a 16-bit Unicode
encoded byte array using a 7-bit ASCII encoding won't give much useful
information.

To get ANSI codepages use Encoding.GetEncoding and either provide a
numeric value or a string representation for the specific encoding you
need.

Encoding MyEncoding = Encoding.GetEncoding("ISO-8859-15"); // Nordic
bytes[] bytes = MyEncoding.GetBytes(UnicodeString);
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3

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

Similar topics

21
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
14
by: tweak | last post by:
I'm struggling with the concept of typecasting when setting up a TCP client. Here's a code snip (modified from W. Richard Stevens Unix Programming book) to demonstrate where I am struggling: ...
19
by: Randy Yates | last post by:
Consider the following code: #include "dsptypes.h" /* definitions */ #define VECTOR_LENGTH 64 /* local variables */
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
6
by: NM | last post by:
I am given an int and have to tell whether the bit representation of that int is a palindrome or not. Here is what I have come up with bool isIntPalindrome(int num) { unsigned int temp = num;...
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
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: 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
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.