Jump to content

[Help] Reversing a hexadecimal value in bytes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Sablis

Sablis

    Newbie

  • Members
  • Pip
  • 1 posts
I have made a little application to convert a decimal value, into a hexadecimal value using the useful:
Convert.ToString(num, 16).ToUpper

But I want to reverse the hexadecimal value into bytes, for an example:

The hexadecimal value is:
3B45C

I want to reverse this value into bytes so it would be:
5C B4 03

Any suggestions? I would be very grateful for any help, as I am at a complete loss to where I should even start.

I am using Visual Basic 2008 Express Edition.

Edited by Sablis, 09 October 2009 - 02:50 PM.
Forgot to add what version of VB I use.


#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
I don't know if VB.NET has some kind of function for this that might help you, if not you could do it the hard way:

(in vb-ish pseudocode)
if (lengthOfHexString mod 2) != 0 then   
hexString = "0" + hexString

for(i =lengthOfHexString to 0, step 2)
    outputString = outputString + hexString[i-1] + hexString[i] + delimitingCharacter
next i

outputString = outputString - delimitingCharacter

I should have been in bed so my logic might be flawed, but try this. Do you understand the pseudocode?
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
There's probably a right() function and a left() function (or a copy function) that will let you peel off 2 characters at a time from the right side of the string.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog