I have a need to convert an Int32 value to a 3-byte (24-bit) integer. Endianness remains the same (little), but I cannot figure out how to move the sign appropriately. The values are already constrained to the proper range, I just can't figure out how to convert 4 bytes to 3. Using C# 4.0. This is for hardware integration, so I have to have 24-bit values, cannot use 32 bit.
Converting Int32 to 24-bit signed integer
9.3k Views Asked by drharris At
2
There are 2 best solutions below
10
Joe
On
Found this: http://bytes.com/topic/c-sharp/answers/238589-int-byte
int myInt = 800;
byte[] myByteArray = System.BitConverter.GetBytes(myInt);
sounds like you just need to get the last 3 elements of the array.
EDIT:
as Jeremiah pointed out, you'd need to do something like
int myInt = 800;
byte[] myByteArray = System.BitConverter.GetBytes(myInt);
if (BitConverter.IsLittleEndian) {
// get the first 3 elements
} else {
// get the last 3 elements
}
Related Questions in C#
- How to call a C language function from x86 assembly code?
- What does: "char *argv[]" mean?
- User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
- How to crop a BMP image in half using C
- How can I get the difference in minutes between two dates and hours?
- Why will this code compile although it defines two variables with the same name?
- Compiling eBPF program in Docker fails due to missing '__u64' type
- Why can't I use the file pointer after the first read attempt fails?
- #include Header files in C with definition too
- OpenCV2 on CLion
- What is causing the store latency in this program?
- How to refer to the filepath of test data in test sourcecode?
- 9 Digit Addresses in Hexadecimal System in MacOS
- My server TCP doesn't receive messages from the client in C
- Printing the characters obtained from the array s using printf?
Related Questions in INTEGER
- Python: why aren’t strings being internalized if they are received from ints by using str()?
- Covert a numbers list (pulled from excel) first into integer then string
- Pinescript Warning of only support to Simple Integer and asking to eliminate the Series Integer
- Get int value from Enum in Visual Scripting (Unity)
- Overcoming TypeError: can't multiply sequence by non-int of type 'list'
- int too large to convert to float, but even larger numbles can be handled
- Using an int from a for loop in another for loop. JAVA
- Alternatives to fractional types
- Is it possible to solve this sumDouble problem with an if else function?
- Checking if a string with leading zeros is a valid integer in Kotlin
- Why 00 is a valid integer in Python?
- How do I classify a float as an integer?
- Am having this error while trying to test my SMTP
- Comparing Multiple Integers in C Workaround
- R ggplot2: Is it possible to remove the zero label after using expand_limits(x = 0)?
Related Questions in ENDIANNESS
- np.diff on big-endian data seems slow
- NASM: little-endian WORD constant
- Converting int to bytes, with confusion over significant bits and endianness
- replacement for write/readXXXLE in netty 5
- How to convert from little endian hex into double in MySQL5.7
- Dart, Endianness in native structs
- Can C23 endianness macros be used to determine the layout of a bit-field?
- What happens when 4-byte mov is used to load multiple words?
- Safely converting a non-owning pointer of multiple uint8_t's to uint16_t's
- Dealing with endianess in Kotlin
- How to parse a DNS packet in network format in C
- Using a union to resolve compiler warning: dereferencing type-punned pointer will break strict-aliasing rules
- shell script to convert big endian to little endian
- Problem With Big Endian and Little Endian
- bswap breaks when using gcc with optimization
Related Questions in BITCONVERTER
- Sending integer with Python TCP socket and receiving with C# - Error receiving correct data
- C# Bitconverter Index[0] is wrong but everything else is correct
- C# performance - pointer to span in a hot loop
- Alternative to BitConverter that doesn't require fixed length, zero-padded byte arrays?
- Bitcoin arbitrage collection formula
- Convert eight bytes into a string
- C# BitConverter byte array to ushort (int16, double, boolean, single, int32, ) to use in Unreal
- How to Convert int to byte array and byte array to Int again? (Edit)
- Why this unexpected output when converting Vector3[] to Byte[] and back using BitConverter?
- How to convert float value to byte in c#
- C# signed fixed point to floating point conversion
- Does BitConverter handle little-endianness incorrectly?
- Why does BitConverter seemingly return incorrect results when converting floats and bytes?
- How can I convert from a Byte Array to Generic Array?
- c# 1D-byte array to 2D-double array
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
If you want to do that conversion, just remove the top byte of the four-byte number. Two's complement representation will take care of the sign correctly. If you want to keep the 24-bit number in an
Int32variable, you can usev & 0xFFFFFFto get just the lower 24 bits. I saw your comment about the byte array: if you have space in the array, write all four bytes of the number and just send the first three; that is specific to little-endian systems, though.