printf("%x") 없이 이진수를 16진수로 바꾸는 프로그램 만들고 싶습니다.

harry p의 이미지

putchar ('0' + 2) 은 출력 "2"
putchar ('a' + 1) 은 출력 "b",
putchar('a' + 2) 은 출력 "c",

로 출력하려고 하는데요.

#include <stdio.h> 
 
int main(void) {
    printf("\n");
    print_binary(65535);
    print_binary(1<= 0) {
        int mask = 1 << curr_bit_num;
        int bit = ((mask & num) >> curr_bit_num) & 1;
        putchar('0' + bit);
        if (curr_bit_num > 0 && curr_bit_num % 4 == 0) {
            putchar('_');
        }
        curr_bit_num--;
    }
 
    printf("    is binary for %d\n", num);
}

이런 코드가 있습니다.
근데 여기서 함수를 추가해서 출력 하라는데
도저히 무슨 말인지 모르겠어서 질문 올립니다.....

세벌의 이미지

https://linux.die.net/man/3/putchar
putchar 함수를 오버로딩 하려는 건가요?
질문의 뜻을 잘 모르겠네요.

harry p의 이미지

죄송합니다.
제가 코드를 잘못 올렸네요.

void print_binary(double num) ;

int main(void) {
printf("\n");
print_binary(65535);
print_binary(1<<31 | 1<<30 | 1<<5 | 1<<0);
print_binary(-7);
print_binary(-7.0);
print_binary(42);
printf("\n");
return 0;
}

void print_binary(double num) {

int curr_bit_num = (sizeof(int) * 8 - 1);

while (curr_bit_num >= 0) {
int mask = 1 << curr_bit_num;
int bit = ((mask & num) >> curr_bit_num) & 1;
putchar('0' + bit);
if (curr_bit_num > 0 && curr_bit_num % 4 == 0) {
putchar('_');
}
curr_bit_num--;
}

printf(" is binary for %d\n", num);
}

이 코드 입니다.

세벌의 이미지

소스코드는 code 태그 안에 넣어주셔요.
https://kldp.org/node/158191 참고.
소스코드 보기 불편하면, 바라는 답을 얻을 확률이 낮아지겠죠?

twinwings의 이미지

#include <stdio.h>
 
char encode(int __4) {
	const static char encode_table[] = "0123456789abcdef";
	return encode_table[__4];
}
 
typedef struct __32_s{
	unsigned char _4 : 4;
	unsigned char _8 : 4;
	unsigned char _12 : 4;
	unsigned char _16 : 4;
	unsigned char _20 : 4;
	unsigned char _24 : 4;
	unsigned char _28 : 4;
	unsigned char _32 : 4;
} __32;
 
void my_hex_print(unsigned int x) {
	__32 *p = (__32*)&x;
	putchar(encode(p->_32));
	putchar(encode(p->_28));
	putchar(encode(p->_24));
	putchar(encode(p->_20));
	putchar(encode(p->_16));
	putchar(encode(p->_12));
	putchar(encode(p->_8));
	putchar(encode(p->_4));
}
 
int main() {
	unsigned int x = 0x1234abcd;
	my_hex_print(x);
 
	return 0;
}
bushi의 이미지

[schoi0@SEL-SCHOI0-D2 net]$ gcc -o h-amd hex.c -static
[schoi0@SEL-SCHOI0-D2 net]$ powerpc-linux-gnu-gcc -o h-ppc hex.c -static
[schoi0@SEL-SCHOI0-D2 net]$ 
[schoi0@SEL-SCHOI0-D2 net]$ ./h-amd
1234abcd
[schoi0@SEL-SCHOI0-D2 net]$ ./h-ppc
dcba4321
 
[schoi0@SEL-SCHOI0-D2 net]$ readelf -h h-amd h-ppc|grep "File:\|Data:\|Machine"
File: h-amd
  Data:                              2's complement, little endian
  Machine:                           Advanced Micro Devices X86-64
File: h-ppc
  Data:                              2's complement, big endian
  Machine:                           PowerPC

bit field 사용할 땐 항상 endian 에 맞춰 field 순서를 완전히 뒤집어줘야해서...
twinwings의 이미지

예 옳은 지적이십니다.
그런데 고기를 잡아서 요리까지 해주는건 아닌거 같아서요...
엔디안 검사하고 엔디안 마다 조건부 컴파일로 비트필드 순서를 뒤집 짓까지하는 정성은 읍지영...

harry p의 이미지

감사합니다.

댓글 달기

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