Hi All,
I have the following hex string "03828710073100A22300";
I want to convert it to byte array in the following way:
"03" to 3
"82" to 82
"87" to 87
"10" to 10
"07" to 07
the byte array should look like {3,82, 87.10. 7....};
Thank you for your help.
|
|
0
|
|
|
|
Reply
|
alexia
|
11/25/2009 2:49:50 PM |
|
"alexia" <alexia.bee@gmail.com> wrote in message
news:24ad9d98-a2b1-4625-8521-42ae5b5edd73@l13g2000yqb.googlegroups.com...
> I have the following hex string "03828710073100A22300";
>
> I want to convert it to byte array in the following way:
>
> "03" to 3
> "82" to 82
> "87" to 87
> "10" to 10
> "07" to 07
> the byte array should look like {3,82, 87.10. 7....};
Okay, lots of problems with this post. First, in the array example above, I
have to assume your finger drifted from the comma to the period and those
two periods are supposed to be commas.
Second, 10 in hex (0x10) is NOT the same as the number "ten," which is what
is implied by what you wrote above in the array sample. Did you mean to
write {3, 0x82, 0x87, 0x10, 7} ?
Third, assuming you really DO have a string of hex digits (and the only part
of your example that suggests you do is the "A"), then all you have to do is
substring through the string in chunks of two characters and pass those
substrings to the byte.TryParse() method. Use the overload that takes a
NumberStyles argument and pass NumberStyles.AllowHexSpecifier.
|
|
0
|
|
|
|
Reply
|
Jeff
|
11/25/2009 3:04:28 PM
|
|
alexia wrote:
> I have the following hex string "03828710073100A22300";
>
> I want to convert it to byte array in the following way:
>
> "03" to 3
> "82" to 82
> "87" to 87
> "10" to 10
> "07" to 07
> the byte array should look like {3,82, 87.10. 7....};
Assuming that you mean hex as in the subject line (and not
decimal as in the example), then:
public static byte[] FromHex(string s)
{
byte[] ba = new byte[(s.Length+1)/3];
for(int i = 0; i < ba.Length; i++)
{
ba[i] = byte.Parse(s.Substring(3 * i, 2),
NumberStyles.HexNumber);
}
return ba;
}
Arne
|
|
0
|
|
|
|
Reply
|
ISO
|
11/25/2009 10:13:53 PM
|
|
"Arne Vajh�j" <arne@vajhoej.dk> wrote in message
news:4b0dac19$0$270$14726298@news.sunsite.dk...
> alexia wrote:
>> I have the following hex string "03828710073100A22300";
>>
>> I want to convert it to byte array in the following way:
>>
>> "03" to 3
>> "82" to 82
>> "87" to 87
>> "10" to 10
>> "07" to 07
>> the byte array should look like {3,82, 87.10. 7....};
>
> Assuming that you mean hex as in the subject line (and not
> decimal as in the example), then:
>
> public static byte[] FromHex(string s)
> {
> byte[] ba = new byte[(s.Length+1)/3];
> for(int i = 0; i < ba.Length; i++)
> {
> ba[i] = byte.Parse(s.Substring(3 * i, 2),
> NumberStyles.HexNumber);
> }
> return ba;
> }
Why are you dividing and multplying by 3? Am I missing something?
|
|
0
|
|
|
|
Reply
|
Jeff
|
11/25/2009 10:23:55 PM
|
|
Jeff Johnson wrote:
> "Arne Vajh�j" <arne@vajhoej.dk> wrote in message
> news:4b0dac19$0$270$14726298@news.sunsite.dk...
>> alexia wrote:
>>> I have the following hex string "03828710073100A22300";
>>>
>>> I want to convert it to byte array in the following way:
>>>
>>> "03" to 3
>>> "82" to 82
>>> "87" to 87
>>> "10" to 10
>>> "07" to 07
>>> the byte array should look like {3,82, 87.10. 7....};
>> Assuming that you mean hex as in the subject line (and not
>> decimal as in the example), then:
>>
>> public static byte[] FromHex(string s)
>> {
>> byte[] ba = new byte[(s.Length+1)/3];
>> for(int i = 0; i < ba.Length; i++)
>> {
>> ba[i] = byte.Parse(s.Substring(3 * i, 2),
>> NumberStyles.HexNumber);
>> }
>> return ba;
>> }
>
> Why are you dividing and multplying by 3? Am I missing something?
Ooops.
That code assumes dashes between bytes.
Replace 3 with 2 for the format specified by the poster.
My apologies for the goof.
Arne
|
|
0
|
|
|
|
Reply
|
ISO
|
11/25/2009 10:32:15 PM
|
|
alexia wrote:
> Hi All,
>
> I have the following hex string "03828710073100A22300";
>
> I want to convert it to byte array in the following way:
>
> "03" to 3
> "82" to 82
> "87" to 87
> "10" to 10
> "07" to 07
> the byte array should look like {3,82, 87.10. 7....};
>
> Thank you for your help.
There's a one-liner for everything. :)
byte[] data =
Regex.Matches("03828710073100A22300", "..")
.OfType<Match>()
.Select(m => Convert.ToByte(m.Value, 16))
.ToArray();
--
G�ran Andersson
_____
http://www.guffa.com
|
|
0
|
|
|
|
Reply
|
ISO
|
11/27/2009 11:30:06 AM
|
|
|
5 Replies
442 Views
(page loaded in 0.186 seconds)
Similiar Articles: read binary file to a byte array - microsoft.public.dotnet ...... data = File.ReadAllBytes(<filepath>); MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(data); now i need to convert this 'hash' to string with hexadecimal ... converting number to string in hex - microsoft.public.sqlserver ...On Tue, 27 Jul 2010 17:40:55 +0300, "Roy Goldhammer" <royg@yahoo.com> wrote: >Hello there > >I have table with bigint values. > >I need to convert them to string ... Converting VARBINARY back to a string - microsoft.public.dotnet ...1) You should use Parameters and then you can set a byte ... Convert varbinary to String: varbinary, convert, string How to convert varbinary value back to hexadecimal string? Read/Write binary file data to/from string - microsoft.public ...The inverse process is achieved by means of Convert.FromBase64String. using System.IO; .... byte[] theBytes = File.ReadAllBytes(myFile); string str = Convert ... read binary file into byte[] - microsoft.public.dotnet.languages ...byte[] theBytes = File.ReadAllBytes(pathname ... converting number to string in hex - microsoft.public ... which is what ... microsoft ... >I must convert string ... Converting a SID from Active Directory to a string. - microsoft ...Hello, I am retrieving users SID from AD which is a byte type as I'm told and I want to convert this value to a string. I have tried the followin... HASHBYTES function - microsoft.public.sqlserver.programming ...... This is only a 32 byte value if you interpret and store it as a string. ... query displays the value in hexadecimal ... How to Convert a String to INT in Classic ASP ... convert currency to string - microsoft.public.access.queries ...VBA: Convert to a Currency Data Type Excel VBA: Convert a String to a ... ... Tutorial · FGCU Technology Skills Orientation Currency ... I can convert hex ... Convert Number to Letters - microsoft.public.access.forms ...converting number to string in hex - microsoft.public.sqlserver ... Convert Number to Letters ... Writer, which function to convert amount to string ... Convert ... SHA1 Hash - microsoft.public.vb.general.discussion... Public - and that's: > Function HexDefaultSHA1(Message() As Byte) As String I ... Base64 encoding I am having a difficult time figuring out how to convert the hex string ... Converting Hexadecimal String to/from Byte Array in C# - CodeProjectDownload source (Application Form, and HexEncoding Class) - 5 Kb; Introduction. While the .NET framework provides methods to convert a byte array into a Hexadecimal ... Convert from HEX to byte arrayHi everyone! How i can't convert an HEX string into a byte array in C++? In C# i have found this : // Convert my HEX serialNumber into byte array byte ... Programmer Ramblings: Convert Hex String to Byte Array and Vice-VersaIf you've dealt with legacy encryption schemes across multiple platforms, you know what a chore it can be to deal with the various encoding options, the ... c# - Convert hex string to byte array - Stack OverflowPossible Duplicate: How do you convert Byte Array to Hexadecimal String, and vice versa, in C#? Can we convert a hex string to a byte array using a built-in function ... Convert a byte array to a Hex string - Real's Java How-toReal's JAVA JAVASCRIPT and PowerBuilder How-to pages with useful code snippets 7/18/2012 4:18:36 PM
|