Java Class Field Convert To fieldToValue(byte[] buffer, int offset, int length)

Here you can find the source of fieldToValue(byte[] buffer, int offset, int length)

Description

Converts a field in a byte array into a value.

License

Open Source License

Parameter

Parameter Description
buffer The byte array that contains the value
offset The offset of where the value begins in the byte array
length The length of the field to be converted

Return

A long that represent the value of the field

Declaration

public static long fieldToValue(byte[] buffer, int offset, int length) 

Method Source Code

//package com.java2s;
/**/*from w w  w.  j  av a  2  s.c o  m*/
 * Copyright (C) 2009-2011 Couchbase, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
 * IN THE SOFTWARE.
 */

public class Main {
    /**
     * Converts a field in a byte array into a value.
     *
     * @param buffer The byte array that contains the value
     * @param offset The offset of where the value begins in the byte array
     * @param length The length of the field to be converted
     * @return A long that represent the value of the field
     */
    public static long fieldToValue(byte[] buffer, int offset, int length) {
        long total = 0;
        long val = 0;
        for (int i = 0; i < length; i++) {
            val = buffer[offset + i];
            if (val < 0) {
                val = val + 256;
            }
            total += (long) Math.pow(256.0, (double) (length - 1 - i)) * val;
        }
        return total;
    }
}

Related

  1. fieldToColumnName(String name)
  2. fieldToGetMethod(String fieldName)
  3. fieldToProperty(String field)
  4. fieldToPropertyName(String name)
  5. fieldToString(String name, Object value)