 Tags IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 # Convert 4 bytes to int

By : sexy boy
Date : October 15 2020, 11:12 PM
like below fixes the issue ByteBuffer has this capability, and is able to work with both little and big endian integers.
Consider this example: code :

## How to turn this simple 5 bytes back into 4 bytes ? (algorithm to convert 4 bytes into 5 bytes is known)

By : user2633784
Date : March 29 2020, 07:55 AM
Any of those help The algorithm to convert input 8 digit hex number into 10 digit are following: , From this conversion algorithm:
code :
``````x1 = 1 * 16^8 * 2^3
x2 = 2 * 16^7 * 2^2
x3 = 3 * 16^6 * 2^1
x4 = 4 * 16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0
``````
``````x1 = 1 * 16^7 * 16^1 * 2^3
x2 = 2 * 16^6 * 16^1 * 2^2
x3 = 3 * 16^5 * 16^1 * 2^1
x4 = 4 * 16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0
``````
``````x1 = 1 * 16^7 * 2^4 * 2^3
x2 = 2 * 16^6 * 2^4 * 2^2
x3 = 3 * 16^5 * 2^4 * 2^1
x4 = 4 * 16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0
``````
``````x1 = 1 * 16^7 * 2^7
x2 = 2 * 16^6 * 2^6
x3 = 3 * 16^5 * 2^5
x4 = 4 * 16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0
``````
``````x1 = 1 * 16^7 << 7
x2 = 2 * 16^6 << 6
x3 = 3 * 16^5 << 5
x4 = 4 * 16^4 << 4
x5 = 5 * 16^3 << 3
x6 = 6 * 16^2 << 2
x7 = 7 * 16^1 << 1
x8 = 8 * 16^0 << 0
``````
``````AAAABBBB CCCCDDDD EEEEFFFF GGGGHHHH
``````
``````0AAAA0BB BB0CCCC0 DDDD0EEE E0FFFF0G GGG0HHHH
``````
``````void convert(unsigned char five[], unsigned char four[]) {
four = (five << 1) & 0xF0  // 11110000
| (five << 2) & 0x0C  // 00001100
| (five >> 6) & 0x03; // 00000011
four = (five << 3) & 0xF0  // 11110000
| (five >> 4) & 0x0F; // 00001111
four = (five << 5) & 0xE0  // 11100000
| (five >> 3) & 0x10  // 00010000
| (five >> 2) & 0x0F; // 00001111
four = (five << 7) & 0x80  // 10000000
| (five >> 1) & 0x70  // 01110000
| (five)      & 0x0F; // 00001111
}
``````
`````` 08 42 10 84 21      11 11 11 11
10 84 21 8C 63      22 22 33 33
52 D8 D0 88 64      AB CD 12 34
21 4E 84 98 62      45 78 96 32
7B DE F7 BD EF      FF FF FF FF
``````

## How do I convert the database size from mega bytes to bytes or vice-versa?

By : OkayBoy
Date : March 29 2020, 07:55 AM
should help you out Bytes to Megabytes = Bytes / (1024 * 1024)
Megabytes to Bytes = Megabytes * (1024 * 1024.0)

## Fixed Point number 4 bytes or 8 bytes with different scale factors - library to convert numbers?

By : faheem kennedy
Date : March 29 2020, 07:55 AM
Hope that helps Perhaps you need to use a 'bignum' library. Might be overkill but here's one example: GMP

## How to convert an array of 27 bytes where each 3 bytes is a 2's complement number into its numeric equivalent?

Date : March 29 2020, 07:55 AM
Hope that helps I suggest parsing your 3-byte values as a single signed byte and an unsigned (16-bit) short. You can then use mathematical operators to recombine them into single number.
Here's a list comprehension that does it all in one step (given s as a bytearray or bytes string):
code :
``````[(a<<16) + b for a, b in zip(*([iter(struct.unpack('>'+'bH'*9, s))]*2))]
``````
``````[((a if a < 128 else a-256)<<16) + (b<<8) + c for a, b, c in zip(*([iter(data)]*3))]
``````

## Convert bytes data inside a string to a true bytes object

By : Alex Golubev
Date : March 29 2020, 07:55 AM
To fix this issue In Python 3, I have a string like the following: , mystr.encode("latin-1") is what you want. 