Convert hexa

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • [Gho]

    #1

    Convert hexa

    How to convert a Hexa value to negative and non negative
    value :
    if i get 0x1d = result should be 29
    but if i get fd = result should be -3

    How to do that
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Convert hexa

    Gho,

    I don't think that is right. Why can't fd equal 253? It all depends on
    the type that you are trying to convert to. Based on that, if the initial
    bit is set, then it is a negative number, otherwise, it is a positive one.
    However, even converting to an signed byte, a value of fd should come out
    to -115 (I believe). I think that a value of 8d will give you -3 though.

    Regardless, to convert from a string representation to a value, you can
    use the Parse method on the Int32 (or other numeric types, Int16, Byte, etc,
    etc) to convert from a string to a value. Use the overload that takes a
    NumberStyles enumeration value and pass NumberStyles.He xNumber for that
    parameter.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "[Gho]" <yousefk@taux01 .nsc.com> wrote in message
    news:04c501c397 d4$a372da60$a00 1280a@phx.gbl.. .[color=blue]
    > How to convert a Hexa value to negative and non negative
    > value :
    > if i get 0x1d = result should be 29
    > but if i get fd = result should be -3
    >
    > How to do that[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Convert hexa

      Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=blue]
      > I don't think that is right. Why can't fd equal 253? It all depends on
      > the type that you are trying to convert to. Based on that, if the initial
      > bit is set, then it is a negative number, otherwise, it is a positive one.
      > However, even converting to an signed byte, a value of fd should come out
      > to -115 (I believe). I think that a value of 8d will give you -3 though.[/color]

      I don't think so - bear in mind that 0xff (all bits set) should be the
      bit pattern for -1.
      [color=blue]
      > Regardless, to convert from a string representation to a value, you can
      > use the Parse method on the Int32 (or other numeric types, Int16, Byte, etc,
      > etc) to convert from a string to a value. Use the overload that takes a
      > NumberStyles enumeration value and pass NumberStyles.He xNumber for that
      > parameter.[/color]

      Or in this case, just cast from the byte to sbyte - I think that would
      work better, as I suspect that SByte.Parse will assume the range of the
      type is the range of the string to accept (as it were) with no overflow
      - and overflow is basically what the OP wants, I think.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...