Hi,
Everything is in the title I have an array in Byte[] which contains the ASCII code of a string but I have to give to my function a Byte[] that have Hex values of my string and not ASCII. How can I do that?
I have actually this code:
String abcd = "You got 1 voicemail";
Byte[] test = System.Text.ASCIIEncoding.UTF8.GetBytes(abcd);
I'm developping in C#.NET
[C#] Convert a Byte to Hexadecimal
7 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Simon66 pointsPosted Jan 27, 2006, 1:30 AM EST
Simon
sudheer panwar19 pointsPosted Jan 27, 2006, 2:14 AM EST
Hi,
try this , plz tell me if it fits ur req. private byte[] ConvertToHexArray(byte [] asciiData) { if(asciiData != null && asciiData.Length >0) { byte[] byteHexArray = new byte[asciiData.Length]; for(int i = 0; i<asciiData.Length ; i ++) byteHexArray[i]= Byte.Parse(asciiData[i].ToString("X2")); return byteHexArray; } return null; }sudheer panwar19 pointsPosted Jan 27, 2006, 2:14 AM EST
Hi,
try this , plz tell me if it fits ur req. private byte[] ConvertToHexArray(byte [] asciiData) { if(asciiData != null && asciiData.Length >0) { byte[] byteHexArray = new byte[asciiData.Length]; for(int i = 0; i<asciiData.Length ; i ++) byteHexArray[i]= Byte.Parse(asciiData[i].ToString("X2")); return byteHexArray; } return null; }akaii de Shangaii9 pointsPosted Jan 27, 2006, 3:10 AM EST
when it arrives at line:
byteHexArray[i] = Byte.Parse(asciiData[i].ToString("X2"));
This is the string I need to convert: "You got 1 voicemail"
akaii de Shangaii9 pointsPosted Jan 27, 2006, 4:23 AM EST
So if you know how I can do this it would help me a lot.
AnttiRank #9,295 · 208 pointsPosted Jan 27, 2006, 6:15 AM EST
What do you mean you have "characters in ascii bytes" and "characters in hex"? If you convert your ASCII's to bytes, they are numbers. So it doesent matter if you read them as decimal numbers or hex numbers.
sudheer panwar19 pointsPosted Jan 30, 2006, 12:02 AM EST
private byte[] ConvertToHexArray(byte [] asciiData){ if(asciiData != null && asciiData.Length >0){ byte[] byteHexArray = new byte[asciiData.Length]; for(int i = 0; i<asciiData.Length ; i ++) byteHexArray[i]= Byte.Parse(asciiData[i].ToString("X2"),NumberStyles.HexNumber); return byteHexArray; } return null; }