0

Good morning\evening everyone!

i have a string in a format like "0x0SD30SV8GN48N84GN" wich represent a byte array... how can i turn that on a actual byte[]?

the actual string is quite longer.

It represents a string in UTF-8 format that im trying to convert to UTF-32...

what i want is to turn that to a human readble string... is that possible?

PS: The scenario is: my colleague stored a string in a varbinary column... executed a "Select myVarBinaryColumn FROM mytable" and sent me the result as txt... i need to convert to a human readble string...

PS2: the string can be understand as the result of the following operation: Cast('Hello World' as varbinary(max))

Leonardo
  • 10,737
  • 10
  • 62
  • 155
  • How does this string represent a byte array? And what do you mean by represents a string in UTF-8 format? Strings always use UTF-16 in .net. Your question is extremely unclear. – CodesInChaos Jul 31 '12 at 22:06
  • @CodesInChaos did you notice that the string starts with "0x" that is a international notation to indicate a hexadecimal number. If you have a MSSQL database engine close by that the following "Cast('Hello World' as varBinary(Max))" – Leonardo Jul 31 '12 at 22:30
  • I noticed The `0x`, but I also noticed plenty of non hex characters. So I wrote off the `0x` as coincidence. – CodesInChaos Aug 01 '12 at 06:00

3 Answers3

4
byte[] array = Encoding.UTF32.GetBytes(somestring);
HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • I have updated my answer after noticing that OP wants to convert to UTF32 – HatSoft Jul 31 '12 at 22:11
  • While I'm still not sure it's what the OP wants, it's no longer clearly the wrong thing(ASCII certainly wasn't correct). So I've removed the download. If it's not what the OP wants, it's his own fault for asking an unclear question. – CodesInChaos Jul 31 '12 at 22:15
  • @CodesInChaos I agree with you, I noticed your comment to OP and expected clearity – HatSoft Jul 31 '12 at 22:17
1
 string yourstring ="0x0SD30SV8GN48N84GN";
 byte[] array = Encoding.UTF32.GetBytes(yourstring);

check out this link. also, there's another very similar question on stackoverflow here

Community
  • 1
  • 1
Thousand
  • 6,562
  • 3
  • 38
  • 46
1
string byteStr = input.Substring(2);
byte[] bytes = new byte [ byteStr.Length / 2 ];
for ( int i = 0, j = 0 ; i < byteStr.Length ; i += 2 , j++ )
{
     bytes [ j ] = byte.Parse ( byteStr.Substring ( i , 2 ) , NumberStyles.HexNumber );
}
string str = Encoding.UTF8.GetString ( bytes );
byte[] UTF32Bytes = Encoding.UTF32.GetBytes ( str );
Thomas
  • 1,281
  • 8
  • 22