[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;
}
Re: [Code proposal] Convert byte array in ASCII to int
Using the internal libraries, you can do it quicker:
Code:
string asciiString = System.Text.Encoding.ASCII.getString(bTable);
return Int32.Parse(asciiString);
Re: [Code proposal] Convert byte array in ASCII to int
[Code proposal] Convert Hex values in ASCII in byte
Hello again people, this time i had a file with hex values typped in ASCII but i needed them in byte array so I made this. Any feedback is appreciated ;)
Code:
private byte castASCIIByte(byte b1,byte b2)
{
byte retval;
retval = 0;
if ((b1 > 0x2F)&&(b1<0x3A))
{
retval =(byte) ((b1 - 0x30) * 16);
}
else if ((b1 > 0x40) && (b1 < 0x47))
{
retval = (byte)((b1 - 55) * 16);
}
else if ((b1 > 0x60) && (b1 < 0x67))
{
retval = (byte)((b1 - 87) * 16);
}
if ((b2 > 0x2F) && (b2 < 0x3A))
{
retval += (byte)(b2 - 0x30);
}
else if ((b2 > 0x40) && (b2 < 0x47))
{
retval += (byte)(b2 - 55);
}
else if ((b2 > 0x60) && (b2 < 0x67))
{
retval += (byte)(b2 - 87);
}
return retval;
}
Re: [Code proposal] Convert Hex values in ASCII in byte
If it is a file and you need to get it into a byte array the simple way is to just read it into a byte array.
create a filestream pointed to the file
create a binaryreader pointed to the file stream
using the binaryreader readallbytes
Re: [Code proposal] Convert byte array in ASCII to int
Do believe we could keep these two related.
Re: [Code proposal] Convert byte array in ASCII to int
Hi,
You can use the Convert Class and the BitConverter classes
Code:
int checkVal;
checkVal = Convert.ToInt16(String.Format("{0:F0}", pageByte), 16) ; //base 16
checkVal = Convert.ToInt16(String.Format("{0:F0}", lsb), 10); //base 10
Trace.WriteLine("Checkval = " + checkVal);
int checkSum;
checkSum = 256 - checkVal;
strcheckSum = BitConverter.ToString(BitConverter.GetBytes(checkSum));
Curt
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
Re: [Code proposal] Convert byte array in ASCII to int
You can parse them like this:
Code:
string hexString = "F4";
byte parsed = Byte.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
//Parsed now contains 0xF4 (=244)
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.
Re: [Code proposal] Convert byte array in ASCII to int
Yay thanks, helps me a lot ;)