2

Assume that I have a string containing a hex value. For example:

string command "0xABCD1234";

How can I convert that string into another string (for example, string codedString = ...) such that this new string's ASCII-encoded representation has the same binary as the original strings contents?

The reason I need to do this is because I have a library from a hardware manufacturer that can transmit data from their piece of hardware to another piece of hardware over SPI. Their functions take strings as an input, but when I try to send "AA" I am expecting the SPI to transmit the binary 10101010, but instead it transmits the ascii representation of AA which is 0110000101100001.

Also, this hex string is going to be 32 hex characters long (that is, 256-bits long).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

4
string command = "AA";
int num = int.Parse(command,NumberStyles.HexNumber);
string bits = Convert.ToString(num,2); // <-- 10101010
I4V
  • 34,891
  • 6
  • 67
  • 79
  • 1
    This shows how knowledge of the framework pays off. I love it. – Nathan Jun 21 '13 at 20:40
  • I would need `bits` to not contain the string "10101010", but whatever string would be represented by the binary 10101010! –  Jun 21 '13 at 20:58
  • @w1res then it is `num`. it contains that bits. (if you want to send 1 byte than use `byte.Parse`). BTW: bits=>string is not always possible. every string has a byte(bit) array representation but every byte array doesn't necesseraly have a valid string representation. – I4V Jun 21 '13 at 21:02
  • You seem to be right. I just tried to do an example by hand, and realized that the ASCII characters don't even go high enough. I am not sure how it is possible to send the 256 bits of data if the only arguments that can be passed to the SPI function are Int8, Int16, Int32, and string. I will have to talk to the manufacturer I guess. –  Jun 21 '13 at 21:23
  • @w1res then use for ex, `Send((byte)0xaa)`; – I4V Jun 21 '13 at 21:28
-2

I think I understand what you need... here is the main code part.. asciiStringWithTheRightBytes is what you would send to your command.

var command = "ABCD1234";
var byteCommand = GetBytesFromHexString(command);
var asciiStringWithTheRightBytes = Encoding.ASCII.GetString(byteCommand);

And the subroutines it uses are here...

static byte[] GetBytesFromHexString(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(byte)];
    for (var i = 0; i < str.Length; i++)
        bytes[i] = HexToInt(str[i]);
        return bytes;
}

static byte HexToInt(char hexChar)
{
    hexChar = char.ToUpper(hexChar);  // may not be necessary

    return (byte)((int)hexChar < (int)'A' ?
        ((int)hexChar - (int)'0') :
        10 + ((int)hexChar - (int)'A'));
}
Kevin
  • 4,586
  • 23
  • 35
  • Kevin, do you know there is a built-in class that does the same thing (SoapHexBinary)? Just search stackoverflow. – I4V Jun 21 '13 at 21:31
  • @I4V I wouldn't have bothered writing the code if I had known. :) Thanks for the tip. When I did search I didn't find SoapHexBinary. – Kevin Jun 21 '13 at 21:47
  • http://stackoverflow.com/questions/13166443/how-to-delete-every-2nd-character-in-a-string http://stackoverflow.com/questions/12942904/calculate-twos-complement-checksum-of-hexadecimal-string – I4V Jun 21 '13 at 21:55