endianness

Swapping endiannes in C

人走茶凉 提交于 2019-12-04 18:48:50
I have this string c1eb044f0708015b267913fc4dff5aabe3dd4a97f10f7ba935cd360000000000 How does one swap it so it becomes 000000000036cd35a97b0ff1974adde3ab5aff4dfc1379265b0108074f04ebc1 Those two are basically examples, but that is what i need to do, but not know how as i have very little knowledge of C. The above two strings are actually unsigned char[] in the C program P.S Don't think i didn't go through google. I did, but i found very little of what i needed so every attempt to do that failed. Something like this; probably not perfect but gives you the idea. You'll want appropriate error

Function convert Hex String to BitArray C#

江枫思渺然 提交于 2019-12-04 18:37:46
I created the following function which will do as requested (convert HEX string to BitArray). I am not sure about the efficiency of the function, but my main problem now is that the Convert.ToInt64 function is endian specific . When this is ported over to alternate chipsets we will get different results (or exceptions). So can anyone think of an alternate way to do this conversion??? public BitArray convertHexToBitArray(string hexData) { string binary_values = ""; BitArray binary_array; if (hexData.Length <= "FFFFFFFFFFFFFFFF".Length) // Max Int64 { binary_values = Convert.ToString(Convert

Why do inet_ntoa and inet_ntop “reverse” the bytes?

£可爱£侵袭症+ 提交于 2019-12-04 18:37:00
This is a rather basic problem with which, to my surprise, I've had a problem today. It looks to me like inet_pton and inet_ntoa are reversing the bytes of the IP address they're given: DWORD IP; inet_pton(AF_INET, "192.168.0.1", &IP); printf("%08X\n", IP); This will print 0100A8C0 . And well, if we break down the bytes, it's 01.00.A8.C0 = 1.0.168.192 . Similarly: IP = 0x7F000001; struct in_addr ia; ia.S_un.S_addr = IP; printf("%s\n", inet_ntoa(ia)); gives me 1.0.0.127 . The first thing that comes to mind is endianness, but I've read the MSDN documentation ( 1 and 2 ) and the byte order is not

What's a portable way of converting Byte-Order of strings in C

僤鯓⒐⒋嵵緔 提交于 2019-12-04 15:36:26
I am trying to write server that will communicate with any standard client that can make socket connections (e.g. telnet client) It started out as an echo server, which of course did not need to worry about network byte ordering. I am familiar with ntohs, ntohl, htons, htonl functions. These would be great by themselves if I were transfering either 16 or 32-bit ints, or if the characters in the string being sent were multiples of 2 or 4 bytes. I'd like create a function that operates on strings such as: str_ntoh(char* net_str, char* host_str, int len) { uint32_t* netp, hostp; netp = (uint32_t*

JavaScript equivalent to htonl?

老子叫甜甜 提交于 2019-12-04 13:13:53
For an AJAX request, I need to send a magic number as the first four bytes of the request body, most significant byte first, along with several other (non-constant) values in the request body. Is there something equivalent to htonl in JavaScript? For example, given 0x42656566, I need to produce the string "Beef". Unfortunately, my number is along the lines of 0xc1ba5ba9. When the server reads the request, it is getting the value -1014906182 (instead of -1044751447). There's no built-in function, but something like this should work: // Convert an integer to an array of "bytes" in network/big

what is the benefit of detecting endian at runtime?

泄露秘密 提交于 2019-12-04 13:04:36
i've searched for macro's to determine endianess on a machine and didn't found any standard proprocessor macros for this, but a lot of solutions doing that on runtime. why should i detect endianess at runtime? if i do somthing like that: #ifdef LITTLE_ENDIAN inline int swap(int& x) { // do swap anyhow return swapped; } #elif BIG_ENDIAN inline int& swap(int& x) { return x; } #else #error "some blabla" #endif int main() { int x = 0x1234; int y = swap(x); return 0; } the compiler will generate only one function. but if i do it like (see predef.endian ): enum { ENDIAN_UNKNOWN, ENDIAN_BIG, ENDIAN

Java Converting long to bytes - which approach is more efficient

时光总嘲笑我的痴心妄想 提交于 2019-12-04 12:12:18
I have two approaches to convert long to byte array. for (int i = 0; i < 7; i++) { data[pos + i] = (byte) (value >> (7- i - 1 << 3)); } and for (int i = 7; i >= 0; --i) { data[p + i] = (byte)(newl & 0xff); newl >>= 8; } which of the two operations is more efficient? I suggest you look at how the Java code does it. public final void writeLong(long v) throws IOException { writeBuffer[0] = (byte)(v >>> 56); writeBuffer[1] = (byte)(v >>> 48); writeBuffer[2] = (byte)(v >>> 40); writeBuffer[3] = (byte)(v >>> 32); writeBuffer[4] = (byte)(v >>> 24); writeBuffer[5] = (byte)(v >>> 16); writeBuffer[6] =

Why does an 8-bit field have endianness?

微笑、不失礼 提交于 2019-12-04 10:13:08
问题 See the definition of TCP header in /netinet/tcp.h: struct tcphdr { u_int16_t th_sport; /* source port */ u_int16_t th_dport; /* destination port */ tcp_seq th_seq; /* sequence number */ tcp_seq th_ack; /* acknowledgement number */ # if __BYTE_ORDER == __LITTLE_ENDIAN u_int8_t th_x2:4; /* (unused) */ u_int8_t th_off:4; /* data offset */ # endif # if __BYTE_ORDER == __BIG_ENDIAN u_int8_t th_off:4; /* data offset */ u_int8_t th_x2:4; /* (unused) */ # endif u_int8_t th_flags; # define TH_FIN

htonl() vs __builtin_bswap32()

让人想犯罪 __ 提交于 2019-12-04 10:09:59
__builtin_bswap32() is used to reverse bytes (it's used for littel/big endian issues (from gcc)). htonl() is used to reverse bytes too (conversion from host to network). I checked both functions and they returns the same result. Are there some one who can confirm that both functions do the same thing? (standard refences are appreciated) Just look at source code : (example from glib 2.18) #undef htonl #undef ntohl uint32_t htonl (x) uint32_t x; { #if BYTE_ORDER == BIG_ENDIAN return x; #elif BYTE_ORDER == LITTLE_ENDIAN return __bswap_32 (x); #else # error "What kind of system is this?" #endif }

Undefined behavior from pointer math on a C++ array

戏子无情 提交于 2019-12-04 08:57:23
问题 Why the output of this program is 4 ? #include <iostream> int main() { short A[] = {1, 2, 3, 4, 5, 6}; std::cout << *(short*)((char*)A + 7) << std::endl; return 0; } From my understanding, on x86 little endian system, where char has 1 byte, and short 2 bytes, the output should be 0x0500 , because the data in array A is as fallow in hex: 01 00 02 00 03 00 04 00 05 00 06 00 We move from the beginning 7 bytes forward, and then read 2 bytes. What I'm missing? 回答1: You are violating strict