How to Convert a String of Bytes into an Int

How to convert a string of bytes into an int?

You can also use the struct module to do this:

>>> struct.unpack("<L", "y\xcc\xa6\xbb")[0]
3148270713L

Convert bytes to int?

Assuming you're on at least 3.2, there's a built in for this:

int.from_bytes( bytes, byteorder, *, signed=False )

...

The argument bytes must either be a bytes-like object or an iterable
producing bytes.

The byteorder argument determines the byte order used to represent the
integer. If byteorder is "big", the most significant byte is at the
beginning of the byte array. If byteorder is "little", the most
significant byte is at the end of the byte array. To request the
native byte order of the host system, use sys.byteorder as the byte
order value.

The signed argument indicates whether two’s complement is used to
represent the integer.

## Examples:
int.from_bytes(b'\x00\x01', "big") # 1
int.from_bytes(b'\x00\x01', "little") # 256

int.from_bytes(b'\x00\x10', byteorder='little') # 4096
int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) #-1024

How to convert bytes string to integer values?

Just throw the list constructor at it.

>>> list(b'\xff\xd8\xff\xe1')
[255, 216, 255, 225]

How to convert from []byte to int in Go Programming

You can use encoding/binary's ByteOrder to do this for 16, 32, 64 bit types

Play

package main

import "fmt"
import "encoding/binary"

func main() {
var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
data := binary.BigEndian.Uint64(mySlice)
fmt.Println(data)
}

Fast way to convert a byte[] string to its Integer value

    int n=0;
for(byte b : token)
n = 10*n + (b-'0');

String of bytes into an int

Use int() with either str.split():

In [31]: s='20 01'

In [32]: int("".join(s.split()),16)
Out[32]: 8193

or str.replace() and pass the base as 16:

In [34]: int(s.replace(" ",""),16)
Out[34]: 8193

Here both split() and replace() are converting '20 01' into '2001':

In [35]: '20 01'.replace(" ","")
Out[35]: '2001'

In [36]: "".join('20 01'.split())
Out[36]: '2001'

Conver string (text) representation of 2 bytes to Integer

You probbaly want this:

#include <stdlib.h>
#include <stdio.h>

int main()
{
char *item[4];
item[0] = "0x4b";
item[1] = "0xab";
item[2] = "0x14";
item[3] = "0x9d";

int value1 = (strtol(item[0], NULL, 0) << 8) | strtol(item[1], NULL, 0);
int value2 = (strtol(item[2], NULL, 0) << 8) | strtol(item[3], NULL, 0);

printf("%x %x", value1, value2);
}

Converting from byte to int in Java

Your array is of byte primitives, but you're trying to call a method on them.

You don't need to do anything explicit to convert a byte to an int, just:

int i=rno[0];

...since it's not a downcast.

Note that the default behavior of byte-to-int conversion is to preserve the sign of the value (remember byte is a signed type in Java). So for instance:

byte b1 = -100;
int i1 = b1;
System.out.println(i1); // -100

If you were thinking of the byte as unsigned (156) rather than signed (-100), as of Java 8 there's Byte.toUnsignedInt:

byte b2 = -100; // Or `= (byte)156;`
int = Byte.toUnsignedInt(b2);
System.out.println(i2); // 156

Prior to Java 8, to get the equivalent value in the int you'd need to mask off the sign bits:

byte b2 = -100; // Or `= (byte)156;`
int i2 = (b2 & 0xFF);
System.out.println(i2); // 156

Just for completeness #1: If you did want to use the various methods of Byte for some reason (you don't need to here), you could use a boxing conversion:

Byte b = rno[0]; // Boxing conversion converts `byte` to `Byte`
int i = b.intValue();

Or the Byte constructor:

Byte b = new Byte(rno[0]);
int i = b.intValue();

But again, you don't need that here.


Just for completeness #2: If it were a downcast (e.g., if you were trying to convert an int to a byte), all you need is a cast:

int i;
byte b;

i = 5;
b = (byte)i;

This assures the compiler that you know it's a downcast, so you don't get the "Possible loss of precision" error.



Related Topics



Leave a reply



Submit