[RESOLVED] [Code proposal] Convert byte array in ASCII to int
Hello,
This converts ASCII data to the equivalent int as if we had converted the string into the meaning int.
Comments and feedback appreciated, thanks !
Code:
public int iByteArrayASCIIToInt(byte[] bTable, int iStart, int iEnd)
{
int retval;
int i;
int iNumberOfElements;
iNumberOfElements = iEnd - iStart;
retval = 0;
for (i = 0; i <= iNumberOfElements; i++)
{
retval += (bTable[iStart + i] - 0x30) * (int)Math.Pow(10, iNumberOfElements - i);
}
return retval;
}
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Re: [Code proposal] Convert byte array in ASCII to int
Do believe we could keep these two related.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Re: [Code proposal] Convert byte array in ASCII to int
Thanks for your comments
What I was looking for is for that :
1) I have a file like this
"F662A4"
Means that in hex the file is like this :
00000000h: 46 36 36 32 41 34 ; F662A4
2) What i wanted is a byte[3] to look like this
byte[0] = F6
byte[1] = 62
byte[2] = A4
This is based on the MSDN article showing how to do it for int (see link), but I verified it works for byte as well.
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Bookmarks