tag:blogger.com,1999:blog-2706170878552557520.post4338748271439740201..comments2023-05-25T09:20:00.422-05:00Comments on Programmer Ramblings: Convert Hex String to Byte Array and Vice-VersaKenneth Scotthttp://www.blogger.com/profile/08960674005656172607noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-2706170878552557520.post-78724327848429078062012-03-12T23:43:52.612-05:002012-03-12T23:43:52.612-05:00how can you do this in vbscript? please help.how can you do this in vbscript? please help.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-58448235150860442702010-04-16T03:07:31.381-05:002010-04-16T03:07:31.381-05:00Hi guys,
I can recommend a pretty good online tool...Hi guys,<br />I can recommend a pretty good online tool to <a href="http://www.stringfunction.com/ascii-binary.html" rel="nofollow">convert ascii to binary</a><br />I think this is a good link to complement this post :)<br />Daviddavitz38https://www.blogger.com/profile/17045429763068282507noreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-58530428891014357802010-01-07T09:26:12.501-06:002010-01-07T09:26:12.501-06:00Fantastic - I've been looking for something li...Fantastic - I've been looking for something like this in order to pass a timestamp value (held in the C# class as a string) into a SQL database...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-8400499739249351442009-11-02T21:30:02.591-06:002009-11-02T21:30:02.591-06:00I understand now, thanks a lotI understand now, thanks a lotAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-81708673657728934472009-11-01T16:15:37.090-06:002009-11-01T16:15:37.090-06:00>> gives the byteBuffer in terms of decimal ...>> gives the byteBuffer in terms of decimal values, i.e, <br />>> What I want my byteBuffer of Byte Array type is of HEX-DECIMAL values, i.e.<br /><br />The statements are meaningless. The contents of byteBuffer are values -- they aren't decimal value or hexadecimal values -- just values. When printed, these values can be displayed as decimal or Hexadecimal (or octal or binary or ASCII or whatever you choose).James Curranhttps://www.blogger.com/profile/03689643776077649326noreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-60506247261056527502009-11-01T01:35:51.546-06:002009-11-01T01:35:51.546-06:00All the above code, including your suggestion of b...All the above code, including your suggestion of byteBuffer[i/2] = Convert.ToByte(bx[i / 2], 16);<br />gives the byteBuffer in terms of decimal values, i.e, <br />byteBuffer[0]=25;<br />byteBuffer[1]=56;<br />byteBuffer[2]=100;<br />byteBuffer[3]=19;<br />byteBuffer[4]=83;<br />byteBuffer[5]=34;<br />byteBuffer[6]=95;<br /><br />What I want my byteBuffer of Byte Array type is of HEX-DECIMAL values, i.e.<br />byteBuffer[0]=19;<br />byteBuffer[1]=38;<br />byteBuffer[2]=64;<br />byteBuffer[3]=13;<br />byteBuffer[4]=53;<br />byteBuffer[5]=22;<br />byteBuffer[6]=5F;<br /><br />I hope my explanation is clear.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-12045502204181014762009-11-01T00:03:05.909-05:002009-11-01T00:03:05.909-05:00Anonymous-
I'm not sure why you wouldn't ...Anonymous-<br /><br />I'm not sure why you wouldn't just use the code shown, but if you want to do it your own way I believe you'll need to specify that you're dealing with base-16 numbers.<br /><br />Change your last line <br />FROM:<br />byteBuffer[i/2] = Convert.ToByte(bx[i / 2]);<br />TO:<br />byteBuffer[i/2] = Convert.ToByte(bx[i / 2], 16);<br /><br />good luck-Kenneth Scotthttps://www.blogger.com/profile/08960674005656172607noreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-7987637224179493882009-10-31T05:17:55.342-05:002009-10-31T05:17:55.342-05:00byte[] byteBuffer = new byte[7];
string x = "...byte[] byteBuffer = new byte[7];<br />string x = "1938641353225F";<br />string[] bx = new string[x.Length / 2];<br />for (int i = 0; i < x.Length; i += 2)<br />{<br />bx[i / 2] = x.Substring(i, 2);<br /><br />byteBuffer[i/2] = Convert.ToByte(bx[i / 2]);<br />}<br /><br />I try the above code, and wants the string to be in Byte Array, not String Array.<br />It does not seem to be able to put 5F into that Byte Array, please advise.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-80798461897946168202009-08-30T02:04:16.327-05:002009-08-30T02:04:16.327-05:00Thanks, this was a real help!Thanks, this was a real help!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-70076623653156733882009-04-14T22:33:00.000-05:002009-04-14T22:33:00.000-05:00It should really be pretty trivial to port to vb.n...It should really be pretty trivial to port to vb.net. Although I sometimes have mixed results, you can probably even use one of the online code converters like this one to get you started:<br />http://www.developerfusion.com/tools/convert/csharp-to-vb/<br /><br />good luck-Kenneth Scotthttps://www.blogger.com/profile/08960674005656172607noreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-57957542645236513202009-04-14T03:50:00.000-05:002009-04-14T03:50:00.000-05:00anyone know how to do it in VB.Net?anyone know how to do it in VB.Net?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-16659968616849580402009-01-29T10:41:00.000-06:002009-01-29T10:41:00.000-06:00great, thanksgreat, thanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-69131117383849622702008-04-19T23:56:00.000-05:002008-04-19T23:56:00.000-05:00james-nice job on the cleanup. that code is sever...james-<BR/>nice job on the cleanup. that code is several years old and definitely needed to be cleaned up. thanks-Kenneth Scotthttps://www.blogger.com/profile/08960674005656172607noreply@blogger.comtag:blogger.com,1999:blog-2706170878552557520.post-31428148569347123382008-04-18T12:07:00.000-05:002008-04-18T12:07:00.000-05:00Your String_to_bytes is more or less reasonable, a...Your String_to_bytes is more or less reasonable, although it can be toned up a bit:<BR/><BR/>private byte[] String_To_Bytes2(string strInput)<BR/>{<BR/> // allocate byte array based on half of string length<BR/> int numBytes = (strInput.Length) / 2;<BR/> byte[] bytes = new byte[numBytes];<BR/> <BR/> // loop through the string - 2 bytes at a time converting it to decimal equivalent and store in byte array<BR/> // x variable used to hold byte array element position<BR/> for(int x=0; x<numBytes; ++x)<BR/> {<BR/> bytes[x] = Convert.ToByte(strInput.Substring(x*2, 2), 16);<BR/> }<BR/> <BR/> // return the finished byte array of decimal values<BR/> return bytes;<BR/>}<BR/><BR/>Bytes_To_String on the other hand, is more of a mess:<BR/><BR/> // convert the byte array back to a true string<BR/>private string Bytes_To_String2(byte[] bytes_Input)<BR/>{<BR/> StringBuilder strTemp = new StringBuilder(bytes_Input.Length *2);<BR/> foreach(byte b in bytes_Input)<BR/> {<BR/> strTemp.Append(b.ToString("X02"));<BR/> }<BR/> return strTemp.ToString();<BR/>}James Curranhttps://www.blogger.com/profile/03689643776077649326noreply@blogger.com