Byte to String

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

    Byte to String

    Hello,

    I am using the following to hash a string:

    public static string Hash(string text) {
    MD5CryptoServic eProvider crypto = new
    MD5CryptoServic eProvider();
    byte[] hash = null;
    UTF8Encoding encoder = new UTF8Encoding();
    hash = crypto.ComputeH ash(encoder.Get Bytes(text));
    return hash.ToString() ;
    } // Hash

    I debugged it but I still get a byte and I can't see the hashed
    string ...

    What am I missing?

    Thanks,
    Miguel
  • Stanimir Stoyanov

    #2
    Re: Byte to String

    Hi Miguel,

    byte[] is an array and ToString() will just return the type name. Here are a
    few examples:

    1) return BitConverter.To String(hash); // e.g. A1-F3-BB...

    2) return BitConverter.To String(hash).Re place("-", ""); // e.g. A1F3BB...

    3) string hashString = string.Empty;
    Array.ForEach(h ash, delegate(byte b) { hashString +=
    b.ToString("X2" ); });
    return hashString; // same as 2)
    --
    Stanimir Stoyanov
    슬롯사이트 리스트중 2025년에도 가장 중요하게 보는것은 안전성과 신뢰성입니다. 슬롯나라를 통해 안전한 온라인카지노 게임을 선택함으로써 먹튀사이트 위험에 보장을 받을 수 있습니다.슬롯나라는 10개의 먹튀검증이 완료된 안전한 온라인카지노를 추천합니다.


    "shapper" <mdmoura@gmail. comwrote in message
    news:03d81397-a924-4993-96a3-26fe92a65b30@e1 g2000pra.google groups.com...
    Hello,
    >
    I am using the following to hash a string:
    >
    public static string Hash(string text) {
    MD5CryptoServic eProvider crypto = new
    MD5CryptoServic eProvider();
    byte[] hash = null;
    UTF8Encoding encoder = new UTF8Encoding();
    hash = crypto.ComputeH ash(encoder.Get Bytes(text));
    return hash.ToString() ;
    } // Hash
    >
    I debugged it but I still get a byte and I can't see the hashed
    string ...
    >
    What am I missing?
    >
    Thanks,
    Miguel

    Comment

    • shapper

      #3
      Re: Byte to String

      On Nov 7, 12:37 am, "Stanimir Stoyanov" <stoya...@REMOV ETHIS.live.com>
      wrote:
      Hi Miguel,
      >
      byte[] is an array and ToString() will just return the type name. Here are a
      few examples:
      >
      1) return BitConverter.To String(hash); // e.g. A1-F3-BB...
      >
      2) return BitConverter.To String(hash).Re place("-", ""); // e.g. A1F3BB...
      >
      3) string hashString = string.Empty;
          Array.ForEach(h ash, delegate(byte b) { hashString +=
      b.ToString("X2" ); });
          return hashString; // same as 2)
      --
      Stanimir Stoyanovhttp://stoyanoff.info
      >
      "shapper" <mdmo...@gmail. comwrote in message
      >
      news:03d81397-a924-4993-96a3-26fe92a65b30@e1 g2000pra.google groups.com...
      >
      Hello,
      >
      I am using the following to hash a string:
      >
         public static string Hash(string text) {
           MD5CryptoServic eProvider crypto = new
      MD5CryptoServic eProvider();
           byte[] hash = null;
           UTF8Encoding encoder = new UTF8Encoding();
           hash = crypto.ComputeH ash(encoder.Get Bytes(text));
           return hash.ToString() ;
         } // Hash
      >
      I debugged it but I still get a byte and I can't see the hashed
      string ...
      >
      What am I missing?
      >
      Thanks,
      Miguel

      Thank You! It worked fine.

      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: Byte to String

        Stanimir Stoyanov wrote:
        Hi Miguel,
        >
        byte[] is an array and ToString() will just return the type name. Here
        are a few examples:
        >
        1) return BitConverter.To String(hash); // e.g. A1-F3-BB...
        >
        2) return BitConverter.To String(hash).Re place("-", ""); // e.g. A1F3BB...
        >
        3) string hashString = string.Empty;
        Array.ForEach(h ash, delegate(byte b) { hashString +=
        b.ToString("X2" ); });
        return hashString; // same as 2)
        Don't use += to concatenate strings in a loop. Use a StringBuilder instead:

        StringBuilder builder = new StringBuilder(h ash.Length * 2);
        foreach (byte b in hash) builder.Append( b.ToString("x2" ));
        return builder.ToStrin g();

        As the size of the final string is specified in the constructor, it will
        allocate a string buffer of the correct size, which then is returned as
        a string by the ToString method. (To make it ever more efficient, you
        could format the hex string yourself and append one character at a time.
        That way it wouldn't create any temporary strings at all.)

        4) return Convert.ToBase6 4String(hash); // e.g. dh1e5gxs4hh3j.. .
        --
        Stanimir Stoyanov
        슬롯사이트 리스트중 2025년에도 가장 중요하게 보는것은 안전성과 신뢰성입니다. 슬롯나라를 통해 안전한 온라인카지노 게임을 선택함으로써 먹튀사이트 위험에 보장을 받을 수 있습니다.슬롯나라는 10개의 먹튀검증이 완료된 안전한 온라인카지노를 추천합니다.

        >

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        Working...