用C交换字节序 [英] Swapping endiannes in C
问题描述
我有这个字符串
c1eb044f0708015b267913fc4dff5aabe3dd4a97f10f7ba935cd360000000000
一个人怎么掉它,所以它成为
How does one swap it so it becomes
000000000036cd35a97b0ff1974adde3ab5aff4dfc1379265b0108074f04ebc1
这两个基本的例子,但是这就是我需要做的,但不知道怎么,我对C知之甚少。
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.
以上两个字符串实际上是无符号的字符[]在C程序
The above two strings are actually unsigned char[] in the C program
P.S
别以为我没有去通过谷歌。我这样做,但我发现很少的东西,我需要让每一个试图这样做失败了。
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 checking, initialization of your buffer, etc.
编辑:我做了一个假设,我不应该,有可能。我间preTED你的字符串作为十六进制重新字节presentations,所以我把 C1
为 unsigned char型
和 00
,例如。如果字符串实际上是小写字母c,1号等,然后弥的回答是你想要的,不是我的。
I made an assumption I shouldn't have, possibly. I interpreted your string as hex representations of bytes, so I took c1
as an unsigned char
and switched it with 00
, for example. If your string is actually lowercase c, the number 1, etc., then Micah's answer is what you want, not mine.
void reverse_string(unsigned char *buf, int length)
{
int i;
unsigned char temp;
for (i = 0; i < length / 2; i++)
{
temp = buf[i];
buf[i] = buf[length - i - 1];
buf[length - i - 1] = temp;
}
}
这篇关于用C交换字节序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!