함수를 안쓰고 문자열을 16진수로 표기??

cs010101의 이미지


라이브러리 함수를 사용안하고,
문자열(아이피주소) 192.168.0.2 를 \xC0\xA8\x00\x02 로 바꾸고 싶습니다.

unsigned char *ip_address="\xC0\xA8\x00\x02"
이렇게 사용하기 위해서 입니다.

간단할거 같은데...-.-
머리가 돌이라서 잘 생각이 안나네요.

감사합니다.

nohmad의 이미지

scanf나 atoi 정도라도 쓸 수 있었으면 좀 쉬우련만, 전부 다 만들어 쓰려니 좀 골치 아팠습니다;

#include <stdio.h>
 
unsigned char
nch_to_uch(char* nch)
{
    int d = 1;
    unsigned char s = 0;
    while (*++nch);
    while (*--nch) {
        s += (unsigned char) (*nch - 48) * d;
        d *= 10;
    }
    return s;
}
 
void
reduce_ipaddr(const char* src, unsigned char* dst)
{
    int i, j;
    char buf[16] = {0, };
    for (i = j = 0; src[i]; i++) {
        if (src[i] == '.') {
            buf[i] = 0;
            *dst++ = nch_to_uch(buf + j);
            j = i + 1;
            continue;
        }
        buf[i] = src[i];
    }
    *dst++ = nch_to_uch(buf + j);
}
 
int
main(int argc, char** argv)
{
    const char* ipstr = "192.168.0.2";
    unsigned char ip4ch[4];
    reduce_ipaddr(ipstr, ip4ch);
    printf("%s => %02X %02X %02X %02X\n", ipstr, ip4ch[0], ip4ch[1], ip4ch[2], ip4ch[3]);
 
    return 0;
}

$ruby.is_a?(Object){|oriented| language} #=> true
http://rubykr.org

cshwang-1의 이미지

#include <stdio.h>
 
static void ipstr2ch(char *ipstr, unsigned char *ipch)
{
        for(*ipch = 0; *ipstr; ipstr++) {
                if(*ipstr == '.')
                        *++ipch = 0;
                else
                        *ipch = *ipch * 10 + (*ipstr - '0');
        }
}
 
int main(int argc, char *argv[])
{
        char *dstr = "192.168.0.2";
        unsigned char cip[4];
 
        ipstr2ch(dstr, cip);
        printf("%x %x %x %x\n", cip[0],cip[1],cip[2],cip[3]);
 
        return 0;
}
nohmad의 이미지

코드도 짧고, 변수도 안 쓰고, 정말 멋진 방법이네요~.

$ruby.is_a?(Object){|oriented| language} #=> true
http://rubykr.org

cs010101의 이미지

조언해주셔서 감사합니다~~~

-----------------------------------------------
어머니,
그 이름만으로도 우리는 풍요로와 집니다.

효도합시다......
-----------------------------------------------

cs010101의 이미지

unsigned char
nch_to_uch(char* nch)
{
int d = 1;
unsigned char s = 0;
while (*++nch);
while (*--nch) {
s += (unsigned char) (*nch - 48) * d;
d *= 10;
}
return s;
}

이것 좀 설명해 주시겠어요? *^_^*

-----------------------------------------------
어머니,
그 이름만으로도 우리는 풍요로와 집니다.

효도합시다......
-----------------------------------------------

nohmad의 이미지

버그가 있군요. 다시 올립니다.

unsigned char
nch_to_uch(char* nch)
{
    int i = 0, d;
    unsigned char s = 0;
    while (*nch++)
        ++i;
    for (d = 1, *--nch; i > 0; --i) {
        s += (unsigned char) (*--nch - '0') * d;
        d *= 10;
    }
    return s;
}

0~255를 나타내는 n개(1~3)의 캐릭터를 1개의 unsigned char로 바꾸는 겁니다. 표준라이브러리의 atoi를 쓰면

#include <stdlib.h>
 
unsigned char
nch_to_uch(char* nch)
{
    return (unsigned char) atoi(nch);
}

와 같은 결과를 만듭니다.

$ruby.is_a?(Object){|oriented| language} #=> true
http://rubykr.org

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.