Converting String to Hex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LCD

    Converting String to Hex

    This a rather simple question for all you studs out there! Please help me
    with this. I have a string = "Please help me", and I want to convert this
    into it's hex equivalent. How do I do it, I have trying for a couple hours
    and can't seem to get my head straight arount this whole conversion things.

    TIA, LCD
  • Dennis

    #2
    RE: Converting String to Hex

    Not sure exactly what you want to do but check out the Hex Function, Convert
    Class, and ToCharArray string method. One or all of these in combination
    should get you what you want.
    --
    Dennis in Houston


    "LCD" wrote:
    [color=blue]
    > This a rather simple question for all you studs out there! Please help me
    > with this. I have a string = "Please help me", and I want to convert this
    > into it's hex equivalent. How do I do it, I have trying for a couple hours
    > and can't seem to get my head straight arount this whole conversion things.
    >
    > TIA, LCD[/color]

    Comment

    • LCD

      #3
      RE: Converting String to Hex

      Okay I want to doing something similar to this. In python, there is a
      function called "binascii".

      So if I type:
      ###[color=blue][color=green][color=darkred]
      >>> binascii.hexlif y('hello world')[/color][/color][/color]

      This is the result I get:

      '68656c6c6f2077 6f726c64'
      ###

      I want to do something similar in VB.NET or C#. Can someone help?

      "Dennis" wrote:
      [color=blue]
      > Not sure exactly what you want to do but check out the Hex Function, Convert
      > Class, and ToCharArray string method. One or all of these in combination
      > should get you what you want.
      > --
      > Dennis in Houston
      >
      >
      > "LCD" wrote:
      >[color=green]
      > > This a rather simple question for all you studs out there! Please help me
      > > with this. I have a string = "Please help me", and I want to convert this
      > > into it's hex equivalent. How do I do it, I have trying for a couple hours
      > > and can't seem to get my head straight arount this whole conversion things.
      > >
      > > TIA, LCD[/color][/color]

      Comment

      • rmacias

        #4
        RE: Converting String to Hex

        Dim str As String = "hello world"
        Dim byteArray() As Byte

        byteArray = System.Text.ASC IIEncoding.ASCI I.GetBytes(str)

        "LCD" wrote:
        [color=blue]
        > Okay I want to doing something similar to this. In python, there is a
        > function called "binascii".
        >
        > So if I type:
        > ###[color=green][color=darkred]
        > >>> binascii.hexlif y('hello world')[/color][/color]
        >
        > This is the result I get:
        >
        > '68656c6c6f2077 6f726c64'
        > ###
        >
        > I want to do something similar in VB.NET or C#. Can someone help?
        >
        > "Dennis" wrote:
        >[color=green]
        > > Not sure exactly what you want to do but check out the Hex Function, Convert
        > > Class, and ToCharArray string method. One or all of these in combination
        > > should get you what you want.
        > > --
        > > Dennis in Houston
        > >
        > >
        > > "LCD" wrote:
        > >[color=darkred]
        > > > This a rather simple question for all you studs out there! Please help me
        > > > with this. I have a string = "Please help me", and I want to convert this
        > > > into it's hex equivalent. How do I do it, I have trying for a couple hours
        > > > and can't seem to get my head straight arount this whole conversion things.
        > > >
        > > > TIA, LCD[/color][/color][/color]

        Comment

        • rmacias

          #5
          RE: Converting String to Hex

          I just realized you are probably asking for the ascii values of each
          character of a string in hex format. This should do it for you:

          Dim str As String = "hello world"
          Dim byteArray() As Byte
          Dim hexNumbers As System.Text.Str ingBuilder = New System.Text.Str ingBuilder

          byteArray = System.Text.ASC IIEncoding.ASCI I.GetBytes(str)

          For i As Integer = 0 To byteArray.Lengt h - 1
          hexNumbers.Appe nd(byteArray(i) .ToString("x"))
          Next

          MessageBox.Show (hexNumbers.ToS tring())

          The output should be:
          68656c6c6f20776 f726c64

          "rmacias" wrote:
          [color=blue]
          > Dim str As String = "hello world"
          > Dim byteArray() As Byte
          >
          > byteArray = System.Text.ASC IIEncoding.ASCI I.GetBytes(str)
          >
          > "LCD" wrote:
          >[color=green]
          > > Okay I want to doing something similar to this. In python, there is a
          > > function called "binascii".
          > >
          > > So if I type:
          > > ###[color=darkred]
          > > >>> binascii.hexlif y('hello world')[/color]
          > >
          > > This is the result I get:
          > >
          > > '68656c6c6f2077 6f726c64'
          > > ###
          > >
          > > I want to do something similar in VB.NET or C#. Can someone help?
          > >
          > > "Dennis" wrote:
          > >[color=darkred]
          > > > Not sure exactly what you want to do but check out the Hex Function, Convert
          > > > Class, and ToCharArray string method. One or all of these in combination
          > > > should get you what you want.
          > > > --
          > > > Dennis in Houston
          > > >
          > > >
          > > > "LCD" wrote:
          > > >
          > > > > This a rather simple question for all you studs out there! Please help me
          > > > > with this. I have a string = "Please help me", and I want to convert this
          > > > > into it's hex equivalent. How do I do it, I have trying for a couple hours
          > > > > and can't seem to get my head straight arount this whole conversion things.
          > > > >
          > > > > TIA, LCD[/color][/color][/color]

          Comment

          • LCD

            #6
            RE: Converting String to Hex

            Dude, you are genius!

            You made it like POC (piece of cake). Now please explain me this, I didn't
            quite catch this part:

            hexNumbers.Appe nd(byteArray(i) .ToString("x"))

            where is this "x" coming from? and why?

            TIA, LCD

            "rmacias" wrote:
            [color=blue]
            > I just realized you are probably asking for the ascii values of each
            > character of a string in hex format. This should do it for you:
            >
            > Dim str As String = "hello world"
            > Dim byteArray() As Byte
            > Dim hexNumbers As System.Text.Str ingBuilder = New System.Text.Str ingBuilder
            >
            > byteArray = System.Text.ASC IIEncoding.ASCI I.GetBytes(str)
            >
            > For i As Integer = 0 To byteArray.Lengt h - 1
            > hexNumbers.Appe nd(byteArray(i) .ToString("x"))
            > Next
            >
            > MessageBox.Show (hexNumbers.ToS tring())
            >
            > The output should be:
            > 68656c6c6f20776 f726c64
            >
            > "rmacias" wrote:
            >[color=green]
            > > Dim str As String = "hello world"
            > > Dim byteArray() As Byte
            > >
            > > byteArray = System.Text.ASC IIEncoding.ASCI I.GetBytes(str)
            > >
            > > "LCD" wrote:
            > >[color=darkred]
            > > > Okay I want to doing something similar to this. In python, there is a
            > > > function called "binascii".
            > > >
            > > > So if I type:
            > > > ###
            > > > >>> binascii.hexlif y('hello world')
            > > >
            > > > This is the result I get:
            > > >
            > > > '68656c6c6f2077 6f726c64'
            > > > ###
            > > >
            > > > I want to do something similar in VB.NET or C#. Can someone help?
            > > >
            > > > "Dennis" wrote:
            > > >
            > > > > Not sure exactly what you want to do but check out the Hex Function, Convert
            > > > > Class, and ToCharArray string method. One or all of these in combination
            > > > > should get you what you want.
            > > > > --
            > > > > Dennis in Houston
            > > > >
            > > > >
            > > > > "LCD" wrote:
            > > > >
            > > > > > This a rather simple question for all you studs out there! Please help me
            > > > > > with this. I have a string = "Please help me", and I want to convert this
            > > > > > into it's hex equivalent. How do I do it, I have trying for a couple hours
            > > > > > and can't seem to get my head straight arount this whole conversion things.
            > > > > >
            > > > > > TIA, LCD[/color][/color][/color]

            Comment

            • rmacias

              #7
              RE: Converting String to Hex

              The Byte structure has several overloaded ToString() methods. The one used
              below accepts a string representation of a NumberFormatInf o.

              "X" or "x" means the format will be hexadecimal
              "D" or "d" means the format will be in decimal, etc.

              The below links shows possible formats:




              "LCD" wrote:
              [color=blue]
              > Dude, you are genius!
              >
              > You made it like POC (piece of cake). Now please explain me this, I didn't
              > quite catch this part:
              >
              > hexNumbers.Appe nd(byteArray(i) .ToString("x"))
              >
              > where is this "x" coming from? and why?
              >
              > TIA, LCD
              >
              > "rmacias" wrote:
              >[color=green]
              > > I just realized you are probably asking for the ascii values of each
              > > character of a string in hex format. This should do it for you:
              > >
              > > Dim str As String = "hello world"
              > > Dim byteArray() As Byte
              > > Dim hexNumbers As System.Text.Str ingBuilder = New System.Text.Str ingBuilder
              > >
              > > byteArray = System.Text.ASC IIEncoding.ASCI I.GetBytes(str)
              > >
              > > For i As Integer = 0 To byteArray.Lengt h - 1
              > > hexNumbers.Appe nd(byteArray(i) .ToString("x"))
              > > Next
              > >
              > > MessageBox.Show (hexNumbers.ToS tring())
              > >
              > > The output should be:
              > > 68656c6c6f20776 f726c64
              > >
              > > "rmacias" wrote:
              > >[color=darkred]
              > > > Dim str As String = "hello world"
              > > > Dim byteArray() As Byte
              > > >
              > > > byteArray = System.Text.ASC IIEncoding.ASCI I.GetBytes(str)
              > > >
              > > > "LCD" wrote:
              > > >
              > > > > Okay I want to doing something similar to this. In python, there is a
              > > > > function called "binascii".
              > > > >
              > > > > So if I type:
              > > > > ###
              > > > > >>> binascii.hexlif y('hello world')
              > > > >
              > > > > This is the result I get:
              > > > >
              > > > > '68656c6c6f2077 6f726c64'
              > > > > ###
              > > > >
              > > > > I want to do something similar in VB.NET or C#. Can someone help?
              > > > >
              > > > > "Dennis" wrote:
              > > > >
              > > > > > Not sure exactly what you want to do but check out the Hex Function, Convert
              > > > > > Class, and ToCharArray string method. One or all of these in combination
              > > > > > should get you what you want.
              > > > > > --
              > > > > > Dennis in Houston
              > > > > >
              > > > > >
              > > > > > "LCD" wrote:
              > > > > >
              > > > > > > This a rather simple question for all you studs out there! Please help me
              > > > > > > with this. I have a string = "Please help me", and I want to convert this
              > > > > > > into it's hex equivalent. How do I do it, I have trying for a couple hours
              > > > > > > and can't seem to get my head straight arount this whole conversion things.
              > > > > > >
              > > > > > > TIA, LCD[/color][/color][/color]

              Comment

              Working...