I have a dll with a method that reads from a pdf file.
It needs a byte array parameter.
I read from another pdf and i get a string (or a char array). Now i want to convert from string to byte array.
I tried Encoding.UTF7/8/ASCII/Unicode.getBytes(string) but they don't work properly (\n\r not converted)
I also tried to convert char by char in byte but i have also a perthousand ( %o ) that creates problem
Help!
Thx

convert help
Greg Dillon
If I were you, I wouldn't convert from bytes to string and back, just use bytes. Conversion of binary data with a particular encoding and back does not always work. Each encoding does not support all byte values (0-255) and may get translated into a "glyph".
But, if you don't care about that:
// load the bytes
byte[] bytes = { 65, 66, 66, 68, 69 };
// convert to string
String s = System.Text.Encoding.ASCII.GetString(bytes);
// modify the string
s = s.Substring(0, 2) + 'C' + s.Substring(3,2);
// convert back to bytes
bytes = System.Text.Encoding.ASCII.GetBytes(s);
ZeeMan48
A question: but if a char corresponds to 2 byte, the byte array must be double size char array
It doen's work...i have a % = 25hex and Buffer.BlockCopy transform it in 2 byte: 37 (correct) and 0 ...
HELP
clivees
// load the bytes
byte[] bytes = { 65, 66, 66, 68, 69 };
// convert to string
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(1252);
String s = encoding.GetString(bytes);
// modify the string
s = s.Substring(0, 2) + 'C' + s.Substring(3, 2);
// convert back to bytes
bytes = encoding.GetBytes(s);
Fish-Dude
Yes, it's a "magic" number. I don't like to use them, but there's no constants defined to make them more meaningful, as far as I can tell.
1252 is a codepage ID for the Western European (Windows) codepage, also known as ISO-8859-1. A codepage is basically a lookup table for supported characters.
arch82
I used ASCII strictly as an example, I could have chosen a better Encoding class.
ASCIIEncoding.Default should provide a good Encoding object to work with; but it will depend upon the current Window settings...
stevelam
Balaam
Are you looking for something like-
string s = "Karthik"; char[] chars = s.ToCharArray(); byte[] bytes = new byte[chars.Length]; Buffer.BlockCopy(chars, 0, bytes, 0, chars.Length);Not sure if this helps,but still...
Konstantin Gonikman
Unfortunately the ASCII will strictly truncate to 7bits, so any characters above 127 becomes ' '. You might get lucky by using the Default encoding which will adopt your default codepage. It's obsolete and dangerous, in view of internationalization and compatibility issues, but it's probably your best bet when handling files that were encoded without Unicode, or UTF's.
This will allow you to get strings out of your byte buffers, manipulate them and then go back to bytes, with the nice side effect that the number of characters will always match the number of bytes.
byte [] toBytes = ASCIIEncoding.Default.GetBytes ("a ");
string backToString = ASCIIEncoding.Default.GetString (toBytes);
HTH
--mc
glorfindel_i510
vice
Checking last solution... but i can't understand 1252...explain better
What's the difference with the previous solution