# How to create an ndArray using the byte\[\] obtained by ndArray.data().asBytes()?

**URL:** <https://community.konduit.ai/t/how-to-create-an-ndarray-using-the-byte-obtained-by-ndarray-data-asbytes/3340>\
**Category:** ND4J\
**Created:** [March 18, 2025, 6:46am UTC](https://community.konduit.ai/t/how-to-create-an-ndarray-using-the-byte-obtained-by-ndarray-data-asbytes/3340 "2025-03-18T06:46:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cqiaoYc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cqiaoyc/32/738_2.png) [@cqiaoYc](https://community.konduit.ai/u/cqiaoYc)\
**Post date:** [March 18, 2025, 6:46am UTC](https://community.konduit.ai/t/how-to-create-an-ndarray-using-the-byte-obtained-by-ndarray-data-asbytes/3340/1 "2025-03-18T06:46:07Z")

</div>

Why is it that for the byte array `dataBuffer` created by `Nd4j.createTypedBuffer(bytes, DataType.FLOAT)` , when using `Nd4j.create(dataBuffer, shape);` to create an `ndarray` , the values of the `ndarray` are float numbers that are the same as the integer values in the `bytes` , instead of converting the byte array into floats?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [March 18, 2025, 7:08am UTC](https://community.konduit.ai/t/how-to-create-an-ndarray-using-the-byte-obtained-by-ndarray-data-asbytes/3340/2 "2025-03-18T07:08:53Z")

</div>

@cqiaoYc I would recommend staying away from that in general. At least in M2.1. Before I rewrote it in the newer code (no the rewrite’s not done yet..) it was really ambiguous as to what the byte array is. I would manually convert it like I did later:  
@Override  
public void put(byte element) {  
if (released.get())  
throw new IllegalStateException(“You can’t use DataBuffer once it was released”);

```
    switch (dataType()) {
        case BOOL:
            ((BooleanIndexer) indexer).put(0, ArrayUtil.toBooleanArray(element));
            break;
        case BYTE:
            ((ByteIndexer) indexer).put(0,element);
            break;
        case UBYTE:
            ((UByteIndexer) indexer).put(0,ArrayUtil.toIntArraySimple(element));
            break;
        case UINT16:
            ((UShortIndexer) indexer).put(0,ArrayUtil.toIntArraySimple(element));
            break;
        case SHORT:
            ((ShortIndexer) indexer).put(0,ArrayUtil.toShorts(element));
            break;
        case UINT32:
            ((UIntIndexer) indexer).put(0,ArrayUtil.toLongArray(element));
            break;
        case INT:
            ((IntIndexer) indexer).put(0,ArrayUtil.toIntArraySimple(element));
            break;
        case UINT64:
            ((ULongIndexer) indexer).put(0,ArrayUtil.toBigInteger(element));
            break;
        case LONG:
            ((LongIndexer) indexer).put(0,ArrayUtil.toLongArray(element));
            break;
        case BFLOAT16:
            ((Bfloat16Indexer) indexer).put(0,ArrayUtil.toFloatArraySimple(element));
            break;
        case HALF:
            ((HalfIndexer) indexer).put(0,ArrayUtil.toFloatArraySimple(element));
            break;
        case FLOAT:
            ((FloatIndexer) indexer).put(0,ArrayUtil.toFloatArraySimple(element));
            break;
        case DOUBLE:
            ((DoubleIndexer) indexer).put(0,ArrayUtil.toDoubleArraySimple(element));
            break;
        default:
            throw new UnsupportedOperationException("Unsupported data type: " + dataType());
    }
}

```

It’s just an extra method call and should be enough for your use case.

---

<div class="post-metadata">

**Author:** ![cqiaoYc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cqiaoyc/32/738_2.png) [@cqiaoYc](https://community.konduit.ai/u/cqiaoYc)\
**Post date:** [March 18, 2025, 7:17am UTC](https://community.konduit.ai/t/how-to-create-an-ndarray-using-the-byte-obtained-by-ndarray-data-asbytes/3340/3 "2025-03-18T07:17:41Z")

</div>

@agibsonccc Maybe I didn’t make my requirement clear. First, save the `bytes` obtained from `ndArray.data().asBytes()` into a binary data file. Then, load the `bytes` from the binary data file into memory. Finally, create an `ndArray` based on the `bytes` .

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [March 18, 2025, 7:19am UTC](https://community.konduit.ai/t/how-to-create-an-ndarray-using-the-byte-obtained-by-ndarray-data-asbytes/3340/4 "2025-03-18T07:19:20Z")

</div>

@cqiaoYc yes and I’m saying either create a wrapper method using what I specified. I’m also mentioning to you that in M2.1 the asBytes() needed a bit of rewriting due to what “bytes” can mean. The way I did it in the latest master should work for you.
