-
Apr 24th, 2017, 06:34 PM
#1
Thread Starter
Lively Member
[RESOLVED] adding zero if byte is less than 10
Hi
I am converting a byte array to hex, problem is I lose the zero if the hex value is less than 10. I've tried googling the problem and cant seem to find a solution for my case.
If you can spot something I've missed I'd appreciate it
Code:
- Function BinaryToHex_Curve(ByRef binaryData As Byte()) As String
- Dim hexValue As String = ""
- Dim I32 As Int32
- For I32 = 4 To binaryData.Length - 1
- hexValue = hexValue + (String.Format("{0}", binaryData(I32).ToString("X")))
- Next
- Return hexValue
- End Function
-
Apr 24th, 2017, 06:50 PM
#2
Addicted Member
Re: adding zero if byte is less than 10
Hex(int) returns a string based on the given integer. Try this in the for loop:
Code:
Dim hx As String = Hex(binaryData(I32))
If hx.Length = 1 Then hexValue &= "0"
hexValue &= hx
Hope this helped!
Nic
-
Apr 24th, 2017, 07:47 PM
#3
Thread Starter
Lively Member
Re: adding zero if byte is less than 10
Cheers Nic, changed a couple of things but it did work.
-
Apr 24th, 2017, 09:58 PM
#4
Re: [RESOLVED] adding zero if byte is less than 10
All you need do is change 'ToString("X")' to 'ToString("X2")'.
-
Apr 24th, 2017, 10:02 PM
#5
Re: [RESOLVED] adding zero if byte is less than 10
There's no good reason to have declared your 'binaryData' parameter ByRef in that code. You should do some reading and learn what ByRef is actually for. For a reference type, you should declare a parameter ByRef if and only if you want to assign a different object to the parameter inside the method and have that affect the original variable too. There is no other reason to declare a reference type parameter ByRef.
As for your code, that can be simplified to this:
vb.net Code:
Return String.Skip(4).Concat(binaryData.Select(Function(b) b.ToString("X2"))
-
Apr 25th, 2017, 03:50 AM
#6
Thread Starter
Lively Member
Re: [RESOLVED] adding zero if byte is less than 10
Thanks for all the reply's i'll take on board all that was said.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|