Return a hex string of the contents of buffer from position to limit - CSharp System

CSharp examples for System:Hex

Description

Return a hex string of the contents of buffer from position to limit

Demo Code

/**/* w  w  w .j ava 2s  . c  o m*/
 * Copyright (C) 2013-2015 Regents of the University of California.
 * Authors:
 *    Jeff Thompson <jefft0@remap.ucla.edu>
 *    Rafael Teixeira <monoman@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * A copy of the GNU Lesser General Public License is in the file COPYING.
 */
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        /**
       * Return a hex string of the contents of buffer from position to limit.
       * @param buffer The buffer.
       * @return A string of hex bytes.
       */
      public static String
      toHex(ByteBuffer buffer)
      {
         StringBuffer output = new StringBuffer(buffer.remaining() * 2);
         for (int i = buffer.position(); i < buffer.limit(); ++i) {
            String hex = Integer.toHexString((int)buffer.get(i) & 0xff);
            if (hex.length() <= 1)
               // Append the leading zero.
               output.append("0");
            output.append(hex);
         }

         return output.toString();
      }
        /**
       * Return a hex string of buf() from position to limit.
       * @return A string of hex bytes, or "" if the buffer is null.
       */
      public String
      toHex()
      {
         if (buffer_ == null)
            return "";
         else
            return toHex(buffer_);
      }
        /**
       * Decode the byte array as UTF8 and return the Unicode string.
       * @return A unicode string, or "" if the buffer is null.
       */
      public String
      toString()
      {
         if (buffer_ == null)
            return "";
         else {
            try {
               return new String(buffer_.array(), "UTF-8");
            } catch (UnsupportedEncodingException ex) {
               // We don't expect this to happen.
               throw new Error("UTF-8 decoder not supported: " + ex.getMessage());
            }
         }
      }
}

Related Tutorials