Git Product home page Git Product logo

hex's Introduction

Hex library

Package contains classes to convert bytes to hex representation and vice versa.

HexHelper class contains methods to convert byte array to hex string, to convert hex string to byte array and to check, if string is a hex representation of bytes.

An example, how to convert bytes to hex symbols:

String str1 = HexHelper.toHex(new byte[]{1, 5, (byte) 140, (byte) 250, -14}); // str1 = "01058CFAF2"
String str2 = HexHelper.toHex(new byte[]{17, 28, (byte) 179, -14}); // str2 = "111CB3F2"
String str3 = HexHelper.toHex(new byte[]{(byte) 255, (byte) 250, (byte) 180, 17, 94}); // str3 = "FFFAB4115E"

An example, how to convert hex symbols to bytes:

byte[] bytes1 = HexHelper.toBytes("AC120F"); // bytes1 = new byte[]{(byte) 172, 18, 15}
byte[] bytes2 = HexHelper.toBytes("AACD2F"); // bytes2 = new byte[]{(byte) 170, (byte) 205, 47}
byte[] bytes3 = HexHelper.toBytes("aacd2f"); // bytes3 = new byte[]{(byte) 170, (byte) 205, 47}

HexHelper class contains all data in memory. For large data (for example, big files) this is not efficient. In such cases HexInputStream and HexOutputStream classes could be used. HexInputStream read stream of hex symbols and translates them to bytes. HexOutputStream writes hex symbols to the stream.

An example, how to write hex symbols to the file:

try (FileInputStream inputStream = new FileInputStream("some input file");
     HexOutputStream outputStream = new HexOutputStream(new FileOutputStream("some output file"))) {
    int b;
    while (true) {
        b = inputStream.read();
        if (b < 0) {
            break;
        }
        outputStream.write(b);
    }
}

Each byte is represented with two hex symbols. That is why hex representation doubles the original data size.

hex's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.