https://github.com/artembotnev/low-level-extensions
Simple Kotlin, Java library for easy work with numeric types converting to bytes array and vice versa.
https://github.com/artembotnev/low-level-extensions
byte-array java kotlin low-level
Last synced: 5 months ago
JSON representation
Simple Kotlin, Java library for easy work with numeric types converting to bytes array and vice versa.
- Host: GitHub
- URL: https://github.com/artembotnev/low-level-extensions
- Owner: ArtemBotnev
- License: apache-2.0
- Created: 2020-05-21T19:16:46.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-29T15:38:50.000Z (over 5 years ago)
- Last Synced: 2025-02-15T13:46:41.574Z (11 months ago)
- Topics: byte-array, java, kotlin, low-level
- Language: Kotlin
- Homepage:
- Size: 64.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://jitpack.io/#ArtemBotnev/low-level-extensions)
## Low level extensions
#### Simple Kotlin, Java library for easy work with numeric types converting to bytes array and vice versa.
#### Support for all Number types.
#### Kotlin example of usage:
Convert integer to byte array
```kotlin
val source = -343
val bytes = source.toByteArray()
```
Convert byte array to short
```kotlin
val bytes = byteArrayOf(-18, 41)
val result: Short = bytes.toShort()
```
Convert byte array to hex string
```kotlin
val bytes = byteArrayOf(-12, 32, 12, 0, 32, -73, 94, 120, -54, 34, 93, -107)
val hexString = value.toHexString()
```
result: f4200c0020b75e78ca225d95
You can pass ```true``` to function ```toHexString(true)``` for upper case letters.
Convert hex string to byte array
```kotlin
val hexString = "a0f33402"
val bytes = hexString.fromHexStringToByteArray()
```
#### Java example of usage:
Convert integer to byte array
```java
int source = -343;
byte[] bytes = LowLevelExtensionsKt.toByteArray(source);
```
Convert byte array to long
```java
byte[] bytes = { 34, -2, 13, 34, 23, -3, 43, 102 };
long result = LowLevelExtensionsKt.toLong(bytes);
```
Convert byte array to hex string
```java
byte[] byteArray = { -12, 32, 12, 0, 32, -73, 94, 120, -54, 34, 93, -107 };
String hexString = LowLevelExtensionsKt.toHexString(byteArray, false);
```
result: f4200c0020b75e78ca225d95
You can pass ```true``` to method ```toHexString(byteArray, true)``` for upper case letters.
Convert hex string to byte array
```java
String hexString = "a0f33402";
byte[] byteArray = LowLevelExtensionsKt.fromHexStringToByteArray(hexString);
```
#### To use this, add dependensies to your project:
Jitpack repository to your root build.gradle at the end of repositories
```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
and to module level build.gradle
```groovy
dependencies {
implementation 'com.github.ArtemBotnev:low-level-extensions:1.1.1'
}
```