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#
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in INTEGER
- String replace with integer not working
- How can I parse fixed-length, non-delimited integers with attoparsec?
- 0 randomly becomes 55?
- Why did Java 8 introduce *Integer.sum(int a, int b)*
- How do char and int work in C++
- Separating an Integer
- How do you generate specific random number?
- Regular expression that would allow numbers from 1-9 excluding 0 and alphabets
- How can I send a integer from my Java file to my XML folder? (Android Studio)
- C++ unsigned long doesn't wrap around after 4294967295
- Java format integer limiting width by truncating to the right
- Transform price with currency to single number if no numbers after comma
- how to stop the program if the value i get for amount_notes is not an integer?
- Get the big-endian byte sequence of integer in Python
- Mapping int to int (in Java)
Related Questions in ENDIANNESS
- why is an integer type sent upside down?
- why does a integer type need to be little-endian?
- Endianness for length shorter than word but more than a byte
- Correct way to unpack a 32 bit vector in Perl to read a uint32 written in C
- MIPS: accessing memory addresses with big/small endian
- Porting C endianness & pointers black magic to Swift
- Choosing endianness for new data formats
- trying to convert NSData of type (BigEndian) from BlueTooth to Int of type Little Endian in Swift
- How to change endianess settings in cortex m3?
- If write(0x01234567) is called on an instance of output stream, what will be written to the destination of the stream?
- Generic conversion of number arrays into bytes and vice-versa in C#
- Differences between objdump and xxd
- Big/Little Endians and MIPS : does load immediate reverse order?
- Convert between little-endian and big-endian floats effectively
- Endian Convertion for structures without knowing data type(s)
Related Questions in BITCONVERTER
- xxHash convert resulting in hash too long
- Convert byte[] array to a short[] array with half the length
- Converting Int32 to 24-bit signed integer
- Fast casting in C# using BitConverter, can it be any faster?
- Converting raw byte data to float[]
- Is there a better way to detect endianness in .NET than BitConverter.IsLittleEndian?
- C#, BitConverter.ToUInt32, incorrect value
- Azure switching from little endian to big endian when deployed
- How do I convert less than 8 bytes to a ulong in C#?
- c# concatenate byte[] and get string result
- Fastest way to get sort order byte array from signed integer
- How can I convert from a Byte Array to Generic Array?
- Why does BitConverter seemingly return incorrect results when converting floats and bytes?
- c# bitconverter.ToString convert to hexadecimal string
- Can BitConverter be used to reliably extract multi-byte values from an IL byte stream (as returned by MethodBody.GetILAsByteArray)?
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 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.