Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string to 20 char byte array

Status
Not open for further replies.

PeterOcean

Programmer
Mar 26, 2002
27
0
0
CA
I have a string value such as the following 881872241251009144651431267159882071681471608600
that I have to convert to a 20 character byte array and I am completely lost.

 
See System.Text.AsciiEncoding

using System;
using System.Text;

class ASCIIEncodingExample {
public static void Main() {
Byte[] bytes;
String chars = "ASCII Encoding Example";

ASCIIEncoding ascii = new ASCIIEncoding();

int byteCount = ascii.GetByteCount(chars.ToCharArray(), 6, 8);
bytes = new Byte[byteCount];
int bytesEncodedCount = ascii.GetBytes(chars, 6, 8, bytes, 0);

Console.WriteLine(
"{0} bytes used to encode string.", bytesEncodedCount
);

Console.Write("Encoded bytes: ");
foreach (Byte b in bytes) {
Console.Write("[{0}]", b);
}
Console.WriteLine();
}
}

[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections;

int main()
{
Byte bytes[];
String * chars = S"ASCII Encoding Example";

ASCIIEncoding* ascii = new ASCIIEncoding();

int byteCount = ascii -> GetByteCount(chars -> ToCharArray(), 6, 8);
bytes = new Byte[byteCount];
int bytesEncodedCount = ascii -> GetBytes(chars, 6, 8, bytes, 0);

Console::WriteLine(S" {0} bytes used to encode String*.", __box(bytesEncodedCount));

Console::Write(S"Encoded bytes: ");
IEnumerator* myEnum = bytes->GetEnumerator();
while (myEnum->MoveNext())
{
Byte b = *__try_cast<Byte __gc*>(myEnum->Current);
Console::Write(S"[{0}]", __box(b));
}

Console::WriteLine();
}



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top