-
April 12th, 2011, 11:31 AM
#1
[RESOLVED] String[] to Byte[] index by index please
Okay, I have a text box for input, and from this text box I need to take the data and parse it out to bytes and send it to a controller card for processing. The input is temperature and should not be below 100 (this is not important right now). My main problem is I need byte[i] = string[i]; exactly. Not its byte value for the char that is represented. if string[i] = 2 I need byte[i] to = 2.
Here is the code I have so far. I get values but not what I want, and it overwrites my byte and only fills 1 array slot.
byte[] gen = new byte[5];
char[] holder = setpointBoxQS.Text.ToCharArray();
string[] conv = new string[holder.Length];
for (int i = 0; i < holder.Length; i++)
{
conv[i] = holder[i].ToString();
}
if (holder.Length > 2)
{
for (int i = 0; i <= holder.Length; i++)
{
gen = Encoding.UTF8.GetBytes(conv[i]);
}
}
Thanks for any help
-
April 12th, 2011, 11:51 AM
#2
Re: String[] to Byte[] index by index please
I got it working for what I want it to do, but I cant help feel there is an easier way to do this.
byte[] gen = new byte[5];
char[] holder = setpointBoxQS.Text.ToCharArray();
string[] conv = new string[holder.Length];
int[] iconv = new int[holder.Length];
for (int i = 0; i < holder.Length; i++)
{
conv[i] = holder[i].ToString();
}
if (holder.Length > 2)
{
for (int i = 0; i < holder.Length; i++)
{
iconv[i] = Convert.ToInt32(conv[i]);
gen[i] = (byte)iconv[i];
}
}
-
April 12th, 2011, 11:58 AM
#3
Re: String[] to Byte[] index by index please
This is what I came up with but I'm unhappy with all the conversions going on
Code:
const int GEN_SIZE = 5;
string text = setpointBoxQS.Text;
byte[] gen = new byte[GEN_SIZE];
char[] holder = text.ToCharArray();
for (int i = 0; i < holder.Length; i++)
{
//Process a full gen array
if (i % GEN_SIZE == 0 &&
i != 0)
{
//Our gen array is complete
//*** Do something with it
}
int byteValue = Convert.ToInt32(Convert.ToString(holder[i]));
gen[i % GEN_SIZE] = (byte)byteValue;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|