[완료]Byte Order 바꾸는 함수 사용 문제(htns, htonl, ntohs, ntohl)

neobug의 이미지

Byte order를 바꾸는 함수를 사용해도 변화가 없어서 질문 올립니다.
함수는 ntohl()로도 해보고 htonl() 2가지 다 해보았지만 결과는 같았습니다.
data type 선언도 unsigned long int 로도 해보았지만 같았습니다.

int main()
{
        int data = 0x00001000;
 
        data = ntohl(data);
        printf("%02X\n", data);
        printf("%d\n",data);
}

코드의 결과 화면은 아래와 같습니다.
1000
4096

sangwoo의 이미지

data = 1;
로 하시고 해보세요~
----
Let's shut up and code.

----
Let's shut up and code.

neobug의 이미지

어떤의미의 조언인지 잘 모르겠습니다.
결과는
01
1
입니다.

sangwoo의 이미지

쓰고 나서 잘못 생각했다는 걸 알았습니다. 근데 글을 지울 수가 없더군요;;
매크로 같은 걸로 만들어 쓰시는게 좋을 거 같네요 ^^;;
----
Let's shut up and code.

----
Let's shut up and code.

neobug의 이미지

관심만 가져주셔도 고마울 나름입니다 ㅎㅎ

익명 사용자의 이미지

OS에 따라서 network byteorder하고 host byteorder하고 같은 경우가 있고
이런경우에는 아무런 변화도 일어나지 않는거 아닌가요?

n to h (netowrk to host) long

neobug의 이미지

저도 그런건 아닐까 하고 생각하고 있습니다
헌데 맞는지 모르겠어요
sparc솔라리스가 그런가요?

neobug의 이미지

i686 아키텍쳐의 linux에서 해보니 잘 바뀌는 군요.
sparc 아키텍쳐는 host byte order가 network byte order와 같네요

결과화면 입니다
1000
4096
100000
1048576

익명 사용자의 이미지

사용중인 컴퓨터가 빅엔디안이 아닌가요?? ppc등...

neobug의 이미지

UNIX 서버에 로그인해서 짜고 있는데요.
호스트 이름이 e3000 이거든요.
하지만 관계없습니다.
htonl()도 해보고
ntohl()도 해봤지만
0x00001000 이라는 byte order가
0x00100000 으로 Converting 되지 않고 있습니다.

superkkt의 이미지

스팍은 빅엔디안입니다. 그러면 위 결과가 맞겠죠. 네트웍 오더도 빅엔디안이니까요.

======================
BLOG : http://superkkt.com

======================
BLOG : http://superkkt.com

neobug의 이미지

Big Endian Little Endian 관계없이
Byte Order만 거꾸로 뒤집어주는 함수는 없는지 알고 싶네요

superkkt의 이미지

/* Swap bytes in 32 bit value.  */
#define __bswap_constant_32(x) \
     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |           \
      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))

무조건 바꾸는 함수가 있는지는 모르겠구요. 일단 위 매크로는 /usr/include/bits/byteswap.h에 있는 내용입니다. 이거 참고해서 직접 만들어 쓰시면 되겠네요.

======================
BLOG : http://superkkt.com

======================
BLOG : http://superkkt.com

neobug의 이미지

순환shift 효과를 내는 함수를 만들려고 했었는데 요로코롬 쉽게 잘되네요 ㅎㅎ

int main()
{
        int dat1 = 0x00001000;
        short int dat2 = 0x0010;
 
        dat1 = ((dat1 & 0xff000000) >> 24) | ((dat1 & 0x00ff0000) >> 8) | \
((dat1 & 0x0000ff00) << 8) | ((dat1 & 0x000000ff) << 24); 
        dat2 = ((dat2 & 0xff00) >> 8) | ((dat2 & 0x00ff) << 8);
 
        printf("\nvalue: %08X\n", dat1);
 
        printf("\nvalue: %04X\n", dat2);
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.